Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Event Listener Not Working Without Administrative Session

Avatar

Level 7

Hello Everyone,

I am writing an EventListener but the onEvent() method is not getting called if I create the session by doing below:

Map<String, Object> param = new HashMap<>()
param.put(ResourceResolverFactory.SUBSERVICE, "SUBSERVICENAME")
resourceResolver = resourceResolverFactory.getServiceResourceResolver(param)
session = resourceResolver.adaptTo(Session.class)

however if I am creating the session by "repository.loginAdministrative(null)" the class is working as expected and the onEvent() is getting called. The latest example in the ACS Commons github is also getting the session by "repository.loginAdministrative(null)".
https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/ad...

But it is not advisable to use repository.loginAdministrative(null) to obtain the session, not to mention its also deprecated. I was just wondering if anyone have faced this before and have some possible solution.

Thanks and Regards,

Tuhin

P.S: My service user have all the permission it needs to do any kind of operation on node.

1 Accepted Solution

Avatar

Correct answer by
Level 10

This looks like a bug. I would file a ticket to let the team know this is not working. LOoks like you are doing everything you are suppose to do.

View solution in original post

17 Replies

Avatar

Level 3

Did you configure the user id in Apache User Mapper Service ?

Also, debug the code and see if you are getting the resource resolver.

Avatar

Level 7

Hi Rajeev,

The user is all good. Working fine for other classes. I am also getting the resource resolver. There isn't any error in the logs too.

thanks

Avatar

Level 10

Did you try and put the System user in an admin group - did that work?

Avatar

Level 3

Did you check if your eventListener class is active when you are using System user to get session. Also, check error log if there is any error.
And if possible, use sling eventHandler instead of eventListener as mentioned at https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/j ava/com/adobe/acs/samples/events/impl/SampleJcrEventListener.java

Avatar

Employee Advisor

Hi,

is your user capable to read the content areas, where the user is supposed to have read access to, and all nodes up to the root node? I've never faced your problem, but I could imagine, that internally some traversal is done, which requires these additional permissions.

kind regards,
Jörg

Avatar

Level 7

Hi Scott,

I have tried the same. It didn't work.

Regards,

Tuhin

Avatar

Level 7

@Rajeev, I have tried everything, then only I have posted this question. The class is fine and active in OSGI, activate method working fine while activating the service. The URL you have mentioned is also mentioned in my question itself.

I know about Sling Event Handler, and that is an alternative. But why should a code is not working which is supposed to work that is my question.

Avatar

Level 7

Screen Shot 2017-06-16 at 8.48.23 am.png

Hi Jörg Hoh

I have added all permissions just to make sure that its not the permission issue specific to my user. But no luck.

Regards,

Tuhin

Avatar

Correct answer by
Level 10

This looks like a bug. I would file a ticket to let the team know this is not working. LOoks like you are doing everything you are suppose to do.

Avatar

Level 10

Also - what AEM version are you using?

Avatar

Level 7

Hi Scott,

Seems like we have hit a dead end, I am using 6.2.

Reagards,

Tuhin

Avatar

Level 5

Did you fix this, I am experiencing the same issue.

Avatar

Level 7

Hi,

Rather than using EventListener I started using the EventHandler which gives you the same functionality as event listener.

Thanks

Tuhin

Avatar

Level 1

Hi Tuhin,

I am facing the same problem as you, is this problem resolved with sling evenhandler. Can you let me know any sample url which is using this.

Avatar

Level 2

Hi,

I am also facing same issue. Do we have any fix for it?

Avatar

Level 1

Summer 2022 , AEM 6.5 SP9 and the same behavior.

Avatar

Level 4

Guys, I realized the problem was in resourceResolver property. In my case when the property was local in activator, it did not work.

When the resourceResolver property is global in class, then it works.

 

Following works:

 

@Activate
public void start(BundleContext context) throws Exception {
    resourceResolver = resolverFactory.getServiceResourceResolver(null);
    session = resourceResolver.adaptTo(Session.class);
    observationManager = session.getWorkspace().getObservationManager();
    listener = new Listener(session);
    observationManager.addEventListener(listener, ...

Following does not work:

@Activate
public void start(BundleContext context) throws Exception {
    ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(null);
    session = resourceResolver.adaptTo(Session.class);
    observationManager = session.getWorkspace().getObservationManager();
    listener = new Listener(session);
    observationManager.addEventListener(listener, ...

It is weird and does not make much sense to me, but making resourceResolver class variable instead of method local solves it.