Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Query on ResourceChangeListener

Avatar

Employee Advisor

Hi All,

I was trying to capture resource change events using ResourceChangeListener but, nothing is getting captured in log file.

Here, resource is - cq:page and it has one property called: 'interval' at /content/poll/jcr:content.

Here is my code -

import java.util.List;

import org.apache.felix.scr.annotations.Properties;

import org.apache.felix.scr.annotations.Property;

import org.apache.felix.scr.annotations.Service;

import org.apache.sling.api.resource.LoginException;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.observation.ResourceChange;

import org.apache.sling.api.resource.observation.ResourceChangeListener;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.aemtraining.service.SessionHandlerService;

@Component(immediate = true)

@Service(value = {ResourceChangeListener.class})

@Properties(value = {

@Property(name = ResourceChangeListener.PATHS, value = { "/content/poll/jcr:content" }),

@Property(name = ResourceChangeListener.CHANGES, value = { "CHANGED"}, propertyPrivate=true),

@Property(name = ResourceChangeListener.PROPERTY_NAMES_HINT, value={"interval"})

})

public class PropertyChangeListener implements ResourceChangeListener {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Reference

private SessionHandlerService sessionHandlerService;

@Override

public void onChange(List<ResourceChange> resourceChangeList) {

ResourceResolver resourceResolver = null;

try {

resourceResolver = sessionHandlerService.getResourceResolver();

for (ResourceChange resourceChange : resourceChangeList) {

String resourcepath = resourceChange.getPath();

logger.info("******* RESOURCE PATH *******"+ resourcepath);

Resource resource = resourceResolver.getResource(resourcepath);

logger.info("******* RESOURCE NAME *******"+ resource.getName());

}

} catch (LoginException e) {

e.printStackTrace();

}

}

}

I am using AEM 6.3 and I have used archetype 11.

Please have a look and let me know if I have missed anything.

Thanks ,

Debal

1 Accepted Solution

Avatar

Correct answer by
Level 1

For AEM 6.3 & archetype 11, change structure to:

@Component(

     immediate = true,

     service = ResourceChangeListener.class,

     property = {

      ResourceChangeListener.PATHS + "=/content/poll/jcr:content",

      ResourceChangeListener.CHANGES + "=CHANGED",

      ResourceChangeListener.PROPERTY_NAMES_HINT + "=interval"

     }

  )

public class PropertyChangeListener implements ResourceChangeListener {

....

reference: Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache S...

View solution in original post

3 Replies

Avatar

Correct answer by
Level 1

For AEM 6.3 & archetype 11, change structure to:

@Component(

     immediate = true,

     service = ResourceChangeListener.class,

     property = {

      ResourceChangeListener.PATHS + "=/content/poll/jcr:content",

      ResourceChangeListener.CHANGES + "=CHANGED",

      ResourceChangeListener.PROPERTY_NAMES_HINT + "=interval"

     }

  )

public class PropertyChangeListener implements ResourceChangeListener {

....

reference: Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache S...

Avatar

Employee Advisor

Hi Jack,

I have done relevant changes based on your suggestion and its working.

Here is the log out -

09.09.2017 00:10:03.249 *INFO* [sling-oak-observation-5970] some-package.PropertyChangeListener ******* RESOURCE PATH *******/content/poll/jcr:content

09.09.2017 00:10:03.257 *INFO* [sling-oak-observation-5970] some-package.PropertyChangeListener ******* RESOURCE NAME *******jcr:content

Thanks ,

Debal