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?
Views
Replies
Total Likes
Hi @BenBr9
Here is the example
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ReplicationListener.java
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
Views
Replies
Total Likes
Hi @BenBr9 ,
You may go through Preprocessor which has following advantages:-
Thanks
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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)"
Views
Replies
Total Likes
So I looked at the examples and used this topic:
@component(immediate = true, service = EventHandler.class,
property = {
EventConstants.EVENT_TOPIC + "=com/day/cq/replication"
})
ReplicationAction action = ReplicationAction.fromEvent(event);
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?
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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
Views
Replies
Total Likes
Views
Likes
Replies