Event handler
Event handler is responsible for Sling level events and Event listener is responsible for JCR level events.
Both handler and lister is responsible for executing an event.
Event Handler
Event handler can be created by implementing EventHandler interface which belongs to org.osgi.service.event package and override handleEvent(Event event) method.
Below is the syntax to define an EventHandler class:
import org.osgi.service.co mponent.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;@Component(service=EventHandler.class, immediate = true)
public class PracticeEventHandler implements EventHandler { @Override
public void handleEvent(Event event) { }
}
Example
Below are the steps to create an event handler:
- Create a class implementing EventHandler interface and override handleEvent(Event event) method.
2. Build and Deploy code package on AEM instance:
mvn clean install -PautoInstallPackage -DskipTests=true
3. Create page under content hierarchy and check logs in error.log file.
Event Topic = org/apache/sling/api/resource/Resource/ADDED, path = /content/practice/us/en/assas
Event Listener
Event listener can be created by implementing EventListener interface which belongs to javax.jcr.observation package.
Below is the syntax to define an EventListener class:
activate(ComponentContext context) will bind an event with the resources and get call on addition of property(PROPERTY_ADDED) and node(NODE_ADDED).
deactivate(ComponentContext context) will unbind the event listener we bound in activate method.
onEvent(EventIterator eventIterator) will listen an event related to jcr resource change.
Build and Deploy code package on AEM instance:
mvn clean install -PautoInstallPackage -DskipTests=true
OUTPUT
Add practice component on the page and check error.log will provide below entry.
ResourceChangeListener
This is recommend to use ResourceChangeListener to listen event related to resource change.
Resource change listener will get call at the time of addition, update and removal of resource and its property..
It will require an implementation of ResourceChangeListener interface and override onChange() method.
Build and Deploy code package on AEM instance:
mvn clean install -PautoInstallPackage -DskipTests=true
OUTPUT
Add practice component on the page and check error.log will provide below entry.
Event Type = ADDED
Event getPath = /content/practice/us/en/test/jcr:content/body/practice_741578400
Event isExternal = false
Event Type = CHANGED
Event getPath = /content/practice/us/en/test/jcr:content
Event isExternal = false
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.