I want to create an event listener on publish page event | Community
Skip to main content
Level 2
November 10, 2024

I want to create an event listener on publish page event

  • November 10, 2024
  • 6 replies
  • 2539 views

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

 

@8220494(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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

6 replies

arunpatidar
Community Advisor
Community Advisor
November 10, 2024
daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 10, 2024

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

MukeshYadav_
Community Advisor
Community Advisor
November 11, 2024

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-behavior/m-p/597320

Thanks

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 11, 2024

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

GabrielMircea
Level 2
November 11, 2024

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)"
BenBr9Author
Level 2
November 11, 2024

So I looked at the examples and used this topic:

 

@8220494(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?

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 11, 2024

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

BenBr9Author
Level 2
November 11, 2024

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

kautuk_sahni
Community Manager
Community Manager
November 25, 2024

@benbr9 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni