Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Clarification - Eventlistener, EventHandler, and ResourceChangeListener

Avatar

Level 2

I'm a beginner in AEM development, and I'm learning about event listening and handlers. I was stuck with some doubt and I searched on the internet but that didn't get more clarity so I am writing here.

  • I know Eventlistener from javax.jcr.observation.EventListener is used to capture the events from only JCR level (correct me if I'm wrong, please )
  • I'm not understanding what to use and when to use in  org.osgi.service.event.EventHandler and org.apache.sling.api.resource.observation.ResourceChangeListener or can I use both (why we have two then?)
  •  And, I saw in some blogs saying that OSGi EventHandler is deprecated so is jcr.obeservation.EventListener is also deprecated?

Correct me if I'm wrong. Thank you

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

hello @shamyaswanth 

 

1. javax.jcr.observation.EventListener:
This interface is part of the Java Content Repository (JCR) API. It allows you to listen to events at the JCR level, which means you can capture events related to changes in the content repository (e.g., node creation, modification, deletion). It's a low-level API specific to JCR. ResourceChangeListener is the recommended one instead. Details added below.

 

2. org.osgi.service.event.EventHandler:
This is part of the OSGi (Open Service Gateway initiative) framework, which is the underlying modular framework used in AEM. The EventHandler interface allows you to listen to events at a higher level within the OSGi framework. It uses the OSGi Event Admin service to receive events. You can use it to capture custom events or events from other parts of the system. It's more versatile and flexible than the JCR-level EventListener.

 

3. org.apache.sling.api.resource.observation.ResourceChangeListener:
This interface is specific to Apache Sling, which is a web framework built on top of the OSGi framework and is used in AEM. The ResourceChangeListener is used to listen for changes in Sling resources (content nodes in AEM). It's designed to work with Sling resources and provides a higher-level abstraction for handling resource-related events.

 

If you want to listen for changes to Sling resources (content nodes), you can use org.apache.sling.api.resource.observation.ResourceChangeListener. This is the preferred way for AEM-specific event handling.

 

  • Benefits of ResourceChangeListener:
    • Avoid long-lived sessions.
    • Avoid Oak observation queue size issues.

 

Reference link: https://techrevelhub.wordpress.com/2017/03/15/resourcechangelistener/


Aanchal Sikka

View solution in original post

3 Replies

Avatar

Community Advisor

Hello @shamyaswanth 

 

hope this below article help your queries. It's very well written, please give it a read

https://medium.com/@toimrank/aem-handler-and-listener-12b6c8b5a3d3

Hope it helps!

Avatar

Level 2

I already read this article and I didn't get that much clarity with it

Avatar

Correct answer by
Community Advisor

hello @shamyaswanth 

 

1. javax.jcr.observation.EventListener:
This interface is part of the Java Content Repository (JCR) API. It allows you to listen to events at the JCR level, which means you can capture events related to changes in the content repository (e.g., node creation, modification, deletion). It's a low-level API specific to JCR. ResourceChangeListener is the recommended one instead. Details added below.

 

2. org.osgi.service.event.EventHandler:
This is part of the OSGi (Open Service Gateway initiative) framework, which is the underlying modular framework used in AEM. The EventHandler interface allows you to listen to events at a higher level within the OSGi framework. It uses the OSGi Event Admin service to receive events. You can use it to capture custom events or events from other parts of the system. It's more versatile and flexible than the JCR-level EventListener.

 

3. org.apache.sling.api.resource.observation.ResourceChangeListener:
This interface is specific to Apache Sling, which is a web framework built on top of the OSGi framework and is used in AEM. The ResourceChangeListener is used to listen for changes in Sling resources (content nodes in AEM). It's designed to work with Sling resources and provides a higher-level abstraction for handling resource-related events.

 

If you want to listen for changes to Sling resources (content nodes), you can use org.apache.sling.api.resource.observation.ResourceChangeListener. This is the preferred way for AEM-specific event handling.

 

  • Benefits of ResourceChangeListener:
    • Avoid long-lived sessions.
    • Avoid Oak observation queue size issues.

 

Reference link: https://techrevelhub.wordpress.com/2017/03/15/resourcechangelistener/


Aanchal Sikka