Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Event handler fails as soon as bundle reinstalled

Avatar

Level 4
Hi, I am using aem 6.5 I have created custom event handler class to run on page publish. It was working properly. But as soon as my bundle reinstalled for other deployments it stopped working. Again when I restarted class specific components from /system/console/components It started working. But I need to know how to keep it active throughout. Restarting component will be adhoc process. As this is very much important step.
1 Accepted Solution

Avatar

Correct answer by
Level 5

@kiranc13433869  - Check this

 

The following class for CustomEventHandler  sample -

 

package org.test.demo.core.listeners;

import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.resource.observation.ResourceChangeListener;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* Event Handler that listens to the Sling events
*/
@Component(immediate = true, service = EventHandler.class, property = {
Constants.SERVICE_DESCRIPTION + "= This event handler listens the events on page activation",
EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/ADDED",
EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/CHANGED",
EventConstants.EVENT_FILTER + "(&" + "(path=/content/we-retail/us/en/*/jcr:content) (|("
+ SlingConstants.PROPERTY_CHANGED_ATTRIBUTES + "=*jcr:title) " + "(" + ResourceChangeListener.CHANGES
+ "=*jcr:title)))" })
public class CustomEventHandler implements EventHandler {

/**
* Logger
*/
private static final Logger log = LoggerFactory.getLogger(CustomEventHandler.class);

@Override
public void handleEvent(Event event) {

log.info("Event is: {}", event.getTopic());
}

}

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
and 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.test.demo.core.listeners.CustomEventHandler Event is: org/apache/sling/api/resource/Resource/ADDED
org.test.demo.core.listeners.CustomEventHandler Event is: org/apache/sling/api/resource/Resource/CHANGED

View solution in original post

3 Replies

Avatar

Community Advisor

Do you see any errors while restarting the bundle for these specific components ?

If possible can you share the snippet of the code for event handler.

 

Avatar

Community Advisor

Hi @kiranc13433869 

 

Ideally, after the installation, the event handler is not getting satisfied and hence you are forced to do it manually. For example, if your handler is using any service and if its not active immediately (immediate = true) this might cause the issue.

 

Can you check if everything is in the satisfied state as per below sample image?

 

Kiran_Vedantam_0-1673971346925.png

 

Hope this helps!


Thanks,
Kiran Vedantam.

 

Avatar

Correct answer by
Level 5

@kiranc13433869  - Check this

 

The following class for CustomEventHandler  sample -

 

package org.test.demo.core.listeners;

import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.resource.observation.ResourceChangeListener;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* Event Handler that listens to the Sling events
*/
@Component(immediate = true, service = EventHandler.class, property = {
Constants.SERVICE_DESCRIPTION + "= This event handler listens the events on page activation",
EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/ADDED",
EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/CHANGED",
EventConstants.EVENT_FILTER + "(&" + "(path=/content/we-retail/us/en/*/jcr:content) (|("
+ SlingConstants.PROPERTY_CHANGED_ATTRIBUTES + "=*jcr:title) " + "(" + ResourceChangeListener.CHANGES
+ "=*jcr:title)))" })
public class CustomEventHandler implements EventHandler {

/**
* Logger
*/
private static final Logger log = LoggerFactory.getLogger(CustomEventHandler.class);

@Override
public void handleEvent(Event event) {

log.info("Event is: {}", event.getTopic());
}

}

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
and 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.test.demo.core.listeners.CustomEventHandler Event is: org/apache/sling/api/resource/Resource/ADDED
org.test.demo.core.listeners.CustomEventHandler Event is: org/apache/sling/api/resource/Resource/CHANGED