Expand my Community achievements bar.

SOLVED

AEM Sling Event Listener Vs JCR Observation

Avatar

Level 10

Hi all,

 

What is the difference between these two?

I see that Sling Event Listener is more performant.

 

This apart, why we have these two?

Only one of these two would have sufficed?
When to use what?

 

Appreciate all your responses.

 

Thanks,

RK.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

It is the API Preference - AEM Rule of thumb or hierarchy.

The general rule is to prefer the APIs/abstractions the following order:

 

  1. AEM
  2. Sling
  3. JCR
  4. OSGi

https://www.linkedin.com/pulse/aem-apis-application-programming-interfaces-suresh-dhulipudi-za2if/?t...

 

 

Both AEM Sling Event Listener and JCR Observation are used to listen for and react to events in the AEM system, but they operate at different levels.

JCR Observation

JCR Observation is a lower-level API that is part of the Java Content Repository (JCR) specification. It allows you to listen for changes to nodes and properties in the repository. JCR Observation is very powerful and flexible, but it can also be more complex to use and can have performance implications if not used carefully.

Example :  You might use JCR Observation if you need to listen for changes to a specific type of node or property in the repository, regardless of how or why the change was made. For example, you might use JCR Observation to track changes to user profiles or to implement a custom versioning system.

Sling Event Listener:

Sling Event Listener is a higher-level API that is part of the Apache Sling framework, which AEM is built on. Sling Event Listener is designed to listen for events that are specific to the Sling framework, such as resource events (added, changed, removed) and OSGi events (services registered, modified, unregistered). Sling Event Listener is generally easier to use and more performant than JCR Observation, especially for high-level events.

 

example :  you might use Sling Event Listener to clear a cache when a page is published or to send a notification when a new user is registered.

 

It is better to Prefer Sling Event Listener over JCR Observation wherever it is possible.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

It is the API Preference - AEM Rule of thumb or hierarchy.

The general rule is to prefer the APIs/abstractions the following order:

 

  1. AEM
  2. Sling
  3. JCR
  4. OSGi

https://www.linkedin.com/pulse/aem-apis-application-programming-interfaces-suresh-dhulipudi-za2if/?t...

 

 

Both AEM Sling Event Listener and JCR Observation are used to listen for and react to events in the AEM system, but they operate at different levels.

JCR Observation

JCR Observation is a lower-level API that is part of the Java Content Repository (JCR) specification. It allows you to listen for changes to nodes and properties in the repository. JCR Observation is very powerful and flexible, but it can also be more complex to use and can have performance implications if not used carefully.

Example :  You might use JCR Observation if you need to listen for changes to a specific type of node or property in the repository, regardless of how or why the change was made. For example, you might use JCR Observation to track changes to user profiles or to implement a custom versioning system.

Sling Event Listener:

Sling Event Listener is a higher-level API that is part of the Apache Sling framework, which AEM is built on. Sling Event Listener is designed to listen for events that are specific to the Sling framework, such as resource events (added, changed, removed) and OSGi events (services registered, modified, unregistered). Sling Event Listener is generally easier to use and more performant than JCR Observation, especially for high-level events.

 

example :  you might use Sling Event Listener to clear a cache when a page is published or to send a notification when a new user is registered.

 

It is better to Prefer Sling Event Listener over JCR Observation wherever it is possible.

Avatar

Employee Advisor

I don't think that you can compare those.

 

  • JCR Observation events are sent when something on the repository changes. 
  • OSGI events is a generic pub-sub mechanism to deliver events within the JVM.
  • In the past JCR Observation events were translated also in Sling Resource Events, which were sent via the OSGI event subsystem; some time ago this approach has been changed because it was not scalable at all. 
  • Instead of that the Sling ResourceChangeListener approach has been introduced, which somehow reflects the possibilities of the JCR Observation on a Sling level.

That means, if you can only compare JCR Observations with the ResourceChangeListener approach.

 

JCR observation:

  • allows you to do what's defined in the JCR specification
  • quite performant, events are cluster-aware, allows event filtering based on ACLs
  • sometimes a bit hard to use, as it's very lowlevel.

Sling ResourceChangeListener

  •  Highlevel API
  • Well suited for events, where you only need the to access the path of the changed resource, not the resource itself.