Event handler triggered twice in AEM as Cloud Service or Not getting Triggered at all. | Community
Skip to main content
Level 3
February 27, 2024

Event handler triggered twice in AEM as Cloud Service or Not getting Triggered at all.

  • February 27, 2024
  • 7 replies
  • 5538 views

HI Team,

We have a requirement :
when Page got activated or deactivated  we need to send the page info( path, title ,uuid) to other systems.
We are writing a event handler for it. It is working as expected. But, Event handler is getting Triggered twice. Below is the code.


@Slf4j
@8220494( service = EventHandler.class, immediate = true,
property = { EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC, })
public class ReplicationEventHandler implements EventHandler {

If we subscribe to org/apache/sling/event/notification/job/FINISHED or org/apache/sling/distribution/agent/package/distributed
Then Event handler is not getting triggered at all. Below is the code.

@Slf4j
@8220494(immediate = true, service = EventHandler.class,

property = { "event.topics=org/apache/sling/event/notification/job/FINISHED",
"event.filter=(|(distribution.type=ADD)(distribution.type=DELETE))" })
public class ReplicationEventHandler implements EventHandler {

OR
@Slf4j @8220494(immediate = true, service = EventHandler.class, property = { "event.topics=org/apache/sling/distribution/agent/package/distributed",
"event.filter=(|(distribution.type=ADD)(distribution.type=DELETE))" })
public class ReplicationEventHandler implements EventHandler {


Could you please review the sample code and provide guidance on resolving this issue?

7 replies

SureshDhulipudi
Community Advisor
Community Advisor
February 27, 2024

Event Handler triggering twice generally happens with multiple events trigger when a page is activated (like one ReplicationAction event and/or ResourceEvent for any change in resources etc.)

To handle this - write a condition to handle your ReplicationAction event only

@Slf4j
@component( service = EventHandler.class, immediate = true,
property = { EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC, })
public class ReplicationEventHandler implements EventHandler {

ReplicationAction action = ReplicationAction.fromEvent(event);
if (action != null && (action.getType() == ReplicationAction.Type.ACTIVATE || action.getType() == ReplicationAction.Type.DEACTIVATE)) {
// Handle the event
}
 
==== Event handler is not getting triggered=====
 
Job Finished event or distributed event - These events are not part of the Page Activate or DeActivate actions - these events are related to Sling - please check more details on Sling Job System / Sling Distribution System.
Level 3
February 27, 2024

Hi Suresh, Thanks for the response.
I wrote the same kind of code. Still it is triggered twice. Because in Cloud it is cluster and it will have minimum 2 clusters at any time. This issue occurs only in cloud but not in local. 

ReplicationAction action =ReplicationAction.fromEvent(event);
ReplicationActionType actionType =action.getType();
if (actionType != ReplicationActionType.DELETE && actionType != ReplicationActionType.ACTIVATE
&& actionType != ReplicationActionType.DEACTIVATE) {
log.error("The action type {} is not allowed for the page {}.", actionType.getName(), actionPath);
return;
}

Harwinder-singh
Community Advisor
Community Advisor
February 28, 2024

@rudra-2024  For the case where the event handler is not getting triggered, could it be due to the fact that your event handler execution takes more than 5000 ms and hence causing it become blacklisted. Once that happens, your event handler is no longer functional.  We ran into this type of scenario recently. 

If that is the case, you might want to look at your implementation and see if there is anything that is potentially causing this.

You can read more on this in a similar discussion thread - 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/lt-critical-gt-event-handler-not-triggering-while-delete-page/m-p/443092

 

kautuk_sahni
Community Manager
Community Manager
February 28, 2024

@rudra-2024 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
Level 3
February 29, 2024

Issue is not resolved yet.

arunpatidar
Community Advisor
Community Advisor
February 28, 2024
Level 3
February 29, 2024

Hi @arunpatidar 

Thanks for the response.

I pretty much followed the same as below article.
https://sudeshaempodcast.wordpress.com/2021/07/04/aemaacs-slingjobs/

Only problem is when the topic is EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC, It is getting triggered twice.



when the topic is"event.topics=org/apache/sling/distribution/agent/package/distributed", or 
 "event.topics=org/apache/sling/event/notification/job/FINISHED
is not triggered at all.

arunpatidar
Community Advisor
Community Advisor
March 1, 2024

HI @rudra-2024 
Are you doing this on publish or author?

Can you check the number of events at http://localhost:4502/system/console/events 

Arun Patidar
aanchal-sikka
Community Advisor
Community Advisor
March 1, 2024

@rudra-2024 

 

Please verify is Sling Jobs would suit your need. They are better suited to work on AEMaaCS Clusters.

https://techrevel.blog/2023/11/06/enhancing-efficiency-and-reliability-by-sling-jobs/

Aanchal Sikka
Level 3
March 1, 2024

Hi @aanchal-sikka 

Thank you for your response. In my scenario, it's crucial to react to both page activation and deactivation events. While Sling jobs might not directly address this requirement, I've devised the following approach:

