AEM Developer Series
- Day 00: AEM Developer Series
- Day 01: Introduction to AEM
- Day 02: AEM Architecture
- Day 03: Setting up AEM Development Environment
- Day 04: Developing First OSGi Bundle
- Day 05: Working with Sling Servlets in AEM
- Day 06: Playing with Sling Post Servlet
- Day 07: Creating your first component in AEM
- Day 08: Dueling with JavaScript Use API
- Day 09: Dueling with Java User API
- Day 10: Getting to know Sling Models
- Day 11: Client Libraries in Action
- Day 12: Creating your custom OSGi Configuration
- Day 13: Schedulers in AEM
- Day 14: Eventing in AEM
- Day 15: Custom Workflows in AEM
- Day 16: Creating JMX Beans in AEM
- Day 17: Working with QueryBuilder API
- Day 18: Working with Granite Datasources in AEM
- Day 19: Replication API in Action
- Day 20: Working with Users and Groups in AEM
- Event Listener - JCR level events with the observation
- Event Handler - Sling level events
- Workflow and Launchers
- Schedulers with cron expressions
In this post, we will be discussing Event Handlers and Event Listeners. Eventing with Workflow and Launchers will be discussed in the next post while we have already discussed Schedulers in one of our previous posts. All the events in AEM can be viewed at - http://<host>:<port>/system/console/events.
Event Handlers
We can create an Event Handler by following below steps -
- Write a service class that implements the EventHandler interface.
- Register the service with property EventConstants.EVENT_TOPIC.
- Implement the handleEvent(Event event) method to trigger the job.
Let us create a custom event handler that will be triggered when a page is activated and logs the topic for which the handler was registered.
- Create a class CustomEventHandler and paste the following code in it. As you can see that we have service property service=EventHandler.class and EventConstants.EVENT_TOPIC registered to resource added and changed which normally happens in AEM Replication
- We have implemented the handleEvent() method and it is logging the topic for which this event handler is registered.
- Deploy the code and activate (publish) any page. You will see following traces in the logs
org.redquark.demo.core.listeners.CustomEventHandler Event is: org/apache/sling/api/resource/Resource/ADDED
org.redquark.demo.core.listeners.CustomEventHandler Event is: org/apache/sling/api/resource/Resource/CHANGED
Event Listeners
We can achieve event handling at the JCR level by implementing the EventListener interface in a class. JCR Observer is the lowest-level event handling in AEM. As its name indicates, it is at the JCR level and allows to listen to JCR-level events, gathered in sets (corresponding to persistence transactions). javax.jcr.observation.Event lists the following types:
- Event.NODE_ADDED
- Event.NODE_MOVED
- Event.NODE_REMOVED
- Event.PERSIST
- Event.PROPERTY_ADDED
- Event.PROPERTY_CHANGED
- Event.PROPERTY_REMOVED
Let us create an event listener that listens to the event when a new node is added at a specified path.
- Create a new class CustomEventListener and paste the following code in it
- First, we need to get the javax.jcr.Session object to add the event listener. Here we are using the concept of service/system user in AEM.
- After getting the session, we are adding the event listener and registering them for PROPERTY_ADDED and NODE_ADDED events on the path /apps/demoproject.
- In the implemented onEvent(EventIterator events), we are logging the paths where a new node is added
- Now go to the CRX Explorer and create a system user "demouser" and give all permissions on the root.
- Go to ./system/console/configMgr and search for Apache Sling Mapper User Mapper Service and configure as per the below screenshot
![]() |
User Mapper Service |
- Deploy the code and create new folder at location /apps/demoproject, you will see the following traces in the logs
org.redquark.demo.core.listeners.CustomEventListener Something has been added: /apps/demoproject/eventTest
org.redquark.demo.core.listeners.CustomEventListener Something has been added: /apps/demoproject/eventTest/jcr:primaryType
org.redquark.demo.core.listeners.CustomEventListener Something has been added: /apps/demoproject/eventTest/jcr:createdBy
org.redquark.demo.core.listeners.CustomEventListener Something has been added: /apps/demoproject/eventTest/jcr:created
Conclusion
Congratulations!! 🙋 we have successfully created event handlers and event listeners to understand JCR and Sling level events. I hope you enjoyed this post.
You can find the complete code of this project on my GitHub in this commit. Feel free to fork or open issues, if any.
I would love to hear your thoughts on this and would like to have suggestions from you to make it better.
Happy Coding 😃
hi @Anirudh,
ReplyDeleteWhat is the use of private SlingRepository repository; when it is not used in the code.