Skip to main content

Day 14: Eventing in AEM

AEM Developer Series

There are various ways by which event handling can be done 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:
  1. Event.NODE_ADDED
  2. Event.NODE_MOVED
  4. Event.PERSIST
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 


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. 

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😃


Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 05: Working with Sling Servlets in AEM

Day 03: Setting up AEM Development Environment