Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Mentioning Multiple paths in EventHandler class

Avatar

Level 8

Hello All,

 

I have CustomEventHandler class which implements  EventHandler interface.

handleEvent method is called during page activation, deactivation, deletion time. Here, I need to mention the page path. I mean if a page is present under /content/abc/project/en then only my custom event handler should trigger. I need to mention multiple page path.   /content/abc/project/en,   /content/xyz/project/en

Any help in this?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

here you go

https://github.com/arunpatidar02/aem63app-repo/blob/24aaa3d42fb06f5f43df94a0acefb13bc08807bc/java/Re... 

 

|(paths=/content/abc/myArticle/*)(paths=/content/abc/newArticle/*)

 



Arun Patidar

View solution in original post

10 Replies

Avatar

Community Advisor

@Mahesh_Gunaje 

 

Preferably use ResourceChangeListener

 

It will also allow you to use multiple paths

https://techrevel.blog/2017/03/15/resourcechangelistener/


Aanchal Sikka

Avatar

Level 8

Hi @aanchal-sikka 

ResourceChangeListener logic sounds interesting. But in my case, if I use implements EventHandler, then I can write my logic for 

if ReplicationActionType.ACTIVATE :  call add customMethod();

if ReplicationActionType.DEACTIVATE :  call customDelete Method

if ReplicationActionType.DELETE:  call customDelete Method

So, How can I call my method if i implement ResourceChangeListener.

I can see only one method.

Avatar

Community Advisor

Hello @Mahesh_Gunaje 

 

Please refer to this blog https://aemhints.com/2020/11/08/event-listener-in-aem-6-5/

 

It uses Event Handler with multiple Paths.


Aanchal Sikka

Avatar

Level 8

Hi @aanchal-sikka 

I have tried the logic mentioned in that link. Still, handleEvent() method is called for other page Path as well.

I have found the solution here:  https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-event-filter-not-worki...

In this, @dishi_k  mentioned about 

EventConstants.EVENT_FILTER + "=(paths=/var/...somepath/*)"

This was working for me. So, now my class looks like:

@Component(service = EventHandler.class,
immediate = true,
property = {
EventConstants.EVENT_FILTER + "=(paths=/content/abc/myArticle/*)",
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC,
})
public class customEventHandler implements EventHandler {

}

So that if I publish any page under other path: /content/abc/newArticles/*  my customEventHandler  class is not at all called.

Now, my next task is to 1: mention regular expression instead of /content/abc/myArticle/* Or else 2: need to pass the path from OSGI config.

Avatar

Level 8

Hi @arunpatidar 

Checked your code. Still have confusion.

As of now, I have used: EventConstants.EVENT_FILTER + "=(paths=/content/abc/myArticle/*)"

 

Now, how to mention 2 paths here:

/content/abc/myArticle/*
/content/abc/newArticle/*

 

Since, your code has:  EventConstants.EVENT_FILTER + "(&" + "(path=/content/we-retail/us/en/*/jcr:content) (|("
+ SlingConstants.PROPERTY_CHANGED_ATTRIBUTES + "=*jcr:title) " + "(" + ResourceChangeListener.CHANGES
+ "=*jcr:title)))" })

Avatar

Correct answer by
Community Advisor

here you go

https://github.com/arunpatidar02/aem63app-repo/blob/24aaa3d42fb06f5f43df94a0acefb13bc08807bc/java/Re... 

 

|(paths=/content/abc/myArticle/*)(paths=/content/abc/newArticle/*)

 



Arun Patidar

Avatar

Level 8

Hi @arunpatidar 

Works like a charm. Thanks a lot Arun

 

@Component(service = EventHandler.class,
immediate = true,
property = {
EventConstants.EVENT_FILTER + "=(|(paths=/content/abc/myArticle/*)(paths=/content/abc/newArticle/en/*))",
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC,
})
public class customEventHandler implements EventHandler { ... }