Expand my Community achievements bar.

Implementing EventHandler leads to inconsistent behaviour

Avatar

Level 4

Hi, I would like to ask if anyone encountered similar issues regarding to EventsHandler, particularly Page Event Topic. We've added this on top of the EventHandler that we created:

@Component(service = EventHandler.class, immediate = true, configurationPolicy = ConfigurationPolicy.OPTIONAL, property = {
"event.topics=" + PageEvent.EVENT_TOPIC })

and added 

@Override
public void handleEvent(Event event) {}


The issue here is that, the override handleEvent didn't seem to run even if we trigger a page modification or page creation event. What else we could check for this? Thanks!

3 Replies

Avatar

Community Advisor

Hi @LyonMartin 

can you try with below topics for page(jcr:content) type

EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/ADDED",
EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/CHANGED",

 

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/TestEventHandler.java 



Arun Patidar

Avatar

Community Advisor

Hi @LyonMartin ,

 

I would suggest to consider using org.apache.sling.api.resource.observation.ResourceChangeListener.

 

Example: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/resourcechangelistener/td-...

 

Best regards,

Kostiantyn Diachenko.

Avatar

Community Advisor

Hi @LyonMartin ,

This is a common issue when working with AEM EventHandlers and listening to Page Events via PageEvent.EVENT_TOPIC. Here’s a breakdown of why your handleEvent() may not be triggered and how to fix it.


Root Causes & Solutions

1. PageEvent.EVENT_TOPIC Might Not Be Triggered as Expected

You're listening to:

"event.topics=" + PageEvent.EVENT_TOPIC

which resolves to:

com/day/cq/wcm/core/page

However, AEM doesn’t always trigger this topic for all page modifications/creations. It’s often used for specific programmatic page events, not low-level JCR events.

What You Can Check or Do:

A. Ensure Event Admin is Working

Confirm in /system/console/bundles that Apache Sling Event and EventAdmin bundles are active.
Check logs (error.log) for any activation issues with your handler.

 

B. Add Logging in handleEvent() for Confirmation

Add logs to see if it’s triggered at all:

@Override
public void handleEvent(Event event) {
    System.out.println("Event received: " + event.getTopic());
}

C. Alternative Approach: Use Sling Resource Events

Try listening to Sling resource-level events which are more reliable for content changes.

@Component(service = EventHandler.class, immediate = true, property = {
    EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/ADDED",
    EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/CHANGED"
})
public class ResourceEventHandler implements EventHandler {
    @Override
    public void handleEvent(Event event) {
        System.out.println("Resource Event: " + event.getTopic());
    }
}

D. Preferred Solution: Use ResourceChangeListener

Adobe recommends this modern approach:

@Component(service = ResourceChangeListener.class, immediate = true, property = {
        ResourceChangeListener.PATHS + "=/content",
        ResourceChangeListener.CHANGES + "=ADDED",
        ResourceChangeListener.CHANGES + "=CHANGED",
        ResourceChangeListener.CHANGES + "=REMOVED"
})
public class PageChangeListener implements ResourceChangeListener {
    @Override
    public void onChange(List<ResourceChange> changes) {
        for (ResourceChange change : changes) {
            System.out.println("Change detected: " + change.getPath());
        }
    }
}

 
Regards,
Amit Vishwakarma