Expand my Community achievements bar.

I want to create an event listener on publish page event

Avatar

Level 3

Hi

 

I want to write an AEM bundle that listens to page publish events.

I got this boilerplate code from chat GPT but I'm not sure what should be the event topic

 

@component(immediate = true, service = EventHandler.class,
        property = {
            "event.topics=" + PageEvent.TOPIC,
            "event.filter=(eventType=pagePublished)"
        })
public class PagePublishEventListener implements EventHandler {

 

I tried various topics and couldn't get it working.

The only way I could get my listener to be invoked is by listening to all events

  property = {
                "event.topics=*"
        })

Which I don't want.

 

How can I listen only to page publish events?

8 Replies

Avatar

Level 8

Hi @BenBr9,

I think you need to change your code to something like:

 

@Component(service = EventHandler.class, immediate = true, property = {
        EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC })
public class MyReplicationEventHandler implements EventHandler {
    public void handleEvent(final Event event) {
        ReplicationAction action = ReplicationAction.fromEvent(event);
        if (StringUtils.startsWith(action.getPath(), "/content/my-project")) {
            //TODO: your logic here
        }
    }
}

 

 

Good luck,

Daniel

Avatar

Community Advisor

Hi @BenBr9 ,

You may go through Preprocessor which has following advantages:-

  • this is part of AEM replication api, and has dedicated entry point which run just before replication is triggered always
  • do not need to create any additional replication agents or configurations
  • as this is part of replication flow, it is guarantee it will be run, unlike event listener you do not have to worry if you have picked up appropriate event topic, or if the event will be blacklisted etc
  • it gives you information about replication action type (e.g. Activation, Deactivation etc), path and others
  • finally it allows you to stop replication process

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/override-replication-behav...

Thanks

Avatar

Level 8

Hi @MukeshYadav_,

the EventHandler approach works also with AEMaaCS and Sling Distribution, I am not sure if this is the case with Preprocessor. Can you confirm?

 

All the best,

Daniel

Avatar

Level 2

The typical topic for page-related events in AEM is PageEvent.TOPIC, which represents page events like publish, unpublish, and modification. However, the event.filter configuration is essential to limit the listener to publish events only.

 

The PageEvent class in AEM provides constants for various event types:

PageEvent.EVENT_TOPIC – The main topic for page events.

PageEvent.EVENT_PAGE_PUBLISHED – Constant specifically for publish events.

 

The topic configuration should use PageEvent.EVENT_TOPIC, and the filter should specify PageEvent.EVENT_PAGE_PUBLISHED.

 

If PageEvent.EVENT_PAGE_PUBLISHED doesn’t work, try setting it explicitly:

"event.filter=(eventType=publish)"

Avatar

Level 3

So I looked at the examples and used this topic:

 

@component(immediate = true, service = EventHandler.class,
        property = {
                EventConstants.EVENT_TOPIC + "=com/day/cq/replication"
        })

 

This seems to work well. Thanks for all the help!
 
But when I try to do:

 

        ReplicationAction action = ReplicationAction.fromEvent(event);

 

 
I need to import

 

import com.day.cq.replication.ReplicationAction;

 

And for that I added to pom:

 

 <dependency>
        <groupId>com.day</groupId>
        <artifactId>cq-api</artifactId>
        <version>5.7.0</version>  <!-- Use the version that matches your AEM SDK -->
        <scope>provided</scope>
    </dependency>

And even:
<repositories>
    <repository>
        <id>aem-public</id>
        <url>https://repo.adobe.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

 

 

But it cannot find the dependency. Any help with that?

Avatar

Level 8

Hi @BenBr9,

there is no need to add any additinal dependencies, the package com.day.cq.replication is part of the SDK on AEMaaCS. Should be similar for the on-prem AEM.

 

Good luck,

Daniel

Avatar

Level 3

That's interesting, but the bundle won't compile.

 

[ERROR] /Users/benbracha/workspace/evinced/scratch-pad/aem-accessiblity-scanner/core/src/main/java/com/evinced/aem/core/PagePublishEventListener.java:[42,9] cannot find symbol
[ERROR] symbol: class ReplicationAction