AEM 6.5: Events won't be fired | Community
Skip to main content
Magicr
Level 6
July 12, 2022
Solved

AEM 6.5: Events won't be fired

  • July 12, 2022
  • 2 replies
  • 1272 views

Hello,

I'm stuck at following situation:
I have a page and I want add some events to it. The content structure is

 

 

/content /site /mypage /jcr:content

 

 

The node 'mypage' has the primary type 'cq:Page' and node 'jcr:content' the type 'cq:PageContent'.

To add events I created a service:

 

 

import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.jcr.api.SlingRepository; import org.apache.sling.settings.SlingSettingsService; import org.osgi.service.component.annotations.*; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.observation.Event; import javax.jcr.observation.EventIterator; import javax.jcr.observation.EventListener; import java.util.*; @Component( service = MyServiceListener.class, immediate= true) public class MyServiceListener implements EventListener { private SlingSettingsService settings; private ResourceResolverFactory resourceResolverFactory; private SlingRepository repository; private ResourceResolver resolver; private Session session = null; protected void activate() { try { Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "aserviceuser"); ResourceResolver resolver = null; resolver = resourceResolverFactory.getServiceResourceResolver(param); session = resolver.adaptTo(Session.class); session.getWorkspace().getObservationManager().addEventListener(new TestOnEvent(), Event.PROPERTY_CHANGED | Event.PROPERTY_ADDED | Event.PROPERTY_REMOVED, "/content/site/mypage/jcr:content", false, null, null, false); } catch (Exception e) { ; } } protected void deactivate() throws RepositoryException { if (session != null) { session.logout(); session = null; } } }

 

 

Whenever the bundle is (re)started the above code works fine. So I added code for actions for defined events:

 

 

public void onEvent(EventIterator events) { try { Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "aserviceuser"); ResourceResolver resourceResolver = null; resourceResolver = resourceResolverFactory.getServiceResourceResolver(param); session = resourceResolver.adaptTo(Session.class); while (events.hasNext()) { ... } } catch (Exception e) { ; } }

 

 

My Problem is, this piece of code won't always be executed. I could see his behaviour by adding output to logfiles and break points for the debug mode.
When some bundles are renewed then existing a good chance that above code will be executed. After that he won't be executed anymore.

My question: Where is my mistake?

 

Thanks in advanced

 

PS: The service user got all nessecary rights to read the content path.

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

2 replies

Shiv_Prakash_Patel
Community Advisor
Community Advisor
July 12, 2022

Hi @magicr ,

Are you also getting the same behavior when using this instead of new TestOnEvent() while adding EventListnener?

Regards,

Shiv

Shiv Prakash
Magicr
MagicrAuthor
Level 6
July 12, 2022

Yes. 😞

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
July 12, 2022
Magicr
MagicrAuthor
Level 6
July 13, 2022

Today, I read your provided links. Due to lack of time I checked and tried first one. This example is same as mine. I added in method "deactivated()" the code for removing eventlistener. Though the behavior is still the same.

Via debug mode I found if the target page recently created, the event will be fired. After that no other events will be fired, no matter wich action I did.

By the wy: When I debuged into 

session.getWorkspace().getObservationManager().getRegisteredEventListeners()

I found follow message "java.lang.Exception: Event listener is already here:" I checked my instance and my code, there are no multiple assignments of event handler.