Custom event not firing | Community
Skip to main content
TheBigRed
Level 4
Jan 15, 2016 5:13 PM UTC
Solved

Custom event not firing

As per the following AEM article. I created nodes as said. Also added the timeout exception.

https://helpx.adobe.com/experience-manager/using/events.html

Here is the error log:

 

15.01.2016 12:08:36.601 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle BundleEvent STOPPING 15.01.2016 12:08:36.635 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle Service [com.adobe.aem.events.SimpleDSComponent,4085, [java.lang.Runnable, javax.jcr.observation.EventListener]] ServiceEvent UNREGISTERING 15.01.2016 12:08:36.656 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle BundleEvent STOPPED 15.01.2016 12:08:36.663 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle BundleEvent UNRESOLVED 15.01.2016 12:08:36.664 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle BundleEvent UPDATED 15.01.2016 12:08:36.668 *INFO* [FelixDispatchQueue] org.apache.felix.framework FrameworkEvent PACKAGES REFRESHED 15.01.2016 12:08:36.670 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle BundleEvent RESOLVED 15.01.2016 12:08:36.671 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle BundleEvent STARTING 15.01.2016 12:08:36.672 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle BundleEvent STARTED 15.01.2016 12:08:36.713 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.eventhandler-bundle Service [com.adobe.aem.events.SimpleDSComponent,4092, [java.lang.Runnable, javax.jcr.observation.EventListener]] ServiceEvent REGISTERED 15.01.2016 12:08:36.757 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.granite.repository Service [4093, [org.apache.jackrabbit.oak.spi.commit.Observer]] ServiceEvent REGISTERED 15.01.2016 12:08:36.794 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.granite.repository Service [4094, [org.apache.jackrabbit.api.jmx.EventListenerMBean]] ServiceEvent REGISTERED 15.01.2016 12:08:36.829 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.granite.repository Service [4095, [org.apache.jackrabbit.oak.spi.commit.BackgroundObserverMBean]] ServiceEvent REGISTERED 15.01.2016 12:08:36.871 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.granite.repository Service [4096, [org.apache.jackrabbit.oak.plugins.observation.filter.FilterConfigMBean]] ServiceEvent REGISTERED 15.01.2016 12:08:36.909 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.granite.repository Service [4097, [java.lang.Runnable]] ServiceEvent REGISTERED 15.01.2016 12:08:36.914 *INFO* [Background Update com.adobe.aem.events.eventhandler-bundle (456)] com.adobe.aem.events.SimpleDSComponent Observing property changes to [nt:unstructured, sling:Folder] nodes under /content/dam/claim 15.01.2016 12:08:36.916 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle BundleEvent STOPPING 15.01.2016 12:08:36.948 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle Service [com.adobe.aem.events.SimpleDSComponent,4092, [java.lang.Runnable, javax.jcr.observation.EventListener]] ServiceEvent UNREGISTERING 15.01.2016 12:08:36.986 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4093, [org.apache.jackrabbit.oak.spi.commit.Observer]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.021 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4094, [org.apache.jackrabbit.api.jmx.EventListenerMBean]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.056 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4095, [org.apache.jackrabbit.oak.spi.commit.BackgroundObserverMBean]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.091 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4096, [org.apache.jackrabbit.oak.plugins.observation.filter.FilterConfigMBean]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.130 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4097, [java.lang.Runnable]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.141 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle BundleEvent STOPPED 15.01.2016 12:08:37.154 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle BundleEvent UNRESOLVED 15.01.2016 12:08:37.157 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle BundleEvent RESOLVED 15.01.2016 12:08:37.158 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle BundleEvent STARTING 15.01.2016 12:08:37.159 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle BundleEvent STARTED 15.01.2016 12:08:37.197 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.eventhandler-bundle Service [com.adobe.aem.events.SimpleDSComponent,4098, [java.lang.Runnable, javax.jcr.observation.EventListener]] ServiceEvent REGISTERED 15.01.2016 12:08:37.239 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4099, [org.apache.jackrabbit.oak.spi.commit.Observer]] ServiceEvent REGISTERED 15.01.2016 12:08:37.276 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4100, [org.apache.jackrabbit.api.jmx.EventListenerMBean]] ServiceEvent REGISTERED 15.01.2016 12:08:37.316 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4101, [org.apache.jackrabbit.oak.spi.commit.BackgroundObserverMBean]] ServiceEvent REGISTERED 15.01.2016 12:08:37.356 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4102, [org.apache.jackrabbit.oak.plugins.observation.filter.FilterConfigMBean]] ServiceEvent REGISTERED 15.01.2016 12:08:37.394 *INFO* [FelixFrameworkWiring] com.adobe.granite.repository Service [4103, [java.lang.Runnable]] ServiceEvent REGISTERED 15.01.2016 12:08:37.400 *INFO* [FelixFrameworkWiring] com.adobe.aem.events.SimpleDSComponent Observing property changes to [nt:unstructured, sling:Folder] nodes under /content/dam/claim 15.01.2016 12:08:37.403 *INFO* [FelixDispatchQueue] org.apache.felix.framework FrameworkEvent PACKAGES REFRESHED 15.01.2016 12:08:37.750 *INFO* [Apache Sling Resource Resolver Finalizer Thread] com.adobe.granite.repository Service [4099, [org.apache.jackrabbit.oak.spi.commit.Observer]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.785 *INFO* [Apache Sling Resource Resolver Finalizer Thread] com.adobe.granite.repository Service [4100, [org.apache.jackrabbit.api.jmx.EventListenerMBean]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.823 *INFO* [Apache Sling Resource Resolver Finalizer Thread] com.adobe.granite.repository Service [4101, [org.apache.jackrabbit.oak.spi.commit.BackgroundObserverMBean]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.860 *INFO* [Apache Sling Resource Resolver Finalizer Thread] com.adobe.granite.repository Service [4102, [org.apache.jackrabbit.oak.plugins.observation.filter.FilterConfigMBean]] ServiceEvent UNREGISTERING 15.01.2016 12:08:37.902 *INFO* [Apache Sling Resource Resolver Finalizer Thread] com.adobe.granite.repository Service [4103, [java.lang.Runnable]] ServiceEvent UNREGISTERING

 

 

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Jan 18, 2016 9:59 PM UTC

Hi BigRed - i did more testing. 

Try this code - this will work. I went back to dependency injection to get session and it now works. Taken from this example: https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/adobe/acs/samples/events/impl/SampleJcrEventListener.java

 

I also see the event as Joerg points out.

@Component(metatype=true)
@Service
public class SimpleDSComponent implements Runnable,EventListener {
    
    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    private BundleContext bundleContext;
    
    
    @Reference
    private SlingRepository repository;
    
  //Inject a Sling ResourceResolverFactory
    @Reference
    private ResourceResolverFactory resolverFactory;
    
    private Session session;
    
    private ObservationManager observationManager;
    
  //Inject a Sling ResourceResolverFactory to create a Session requited by the EventHandler
 
    
    public void run() {
        log.info("Running...");
    }
    
    
    //Place app logic here to define the AEM Custom Event Handler
    protected void activate(ComponentContext ctx) {
        this.bundleContext = ctx.getBundleContext();
        
        try
        {
                   
            //Invoke the adaptTo method to create a Session 
            // ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
             session = repository.loginAdministrative(null);
             
            // Setup the event handler to respond to a new claim under content/claim.... 
                observationManager = session.getWorkspace().getObservationManager();
                 final String[] types = { "nt:unstructured","sling:Folder" };
                 final String path = "/content/claim"; // 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)
        {
            e.printStackTrace(); 
         }
        }

         protected void deactivate(ComponentContext componentContext) throws RepositoryException {
             
             if(observationManager != null) {
                 observationManager.removeEventListener(this);
             }
             if (session != null) {
                 session.logout();
                 session = null;
               }
         }

        //Define app logic that is fired when the event occurs - simply track the time 
        //when the event occurred. 
         public void onEvent(EventIterator itr) {
             
             //Calendar cal = Calendar.getInstance();
            //cal.getTime();
            //SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
             
            //log the time when the event occurred 
             log.info("A new node was added to content/claim ");
                         
         }
}

See http://localhost:4502/system/console/events: 

21 replies

New Participant
May 7, 2018 12:54 PM UTC

try session = repository.loginService(String serviceName,String Workspace);

its working.

configure the Service to created system user

Translate
New Participant
May 2, 2018 5:27 AM UTC

please see the java logic there https://helpx.adobe.com/experience-manager/using/events.html,

They are getting session by using admin,then its working fine.but when I'm using system user, Onevent method is  not firing.

Translate
viveksachdeva
New Participant
May 2, 2018 5:21 AM UTC

Possible to share github repo for what you are trying ?

Translate
New Participant
May 2, 2018 5:09 AM UTC

please suggest any solution for this.

Translate
smacdonald2008
Level 10
Apr 30, 2018 1:19 PM UTC

We tried setting up a system user - but for some reason the event does not fire. Seems to only work with the admin call.

Translate
New Participant
Apr 30, 2018 1:06 PM UTC

how can we avoid session = repository.loginAdministrative(null);

I want to get session not by admin access, I need session from  created user

Translate
smacdonald2008
smacdonald2008Accepted solution
Level 10
Jan 18, 2016 9:59 PM UTC

Hi BigRed - i did more testing. 

Try this code - this will work. I went back to dependency injection to get session and it now works. Taken from this example: https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/adobe/acs/samples/events/impl/SampleJcrEventListener.java

 

I also see the event as Joerg points out.

@Component(metatype=true)
@Service
public class SimpleDSComponent implements Runnable,EventListener {
    
    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    private BundleContext bundleContext;
    
    
    @Reference
    private SlingRepository repository;
    
  //Inject a Sling ResourceResolverFactory
    @Reference
    private ResourceResolverFactory resolverFactory;
    
    private Session session;
    
    private ObservationManager observationManager;
    
  //Inject a Sling ResourceResolverFactory to create a Session requited by the EventHandler
 
    
    public void run() {
        log.info("Running...");
    }
    
    
    //Place app logic here to define the AEM Custom Event Handler
    protected void activate(ComponentContext ctx) {
        this.bundleContext = ctx.getBundleContext();
        
        try
        {
                   
            //Invoke the adaptTo method to create a Session 
            // ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
             session = repository.loginAdministrative(null);
             
            // Setup the event handler to respond to a new claim under content/claim.... 
                observationManager = session.getWorkspace().getObservationManager();
                 final String[] types = { "nt:unstructured","sling:Folder" };
                 final String path = "/content/claim"; // 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)
        {
            e.printStackTrace(); 
         }
        }

         protected void deactivate(ComponentContext componentContext) throws RepositoryException {
             
             if(observationManager != null) {
                 observationManager.removeEventListener(this);
             }
             if (session != null) {
                 session.logout();
                 session = null;
               }
         }

        //Define app logic that is fired when the event occurs - simply track the time 
        //when the event occurred. 
         public void onEvent(EventIterator itr) {
             
             //Calendar cal = Calendar.getInstance();
            //cal.getTime();
            //SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
             
            //log the time when the event occurred 
             log.info("A new node was added to content/claim ");
                         
         }
}

See http://localhost:4502/system/console/events: 

Translate
smacdonald2008
Level 10
Jan 18, 2016 9:18 PM UTC

that is a good point Joerg - we will update JCR Event article to mention this. 

Translate
joerghoh
Employee
Jan 18, 2016 9:00 PM UTC

Hi,

I am not aware of any restriction with AEM 6.x (or better: Apache Oak), which matches your description. We are using JCR observation in quite a number of usecases, and we don't face the problem you are describing.

A way to control your tests would be to observe the events page in the Felix webconsole (/system/console/events); for each change in the repository there should be a resource added/removed/changed/... event. When you do your tests and modify nodes in the repository, you should be able to see your changes there as well. For your case I would just recommend it as way to control your tests. Of course you can create a Sling event listener instead of the JCR event listener, but that's a different story then...

kind regards,
Jörg

Translate
smacdonald2008
Level 10
Jan 18, 2016 8:46 PM UTC

THis was fixed - see the answer marked as correct. 

Translate