  1. Implement an event handler to capture page activation/deactivation events.
  2. Within the event handler, incorporate logic to trigger a job.
  3. Utilize a Sling job to execute the necessary business logic.

However, if you believe that Sling jobs will directly listen page activation and deactivation events, I would greatly appreciate any reference links or sample code you could provide.




Level 3
March 1, 2024

HI @joerghoh,Good Evening.

Any insights or leads regarding this matter?

Thanks in advance.

Level 3
March 6, 2024

@sureshdhulipudi @arunpatidar @aanchal-sikka @harwinder-singh 

I tried 

@Component(immediate = true, service = EventHandler.class, property = {EVENT_TOPIC + "=" + AGENT_PACKAGE_DISTRIBUTED,
"event.filter=(|(distribution.type=ADD)(distribution.type=DELETE))"})
public class ContentDistributionEventHandler implements EventHandler {

 


I tried below options. Still it is printing the logs twice.

@Component(immediate = true, service = EventHandler.class, property = {EVENT_TOPIC + "=" + AGENT_PACKAGE_DISTRIBUTED,
"event.filter=(|(distribution.type=ADD)(distribution.type=DELETE))"})
public class ContentDistributionEventHandler implements EventHandler {

 

 

@Slf4j
@Component(service = ResourceChangeListener.class, immediate = true, property = {
ResourceChangeListener.PATHS + "=" + GlobalConstants.ONTENT_ROOT_PATH,
ResourceChangeListener.CHANGES + "=" + "ADDED",
ResourceChangeListener.CHANGES + "=" + "CHANGED",
ResourceChangeListener.PROPERTY_NAMES_HINT + "=" + "cq:lastReplicated"
})
@ServiceDescription("TEST REPLICATION PROPERTY")
public class ReplicationChangeListener implements ResourceChangeListener {

 

 Any suggestions/leads on this?

 

 

SureshDhulipudi
Community Advisor
Community Advisor
March 6, 2024

can you keep a condition for resourceType (I,e if the replication action happened only on Page) something like that?

or filter the resourceType or filter based on the page path.

duplicate events might be with any of the node properties also getting replicated??

Level 3
March 12, 2024

Hi Suresh,

 

Thanks for the response.

 

I tried the same. still it is getting triggered twice.

Level 2
March 18, 2024

@rudra-2024 
Hello. I have faced the same issue both of EventListener and EventHandler.
You can try one of the following options mentioned below.

1) Add this in your OSGi filter !(event.application=*). This makes sure, that the event is emitted only once, on clustered authors.
2) Use org.apache.sling.discovery.DiscoveryService#getTopology().getLocalInstance().isLeader() - this will make sure that the event processing will be handled only on master/leader pod.

P.S Sample OSGi Filter - (&(|(distribution.type=ADD)(distribution.type=DELETE))(!(event.application=*)))

Hope this helps.

Level 3
March 18, 2024

Hi @martin_knyazyan 

One Quick Question

Which topic you got subscribed with this filter

Is it org/apache/sling/event/notification/job/FINISHED   or

org/apache/sling/distribution/agent/package/distributed

0r

com/day/cq/replication
Level 2
July 11, 2025

Hi

 

org/apache/sling/distribution/agent/package/distributed is only triggered in publishers. Publisher is different from Author. Each publisher is a single instance with a dispatcher, not a pod in a cluster. So each publisher is a leader.

 

Flo Tang from Leadstec


Hi @fulongt37334138 .

Thanks for your response.
org/apache/sling/distribution/agent/package/distributed  - events to this topic are posted on author (see https://sling.apache.org/documentation/bundles/content-distribution.html#events).

Publishers are also pods on cluster, but along with those pods you have Golden Publisher which:

 

  • Not connected to LB
  • It maintains a “golden copy” of the segment store
  • New publish pod just clones it