Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

How to use Interface ResourceChangeListener

Avatar

Level 3

Hi,

in the past I used some static fields (e.g. SlingConstants.TOPIC_RESOURCE_ADDED, SlingConstants.TOPIC_RESOURCE_CHANGED, ...) to implement EventHandlers. Since AEM 6.2 this static fields are deprecated. In JavaDoc I found the hint to use ResourceChangeListener instead. But I didn't find any example how to use ResourceChangeListener. Has anybody an example how to use them?

regards
Reini

5 Replies

Avatar

Level 5

Not sure why it's not showing the @Deprecated annotation on any of the Sling API JavaDocs online[1], but if you open the org.apache.sling.api.SlingConstants class that's included in the AEM 6.2.0 UberJar, you'll see the following:

/** * The topic for the OSGi event which is sent when a resource has been added * to the resource tree. * The event contains at least the {@link #PROPERTY_PATH}, {@link #PROPERTY_RESOURCE_SUPER_TYPE} * and {@link #PROPERTY_RESOURCE_TYPE} properties. * @since 2.0.6 (Sling API Bundle 2.0.6) * * @deprecated Register a {@link ResourceChangeListener} instead */ @Deprecated public static final String TOPIC_RESOURCE_ADDED = "org/apache/sling/api/resource/Resource/ADDED"; /** * The topic for the OSGi event which is sent when a resource has been removed * from the resource tree. * The event contains at least the {@link #PROPERTY_PATH}. As the resource has already been removed * no further information like resource type etc. might be available. * @since 2.0.6 (Sling API Bundle 2.0.6) * * @deprecated Register a {@link ResourceChangeListener} instead */ @Deprecated public static final String TOPIC_RESOURCE_REMOVED = "org/apache/sling/api/resource/Resource/REMOVED"; /** * The topic for the OSGi event which is sent when a resource has been changed * in the resource tree. * The event contains at least the {@link #PROPERTY_PATH}, {@link #PROPERTY_RESOURCE_SUPER_TYPE} * and {@link #PROPERTY_RESOURCE_TYPE} properties. * Since 2.2.0 the event might contain these properties {@link #PROPERTY_ADDED_ATTRIBUTES}, * {@link #PROPERTY_REMOVED_ATTRIBUTES}, {@link #PROPERTY_CHANGED_ATTRIBUTES}. All of them are * optional. * @since 2.0.6 (Sling API Bundle 2.0.6) * * @deprecated Register a {@link ResourceChangeListener} instead */ @Deprecated public static final String TOPIC_RESOURCE_CHANGED = "org/apache/sling/api/resource/Resource/CHANGED";

[1] http://sling.apache.org/apidocs/sling8/org/apache/sling/api/SlingConstants.html#TOPIC_RESOURCE_CHANG...

Avatar

Level 5

Also worth noting, in my own search for replacement of these deprecations in our own listener classes, I stumbled onto this code sample from Adobe Consulting Services' ACS AEM Samples github[1]: SampleResourceChangeListener.java[2].

Looks like exactly what you need, Reini-.

[1] https://github.com/Adobe-Consulting-Services/acs-aem-samples
[2] https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/ad...

Avatar

Community Advisor

Please refer to the examples on : https://techrevelhub.wordpress.com/2017/03/15/resourcechangelistener/ 

 

It has code samples for Resource Change and Property Change as well


Aanchal Sikka