Event handler fails as soon as bundle reinstalled | Community
Skip to main content
Level 4
January 17, 2023
Solved

Event handler fails as soon as bundle reinstalled

  • January 17, 2023
  • 3 replies
  • 1274 views
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.
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by manjunathdj

@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

3 replies

Mani_kumar_
Community Advisor
Community Advisor
January 17, 2023

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.

 

Kiran_Vedantam
Community Advisor
Community Advisor
January 17, 2023

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?

 

 

Hope this helps!


Thanks,
Kiran Vedantam.

 

manjunathdj
manjunathdjAccepted solution
January 18, 2023

@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