EventListener event never triggered | Community
Skip to main content
Level 4
January 19, 2018

EventListener event never triggered

  • January 19, 2018
  • 4 replies
  • 5328 views

I am trying to hook to the rollout event of a page to a live copy. I am experimenting with the EvenListener but I am unable to get it to work. This is my code:

@Component(metatype = true)

@Service

public class MyCustomListener implements EventListener {

private static final Logger LOG = LoggerFactory.getLogger("logger");

@Reference

private ResourceResolverFactory resolverFactory;

private Session session;

private ObservationManager observationManager;

public void run() {

LOG.info("Running...");

}

//Place app logic here to define the AEM Custom Event Handler

protected void activate(ComponentContext ctx) {

try {

//Invoke the adaptTo method to create a Session

ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(AutoClosableResourceResolverFactory.getCredentials());

session = resourceResolver.adaptTo(Session.class);

observationManager = session.getWorkspace().getObservationManager();

final String[] types = {"cq:Page", "nt:unstructured"};

final String path = "/"; // define the path

observationManager.addEventListener(this, Event.NODE_ADDED, path, true, null, null, false);

LOG.info("Observing property changes to {} nodes under {}", Arrays.asList(types), path);

} catch (Exception e) {

LOG.error("unable to register session", e);

}

}

protected void deactivate(ComponentContext componentContext) throws RepositoryException {

if (observationManager != null) {

observationManager.removeEventListener(this);

}

if (session != null) {

session.logout();

session = null;

}

}

@Override

public void onEvent(EventIterator eventIterator) {

System.out.println();

}

}

My debug point in the onEvent method never gets called. Am I doing something wrong?

NOTES:

- my session or resource resolver are NOT null.

- AEM 6.3.1.0

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

smacdonald2008
Level 10
January 19, 2018

You are using Felix SRC annotations. Look at the SimpleResourceListener that uses DS annotations here - Creating an Adobe Experience Manager 6.3 Project using Adobe Maven Archetype 12

Level 4
January 22, 2018
Ratna_Kumar
Level 10
January 19, 2018

Hi,

We got this working as Scott mentioned above that we are using SimpleResourceListener class that uses DS annotations.

We will be releasing this article on early next week i.e., Monday or Tuesday!!

Watch this thread for more updates.

Thanks,

Ratna

smacdonald2008
Level 10
January 19, 2018
cquser1
Level 7
January 22, 2018

Hi,

In 6.3 we have an event listener working fine. Few thoughts on what we have done, in case it helps :

We have @Component, @Properties & @Service Annotations being used.

@Service

public class Sample implements ResourceChangeListener {

@Reference

private ResourceResolverFactory resourceResolverFactory;

protected ResourceResolver resourceResolver;

public void onChange(List<ResourceChange> changes) {

// Get the session object from the service user and the required logic.

}

Level 4
January 22, 2018

It works but only if I use the following code to retrieve a session:

observationSession = repository.loginAdministrative(null);

If I use the following my onEvent method is never called:

ResourceResolver serviceResourceResolver = resolverFactory.getServiceResourceResolver(AutoClosableResourceResolverFactory.getCredentials());

Any ideas? NOTE: we use the service user for our entire application (it has all rights)

smacdonald2008
Level 10
January 22, 2018

Seems to be a bug - i would whitelist the bundle and use that call. I see the same behaviour.

jkpanera
Level 4
July 31, 2019

I had a similar issue. It turns out in my case that the events were being fired and processed correctly but the Java debugger wasn't stopping inside the onEvent method. Unfortunately, I have had to use logging to troubleshoot rather than the debugger.