Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

ResourceChangeListener for page moves: Will the listener receive a pair of events (ADDED and REMOVED) in a single call only when a page has been moved?

Avatar

Level 3

Hi, 

 

If a page is moved, then my listener shall react to it. - The listener needs the old and the new path of the moved page in order to move another resource which is located at a different root path in the repository accordingly. 

The listener implements the ResourceChangeListener and therefore has the following method: 

public void onChange(final List<ResourceChange> changes)

All pages which are observed by the listener are located under "/content/". 

According to my analysis, the listener receives the following events when a page is moved: 

  • several CHANGED events - each CHANGED event in a separate call to the onChange method, i.e. as a single object in the input list,
  • afterwards one list (ResourceChangeList) which contains 2 JcrResourceChange events of type ADDED and REMOVED:
    [ResourceChange[type=ADDED, path=/content/b/c, external=false], ResourceChange[type=REMOVED, path=/content/a/c, external=false]]

This last pair of events (ADDED and REMOVED) is interesting to me, because it contains the old and new path of the moved page. The old path is located in the event of type REMOVED and the new path in the event of type ADDED. 

My question is: Will the listener receive such a pair of events (ADDED and REMOVED) in a single call only when a page has been moved? - This is my assumption, but despite a long search I haven't found any specification of this behavior, therefore I ask you here. 

Is it maybe possible that the listener will receive such a pair of events (ADDED and REMOVED) in a single call if two different pages are added and removed exactly at the same time? 

I ask, because I need the old and new path of the moved page and want to make sure that such a pair of events will be received by the listener in a single call only in case of a moved page and not in any other case. 

 

Thank you in advance for your help. 

8 Replies

Avatar

Community Advisor

with JCR event you can get old and new property in a same event https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SampleJCREvent.java 

 

When you move both event execute.

Example: 

 

08/09/2022, 17:40:00 org/osgi/framework/ServiceEvent/MODIFIED Service (id=29, objectClass=org.osgi.service.component.runtime.ServiceComponentRuntime, bundle=org.apache.felix.scr) modified
08/09/2022, 17:39:59 org/apache/sling/api/resource/Resource/REMOVED
path /var/dam/download/test
event.topics org/apache/sling/api/resource/Resource/REMOVED
userid admin
08/09/2022, 17:39:59 org/apache/sling/api/resource/Resource/ADDED
path /var/dam/download/test2
event.topics org/apache/sling/api/resource/Resource/ADDED
userid admin
resourceType nt:unstructured

 

Avatar

Level 3

Hi 

 

Thank you for your reply. Is this solution not deprecated? - I don't know, I just ask you. 

 

I had read the blog post "The deprecation of Sling Resource Events" by Jörg_Hoh and understood from it earlier, that the solution using the ResourceChangeListener is generally recommended, therefore I have tried to use it. 

However, I am not sure if the solution proposed by you (using a javax.jcr.observation.EventListener) is also deprecated or not. - It's not clear to me. 

As far as I understand the contents of the aforementioned blog post by Jörg_Hoh, the solution using an org.osgi.service.event.EventHandler is deprecated. - We had indeed even used it in our system in the past and can now see some warnings in our logs due to it, as described by Jörg_Hoh

 

Is a solution using the ResourceChangeListener more recommended or better in any way than the one proposed by you (using a javax.jcr.observation.EventListener)? 

 

Thank you in advance for your help. 

Avatar

Level 3

Hi 

Will the ResourceChangeListener receive a pair of events (ADDED and REMOVED) in a single call only when a page has been moved and not in any other case? 

Is a solution using the ResourceChangeListener more recommended or better in any way than using a javax.jcr.observation.EventListener? 

Avatar

Employee Advisor

You can use ResourceChangeEvents for that, but explicitly for pages there other events on a page level, and IIRC there might even be a MOVE event (I have seen code for that once).

Avatar

Level 3

Hi

Thank you for your reply. Do you mean that the ResourceChangeListener will receive such a pair of events (ADDED and REMOVED) in a single call only when a page has been moved and not in any other case? - It is very important for me to be sure about that, because otherwise the solution implemented by me might work incorrectly. 

 

Which events on a page level do you mean as an alternative to using the ResourceChangeListener? Do you mean using javax.jcr.observation.EventListener (events at the JCR level)? - It has an event javax.jcr.observation.Event.NODE_MOVED when a node is moved. 

Do I understand correctly that the javax.jcr.observation.EventListener (using events at the JCR level) isn't deprecated, but only the org.osgi.service.event.EventHandler (using events at the Sling level) is deprecated? - Please also see my reply above to  regarding this subject. 

Reading your blog post "The deprecation of Sling Resource Events" has led me to use the ResourceChangeListener, but maybe the javax.jcr.observation.EventListener will be better in my case? 

Avatar

Level 1

Hi, I think you should check type as CHANGED

 https://sling.apache.org/apidocs/sling9/org/apache/sling/api/resource/observation/ResourceChange.Cha...

I have noticed in case you move a resource, you will not get REMOVED event. 

 

Something like this

ResourceChangeListener.PATHS + "=glob:/content/dam/project-xyz/wip/us/en/**/content-fragments/**/jcr:content/model/variations/**", ResourceChangeListener.CHANGES + "=" + "CHANGED"

Avatar

Level 3

Hi, 

I am using AEM 6.5 on premise version and in my case it works in a different way than described by you. It works in my case exactly as I have written in my original question: 

According to my analysis (I have checked it in a debugger), the listener receives the following events when a page is moved: 

  • several CHANGED events - each CHANGED event in a separate call to the onChange method, i.e. as a single object in the input list,
  • afterwards one list (ResourceChangeList) which contains 2 JcrResourceChange events of type ADDED and REMOVED

PS. My listener has the following configuration: 

@Component(service = ResourceChangeListener.class, configurationPolicy = ConfigurationPolicy.IGNORE, property = {
Constants.SERVICE_VENDOR + "=" + "xyz",
ResourceChangeListener.PATHS + "=/content",
ResourceChangeListener.CHANGES + "=ADDED",
ResourceChangeListener.CHANGES + "=CHANGED",
ResourceChangeListener.CHANGES + "=REMOVED",
})