Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

[AEM Gems Webinar] Accelerating Experience Manager as a Cloud Service development with Rapid Development Environments
SOLVED

AEM 6.5: Events won't be fired

Avatar

Level 6

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
1 Reply

Avatar

Level 4

Hi @Magicr ,

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

Regards,

Shiv

Avatar

Correct answer by
Community Advisor

Avatar

Level 6

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.