Expand my Community achievements bar.

Event Handler not triggered in AEM 6.3

Avatar

Level 4

Hi all,

I have a requirement where i have to trigger the event handler to read all the images in a particular dam folder when ever an asset is added. the dam folder structure can have multiple levels where we add assets inside each folder. I used JCR EventListener which is working fine but not working for EventHandler. I'm using OSGI DS annotations in Archetype 11. Please find the code below. What am i missing? It's not getting triggered at all.

package uk.co.my.project.my_project.core.impl;

import org.apache.sling.api.SlingConstants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.osgi.service.event.Event;

import org.osgi.service.event.EventConstants;

import org.osgi.service.event.EventHandler;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import uk.co.my_project.core.service.ProductDAMService;

@Component(

    immediate = true,

    service = EventHandler.class,

    property = {

        EventConstants.EVENT_FILTER + "=(path=/content/dam/my_project/*)",

        EventConstants.EVENT_TOPIC + "=" + SlingConstants.TOPIC_RESOURCE_ADDED,

        EventConstants.EVENT_TOPIC + "=" + SlingConstants.TOPIC_RESOURCE_CHANGED,

        EventConstants.EVENT_TOPIC + "=" + SlingConstants.TOPIC_RESOURCE_REMOVED

    },

    configurationPid = "uk.co.my.project.my_project.core.impl.ProductDAMEventHandler"

)

public class ProductDAMEventHandler implements EventHandler {

private Logger log = LoggerFactory.getLogger(this.getClass());

@Reference

private ProductDAMService productDAMService;

@Override

public void handleEvent(Event event) {

String topic = event.getTopic();

Object path = event.getProperty(SlingConstants.PROPERTY_PATH);

log.info("Inside the event handler for DAM:"+topic+":::path:"+path);

boolean associateDAMAndPoducts = productDAMService.associateDAMAndPoducts();

log.info("assets associated:" + associateDAMAndPoducts);

}

}

I've taken this as reference

aem-osgi-annotation-demo/SampleOsgiResourceListener.java at master · nateyolles/aem-osgi-annotation-...

4 Replies

Avatar

Administrator

smacdonald2008​ you may consider writing article on this.

We had till 5.6



Kautuk Sahni

Avatar

Level 1

Have you tried something like that:

        @Property(name = EventConstants.EVENT_FILTER, value = "(path=/content/dam/my_project/*))"),

        @Property(name = EventConstants.EVENT_TOPIC, value = {SlingConstants.TOPIC_RESOURCE_REMOVED,

                SlingConstants.TOPIC_RESOURCE_ADDED, SlingConstants.TOPIC_RESOURCE_CHANGED})

Avatar

Level 3

@vitis90, I tried your code and it's working fine; at least the EventHandler part. Here's the code I used and I did get the expected log messages when I added/modified content under the expected path. Maybe you are listening to one path and making changes to another. Here's also the dependency for org.osgi.service.component.annotations.Component and the output I got.

Dependency

    <dependency>

        <groupId>org.osgi</groupId>

        <artifactId>org.osgi.service.component.annotations</artifactId>

        <version>1.3.0</version>

        <scope>provided</scope>

      </dependency>

Code

package com.nnp.forums63.listenevent;

import org.apache.sling.api.SlingConstants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.event.Event;

import org.osgi.service.event.EventConstants;

import org.osgi.service.event.EventHandler;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

@Component(

    immediate = true,

    service = EventHandler.class,

    property = {

        EventConstants.EVENT_FILTER + "=(path=/content/dam/trainingproject/*)",

        EventConstants.EVENT_TOPIC + "=" + SlingConstants.TOPIC_RESOURCE_ADDED,

        EventConstants.EVENT_TOPIC + "=" + SlingConstants.TOPIC_RESOURCE_CHANGED,

        EventConstants.EVENT_TOPIC + "=" + SlingConstants.TOPIC_RESOURCE_REMOVED

    }

)

public class DamFolderListener implements EventHandler {

  private Logger logger = LoggerFactory.getLogger(this.getClass());

@Override

  public void handleEvent(Event event) {

    logger.info("The Handler has FIRED!!!!!!!");

   

    String topic = event.getTopic();

    Object path = event.getProperty(SlingConstants.PROPERTY_PATH);

    logger.info("Inside the event handler for DAM:"+topic+":::path:"+path);

  }

}

logged output

20.02.2018 13:43:47.492 *INFO* [pool-363-thread-1] com.nnp.forums63.listenevent.DamFolderListener The Handler has FIRED!!!!!!!

20.02.2018 13:43:47.492 *INFO* [pool-363-thread-1] com.nnp.forums63.listenevent.DamFolderListener Inside the event handler for DAM:org/apache/sling/api/resource/Resource/CHANGED:::path:/content/dam/trainingproject/landscape.jpg/jcr:content