Expand my Community achievements bar.

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 1

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

its working.

configure the Service to created system user