How to use #onChange in ResourceChangeListener | Community
Skip to main content
Level 6
June 24, 2022
Solved

How to use #onChange in ResourceChangeListener

  • June 24, 2022
  • 2 replies
  • 1200 views

Hi

I would like to know if and why do I need the NotNull Constraint when implementing onChange in ResourceChangeListener. Here is the code:

 

public class ComponentCampaignCleanupListener implements ResourceChangeListener {

private static final Logger logger = LoggerFactory.getLogger(ComponentCampaignCleanupListener.class);

@Reference
private ResourceResolverService resourceResolverService;


@Override
public void onChange(List<ResourceChange> list) {...} //**Do I need here @NotNull for the list parameter and why?
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

I have tried both ways. It is working fine for me

@9944223 public void onChange(List<ResourceChange> changes) { logger.info("On Change Event !!!"); changes.forEach(change -> { logger.info("Resource event: {} at: {}", change.getType(), change.getPath()); }); } @9944223 public void onChange(@NotNull List<ResourceChange> changes) { logger.info("On Change Event !!!"); changes.forEach(change -> { logger.info("Resource event: {} at: {}", change.getType(), change.getPath()); }); }

 

 

2 replies

Adobe Employee
June 24, 2022

I dont think it is mandatory to define notnull here. Refer below 

https://github.com/apache/sling-org-apache-sling-discovery-commons/blob/4f7d7ca3224239d52798cc8418ec8283f5eddc9e/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/IdMapService.java 

 

The @126844 Annotation is, actually, an explicit contract declaring the following:

  1. A method should not return null.
  2. A variable (like fields, local variables, and parameters) cannot should not hold null value.

Additionally, @126844 is often checked by ConstraintValidators (eg. in spring and hibernate).

The @126844 annotation doesn't do any validation on its own because the annotation definition does not provide any ConstraintValidator type reference.

anasusticAuthor
Level 6
June 24, 2022

Thanks @tushar_gupta 

In my specific case the list variable could be null. I am listening to /content ADDED changes. In fact, if I add the @NotNull constraint AEM I cannot render any Page and get a compilation error.

The reason why I posted the question is that I thought according to the interface, this method should have a @NotNull annotation.

 

Here is the interface:

@ConsumerType
public interface ResourceChangeListener {
String PATHS = "resource.paths";
String CHANGES = "resource.change.types";
String PROPERTY_NAMES_HINT = "resource.property.names.hint";

void onChange(@NotNull List<ResourceChange> var1);
}

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
January 31, 2024

I have tried both ways. It is working fine for me

@9944223 public void onChange(List<ResourceChange> changes) { logger.info("On Change Event !!!"); changes.forEach(change -> { logger.info("Resource event: {} at: {}", change.getType(), change.getPath()); }); } @9944223 public void onChange(@NotNull List<ResourceChange> changes) { logger.info("On Change Event !!!"); changes.forEach(change -> { logger.info("Resource event: {} at: {}", change.getType(), change.getPath()); }); }

 

 

Aanchal Sikka
SantoshSai
Community Advisor
Community Advisor
June 24, 2022

Hi @anasustic ,

If you want to use Javax validation constraints to ensure that the List<ResourceChange> list

 being passed in is not null then @NotNull annotation is being used.

Fore more details please visit: https://docs.oracle.com/javaee/7/api/javax/validation/constraints/NotNull.html

Hope that helps!

Regards,

Santosh

Santosh Sai