Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Custom event not firing

Avatar

Level 5

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.

1 Accepted Solution

Avatar

Correct answer by
Level 10

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/ad...

 

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: 

View solution in original post

21 Replies

Avatar

Level 10

Can you elaborate more on this. It will help community to solve this...

I couldn't see errors in log file you shared.

Avatar

Level 10

What aem version are you using?

Avatar

Level 5

smacdonald2008 wrote...

What aem version are you using?

 


6.1

Avatar

Level 5

edubey wrote...

Can you elaborate more on this. It will help community to solve this...

I couldn't see errors in log file you shared.

 

 

I have a use for manipulating a property field on upload

was hoping to use this method instead of workflow route.
Simply just testing event article out before I make changes.

The article claims it's supposed to log a time/date when I create nt:unstructured or sling:folder

nodes under the /content/claim

Thanks.

Avatar

Level 10

Article is marked as 5.6. We will test on 6.1

Avatar

Level 10

Try modifying the package that the custom event handler is in. I changed to this package: com.community

I got it working with that package. I think the package com.adobe.cq was conflicting with other ones. I will test on 6 and 6.1. 

I will also update the article to reflect new package and testing on newer AEM versions. 

I just got this output:

15.01.2016 14:57:56.028 *INFO* [ObservationManager] com.community.SimpleDSComponent A new node was added to content/claim 
 

Avatar

Employee Advisor

Hi,

this should work with 6.1 as well; have you actually created such a node ("nt:unstructured" or "sling:Folder") below /content/claim?

Jörg

Avatar

Level 10

Also try using this code too - replace the code in your OSGi bundle with this code... . 

// 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);

Avatar

Level 5

smacdonald2008 wrote...

Also try using this code too - replace the code in your OSGi bundle with this code... . 

// 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);

 


Okay this worked. Thanks Scott!

Now my problem is this seems to only fire once. I set timeout exception as well.

I need it to constantly watch over this folder to change some properties in crdxe.

Is this the wrong use case?

Avatar

Level 5

Any help on this please with 6.1?

Thanks for all the support.

Avatar

Level 10

The code only fired once - the following nodes did not cause a log message. If this is true - I am wondering if there is an issue with AEM 6.1 and events - the code is the correct way. 

I am checking with internal Adobe ppl to see if anyone has seen this. 

Avatar

Level 10

THis was fixed - see the answer marked as correct. 

Avatar

Employee Advisor

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

Avatar

Level 10

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

Avatar

Correct answer by
Level 10

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/ad...

 

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: 

Avatar

Level 1

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

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

Avatar

Level 10

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

Avatar

Level 1

please suggest any solution for this.

Avatar

Community Advisor

Possible to share github repo for what you are trying ?

Avatar

Level 1

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.