Expand my Community achievements bar.

SOLVED

Event Listener is not firing

Avatar

Level 2
Level 2

Hi All,

i have written simple listener which will be triggered when a node is added under "/content" of type sling:Folder.Please help me to fix this issue.

 

package com.sample.eventhandler;

import java.util.Arrays;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype = false, immediate = true, label = "Event heading End Date Chnange Listner",
description="This component will listen to change in event end date node")
@Service(value={EventListener.class})
public class SimpleDSComponent implements /*Runnable*/EventListener {
    private Logger log = LoggerFactory.getLogger(SimpleDSComponent.class);
    private BundleContext bundleCtxt=null;
    @Reference
    private ResourceResolverFactory resolverFactory;
    @Reference
    private SlingRepository repository;
    private Session session;
    private ObservationManager obsMgnr;
    
    @Activate
    protected void activate(ComponentContext ctxt)
    {
        log.info("##In activate() ##");
        this.bundleCtxt=ctxt.getBundleContext();
        try {
            ResourceResolver resolver = resolverFactory.getAdministrativeResourceResolver(null);
            Session sessObj = resolver.adaptTo(Session.class);
            log.info("sessObj is live?"+sessObj.isLive());
            obsMgnr = sessObj.getWorkspace().getObservationManager();
            String[] types={"sling:Folder"};
            String path="/content";
            obsMgnr.addEventListener(
                    this,
                    Event.NODE_ADDED,
                    //Event.NODE_REMOVED,
                    path,
                    true,
                    null,
                    null,
                    false);
            log.info("Observing property changes to {} nodes under {}", Arrays.asList(types), path);
            
        } catch (LoginException le) {
            // TODO Auto-generated catch block
            //log.info(le.getMessage());
            le.printStackTrace();
        } catch(RepositoryException re){
            re.printStackTrace();
            
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    /*@Deactivate
     * protected void deactivate(ComponentContext cCtxt)
    {
        log.info("##In deactivate() ##");
        if(obsMgnr !=null)
        {
            try {
                obsMgnr.removeEventListener(this);
            } catch (RepositoryException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(session !=null)
        {
            session.logout();
            session=null;
        }
    }*/
    public void onEvent(EventIterator arg0) {
        // TODO Auto-generated method stub
        log.info("## In onEvent() ##");
        long eventsSize = arg0.getSize();
        log.info("eventsSize :"+eventsSize);
        Node nodeObj = null;
        while(arg0.hasNext())
        {
            try {
                session = repository.loginAdministrative(null);
                Event event = arg0.nextEvent();
                String eventFiredPath = event.getPath();
                log.info("eventFiredPath :"+eventFiredPath);
                nodeObj = session.getNode(eventFiredPath);
                log.info("nodeObj is locked ?"+nodeObj.isLocked());
            } catch (RepositoryException e) {
                e.printStackTrace();
            }
            
        }
        
    }

    /*public void run() {
        // TODO Auto-generated method stub
        log.info("## In run() ##");
    }
*/
    
}

Thaknks

 

In Advance

1 Accepted Solution

Avatar

Correct answer by
Level 5

I think the issue is with the session that you are getting via ResourceResolverFactory.

Try Below :

           

//  ResourceResolver resolver = resolverFactory.getAdministrativeResourceResolver(null); //  Session sessObj = resolver.adaptTo(Session.class); session = repository.loginAdministrative(null); log.info("sessObj is live?"+session.isLive()); obsMgnr = session.getWorkspace().getObservationManager();

View solution in original post

8 Replies

Avatar

Level 10

Hi,

I see Event.NODE_ADDED is commented !!

Avatar

Level 2
Level 2

bsloki wrote...

Hi,

I see Event.NODE_ADDED is commented !!

 

i tried for bith Node addition and deletion both the events are not firing.

Anything i need to add to Listener Class?

Avatar

Level 10

Replace this 

obsMgnr.addEventListener(
                    this,
                    Event.NODE_ADDED,
                    //Event.NODE_REMOVED,
                    path,
                    true,
                    null,
                    null,
                    false);

with

obsMgnr.addEventListener(
                    this,
                    Event.NODE_ADDED,
                    //Event.NODE_REMOVED,
                    path,
                    true,
                    null,
                    types,
                    false);

Basically, you have missed on what type of nodes these event should be triggered. its currently you are passing it as 'null' and I think that would be the reason it is not firing on any action

Avatar

Correct answer by
Level 5

I think the issue is with the session that you are getting via ResourceResolverFactory.

Try Below :

           

//  ResourceResolver resolver = resolverFactory.getAdministrativeResourceResolver(null); //  Session sessObj = resolver.adaptTo(Session.class); session = repository.loginAdministrative(null); log.info("sessObj is live?"+session.isLive()); obsMgnr = session.getWorkspace().getObservationManager();

Avatar

Level 2
Level 2

vdhim23 wrote...

I think the issue is with the session that you are getting via ResourceResolverFactory.

Try Below :

           

  1. // ResourceResolver resolver = resolverFactory.getAdministrativeResourceResolver(null);
  2. // Session sessObj = resolver.adaptTo(Session.class);
  3. session = repository.loginAdministrative(null);
  4. log.info("sessObj is live?"+session.isLive());
  5. obsMgnr = session.getWorkspace().getObservationManager();

 

 

Now seems to be issue with getting session.Now events are getting fired.

i have another question, what is the difference between getting session object from resourceResolverFactory and  getting from SlingRepository object?

ResourceResolver resObj = resolverFactory.getAdministrativeResourceResolver(null);

Session sessObj = resObj.adaptTo(Session.class);

session = repository.loginAdministrative(null); [repository is an object of type SlingRepositry object]

Thanks,

Kishore

Avatar

Level 5

GK@AEM wrote...

vdhim23 wrote...

I think the issue is with the session that you are getting via ResourceResolverFactory.

Try Below :

           

  1. // ResourceResolver resolver = resolverFactory.getAdministrativeResourceResolver(null);
  2. // Session sessObj = resolver.adaptTo(Session.class);
  3. session = repository.loginAdministrative(null);
  4. log.info("sessObj is live?"+session.isLive());
  5. obsMgnr = session.getWorkspace().getObservationManager();

 

 

Now seems to be issue with getting session.Now events are getting fired.

i have another question, what is the difference between getting session object from resourceResolverFactory and  getting from SlingRepository object?

ResourceResolver resObj = resolverFactory.getAdministrativeResourceResolver(null);

Session sessObj = resObj.adaptTo(Session.class);

session = repository.loginAdministrative(null); [repository is an object of type SlingRepositry object]

Thanks,

Kishore

 

 

What i understand as per the docs 

resolverFactory.getAdministrativeResourceResolver(null)

Returns a new ResourceResolver instance with administrative privileges with further configuration taken from the given authenticationInfo map.

repository.loginAdministrative(null);

Returns a session to the default workspace which has administrative powers.

So ResourceResolver with administrative privileges provide an access over resources that you resolve via ResourceResolver, In that case you can get admin access over resource that get restricted via ACL.

Avatar

Employee Advisor

Hi,

There should not be any difference between these 2 approaches. Both return a new admin session.

kind regards,
Jörg

Avatar

Level 5

Jörg Hoh wrote...

Hi,

There should not be any difference between these 2 approaches. Both return a new admin session.

kind regards,
Jörg

 

Thanks Jorg, But how can listener not fire in case of ResourceResolverFactory, but if we get the session via SlingRepository it works?