ResourceChangeListener | Community
Skip to main content
Level 2
May 6, 2024
Solved

ResourceChangeListener

  • May 6, 2024
  • 3 replies
  • 3111 views

I'm writing a ResourceChangeListener to check for changes in a particular property ( for ex : "cq:lastReplicated") 
But the listener gets triggered for any kind of change, I want it to listen only to the property specified in the 

"ResourceChangeListener.PROPERTY_NAMES_HINT"
 
I don't want to use EventListener because I need to listen to multiple changes at once

Here is my example code

 

package com.demo.core.jobs; import java.util.List; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.osgi.framework.Constants; /** * ResourceChangeListener */ @Component(service = ResourceChangeListener.class, property = { Constants.SERVICE_DESCRIPTION + "=Demo to listen on changes in the replication", ResourceChangeListener.PATHS + "=" + "/content/dam", ResourceChangeListener.CHANGES + "=" + "CHANGED", ResourceChangeListener.PROPERTY_NAMES_HINT + "=" + "cq:lastReplicated" }) public class ResourceListenerTest implements ResourceChangeListener { /** * logger for logging. */ private final Logger logger = LoggerFactory.getLogger(getClass()); /** * Override */ public final void onChange(final List<ResourceChange> changes) { for (ResourceChange change : changes) { String path = change.getPath(); logger.info("Handling resource change at: {}", path); } } }

 

 

 
Any suggestion on how to make this work would be appreciated. 
Thanks
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

Hi @aanchal-sikka 

I want to know the name of the property that has been changed, path isn't of much use in my case


@hiteshra3 

 

If my memory serves me right, following combination of path regex and getPath returns path till property. 

 

@Component(service = ResourceChangeListener.class, immediate = true, property = { ResourceChangeListener.PATHS + "=glob:/content/techrevelaemsite/us/**/products/**", ResourceChangeListener.CHANGES + "=" + "ADDED", ResourceChangeListener.CHANGES + "=" + "CHANGED", ResourceChangeListener.CHANGES + "=" + "REMOVED" } ) public class SimpleResourceListener implements ResourceChangeListener { private final Logger logger = LoggerFactory.getLogger(getClass()); @Override public void onChange(List<ResourceChange> changes) changes.forEach(change -> { logger.info("Resource event: {} at: {}", change.getType(), change.getPath()); }); } }

 Source: https://techrevel.blog/2017/03/15/resourcechangelistener/

3 replies

Jineet_Vora
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 6, 2024

@hiteshra3 - From the Apache documentation mentioned here https://sling.apache.org/apidocs/sling9/org/apache/sling/api/resource/observation/ResourceChangeListener.html#PROPERTY_NAMES_HINT, its states below:

An optional hint indicating to the underlying implementation that for changes regarding properties (added/removed/changed) the listener is only interested in those property names listed inhere. If the underlying implementation supports this, events for property names that are not enlisted here will not be delivered, however events concerning resources are not affected by this hint. This is only a hint, a change listener registering with this property must be prepared that the underlying implementation is not able to filter based on this property. In this case the listener gets all events as defined with the other properties.

 

Hope this helps!

HiteshRa3Author
Level 2
May 6, 2024

Hi @jineet_vora  thanks for the response.
Is there any way to filter out changes based on a specific property change, from what I see 

change.getChangedPropertyNames() method is deprecated so there must be a better way to achieve this
aanchal-sikka
Community Advisor
Community Advisor
May 7, 2024

@hiteshra3 

 

Can you please try printing change.getPath()? I guess it gives you complete path of the change

Aanchal Sikka
kautuk_sahni
Community Manager
Community Manager
May 16, 2024

@hiteshra3 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
Level 2
November 5, 2024

HI @hiteshra3 

 

we have the similar requirement for that we have used workflow launcher.