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

How to know what changed using a ResourceChange object?

jkpanera
Level 5
Level 5

He guys,

I am writing a ResourceChangeListener. I'm implementing onChange(List<ResourceChange> changes). But the list of changes has very little information.

getChangedPropertyNames(), addedPropertyNames(), removedPropertyNames() are all deprecated (and return null). So how do I know what was actually changed?

4 Replies
Himanshu_Singhal
Community Advisor
Community Advisor

@Component(

  service = ResourceChangeListener.class,

  property = {

  ResourceChangeListener.PATHS + "=" + "/apps/sample/i18n/",

  ResourceChangeListener.CHANGES + "=" + "ADDED"
   }

)

public class ChangedResourceHandler implements ResourceChangeListener {

   @Override
   public void onChange(@Nonnull List<ResourceChange> list) {

   try {

   for (final ResourceChange change : list) {

          change.getPath(); //Gives you the path where changes occurs

  }

  } catch (Exception e) {

   LOGGER.error("Exception {}", e);

  }

  }

}

Sample code for ResourceChange Listener. You can add type of event i.e. ADDED, CREATED
And, inside onChnage method, you get the list of paths which has changed.

What else do you need apart from PATH where change occurs because using that path, you can get resource and once you've resource, then can get all resource related data.

jkpanera
Level 5
Level 5

Once I have the path, I can get the resource that changed, but I don't know which properties are new or changed. I can get the new properties but I don't know what values were there before.

Himanshu_Singhal
Community Advisor
Community Advisor

True. You can't get the information which one is old and which one is new. To get the information of what has specifically has changed, you'll have to get the previous version of page and for that, you should've the versioning enabled so every time you make the change, it creates a new version of page.

jkpanera
Level 5
Level 5

When I install the same content package twice, the onChange method is called twice even though nothing has actually changed the second time. So why does it get called?