Expand my Community achievements bar.

EventListener event never triggered

Avatar

Level 5

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

17 Replies

Avatar

Level 10

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

Avatar

Level 10

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

Avatar

Level 8

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.

}

Avatar

Level 5

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)

Avatar

Level 10

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

Avatar

Employee Advisor

Is your code picking up the correct service user?

Avatar

Level 5

It is, we use the same method all over our code-base to modify all kind of node structures. smacdonald2008 seems to have the same issue.

Avatar

Level 10

I am going to test deeper. It does seem to be a permission issue at some level. That is why i whitelisted the bundle - thereby giving permission for the code to use the admin call.

Avatar

Level 5

I've read your article by now, is this bug being looked into, so I can use a serviceResourceResolver instead of the admin one?

Avatar

Level 10

I triple checked this morning - while this code compiles -

Map<String, Object> param = new HashMap<String, Object>();

param.put(ResourceResolverFactory.SUBSERVICE, "eventwrite");

ResourceResolver resolver = null;

try {

//Invoke the adaptTo method to create a Session used to create a QueryManager

resolver = resolverFactory.getServiceResourceResolver(param);

adminSession = resolver.adaptTo(Session.class);

adminSession.getWorkspace().getObservationManager().addEventListener(

Its not logging events. The only way it currently works is to use the admin call and whitelist the bundle.

I give system user all permissions and even tried adding them to the admin group. Still did not work.

Avatar

Level 5

Any idea if this will be fixed in a hotfix or something?

Avatar

Level 10

You can log a ticket - its possible Eng is not aware of this. To log a ticket - see Peter Nash thread in the pinned threads at the start of the community.

Avatar

Level 1

I have the same issue too. Please let me know if there is a fix for this or alternate approach (without Admin credentials).

Appreciate your help!

Avatar

Level 10

As we stated - there does not appear to be a way - i tested a lot using a System user. THe only way that worked was to WhiteList the bundle. You can log a bug to bring this issue to the Attention of AEM Eng team.

Avatar

Level 5

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.