Highlighted

Set up PageEvent Handler/Listener

gregorio_meraz

07-02-2019

Hello AEM Community

Im currently working on setting up a event handler for page creations and deletions on aem to then call one of our vendors API.

Ive been basing my work on a module we already have that listens to replication events.

So far i was able to reproduce that behavior on my module and trigger the code upon replications. However, i only need the calls to the API on Page Publications and deletions.

Ive been trying to find how to diferentiate between replications and page deletions and activations.

So far, it seems that AEM handles crx replications and page publications as the same type of event "type= ACTIVATION".

If i delete a page, it does set the type as "DELETE" so i can work with that to call  the API but for the page publications im lost since as i mentioned, AEM looks like it handles CRX replications and pages publications as the same type.

After some research, i found the PageEvent API and I tried to set up a Page Event Listener but it is not getting triggered upon publications or deletions of pages, so im not sure that if what im trying to do its possible or maybe my componet is located on a wrong part of the project to listen for Page Events.

Thanks beforehand

Replies

Highlighted
Highlighted

gregorio_meraz

07-02-2019

Hello.

So, for example, if i go to crx and i replicate any of the files we already have being html, css or js, i get a replicate type=ACTIVATE event.

However, we only want to catch whenever a new page is created or deleted.

Not sure if that answers your question

Highlighted

gregorio_meraz

07-02-2019

yes, ill post what i have currently.

I dont see an option to edit the original post so ill post it here.

Component(

   configurationPolicy = ConfigurationPolicy.REQUIRE,
   immediate = true,
   service = EventHandler.class)

@Designate(ocd = APIConfiguration.class)

public class CustomEventListener implements EventHandler {

   private static final Logger LOGGER = LoggerFactory.getLogger(CustomEventListener.class);

  
  public void handleEvent(final Event event) {

   LOGGER.debug("handling event");
   PageEvent pgEvent = PageEvent.fromEvent(event);
   LOGGER.debug(pgEvent.toString());
   LOGGER.debug(pgEvent.toEvent().toString());


   }

}

on my config class i have:

@ObjectClassDefinition(

   name = "API Connection Details",
   description = "APIConnection Details")

public @interface APIConfiguration {

   @AttributeDefinition(

   name = EventConstants.EVENT_TOPIC,
   description = "Page Action Event"
   )

  String page_topics() default PageEvent.EVENT_TOPIC;
}

So whenever i either create a new page or delete it from AEM Dashboard the code doesnt get triggered.

It only works if add the replication topic to the config class.

Highlighted

Jörg_Hoh

Employee

07-02-2019

When you want to track creation or deletion of pages (and ignore all activation and deactivation stuff) on a single instance, you can use eventing for it.

Create a page in your AEM instance then check localhost:4502/system/console/events for the event describing the page creation.

Highlighted

gregorio_meraz

07-02-2019

ok, seems that the topic would be: org/apache/sling/api/resource/Resource/ADDED for page additions

And org/apache/sling/api/resource/Resource/REMOVED

Highlighted

gregorio_meraz

07-02-2019

I set both topics on the configuration file

topics config.png

but i still dont get the event triggered upon page addition or deletion even tho i can see the event with the topic being generated correctly on /system/console/events

Highlighted

Gaurav-Behl

MVP

08-02-2019

Debug the code to check if it get's registered and listens to the topics or not? If not, then possibly you're running into some API versions/library versions conflicts.

check if this helps - java - AEM 6.3 - Creating Event handler using OSGi R6 annotations - Stack Overflow

this one uses SCR annotations - Adobe CQ Help | Creating Replication Event Handlers for Adobe Experience Manager