By pass event handler from a service impl | Community
Skip to main content
Level 3
March 14, 2024
Solved

By pass event handler from a service impl

  • March 14, 2024
  • 6 replies
  • 1168 views

Hi Team,

 

I have implemented a event handler which executes on replication of page . Now I want event handler to be excluded for one specific use case while i am publishing the page.

 

How to implement this?

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

Let's say your event hander has is replicating a node. You have added a custom property named "excludeEventHandler". Now if you have this property that exists, do not replicate, and run custom code.

 

Example Code:

 

public void handleEvent(Event event) { String path = (String) event.getProperty("path"); try (ResourceResolver resourceResolver = resolver) { Resource resource = resourceResolver.getResource(path); if (resource != null) { Node node = resource.adaptTo(Node.class); if (node != null && !node.hasProperty("excludeEventHandler")) { // Your custom logic here System.out.println("Handling event for: " + path); } } } catch (Exception e) { e.printStackTrace(); } }

 

 

6 replies

anupampat
Community Advisor
Community Advisor
March 14, 2024

Hi @sateeshkreddy ,

 

You should be able to achieve it with simple if else condition. Although there could be specific cases where you may not achieve it. 

Can you share the use case as well ?

 

Ps- Since the use case is related to the TOPIC (publishing) it would be wise to simply use if else.

Imran Khan
Community Advisor
Community Advisor
March 14, 2024

@sateeshkreddy Inside event handler apply path check to exclude it.

 

With JCR events it is not possible. Either you swicth to Sling events or check path while act on event.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/in-event-handler-how-to-exclude-the-specific-folder-in-path/m-p/273872

 

Level 7
March 14, 2024

Hi @sateeshkreddy 

 

If you want to execute only for few page paths, try to use this logic.

 

import org.osgi.service.event.EventHandler; @Component(service = EventHandler.class, immediate = true, property = { EventConstants.EVENT_FILTER + "=(|(paths=/content/abc/articles/*)(paths=/content/xyz/books/*))", EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC, }) public class CustomReplicationEventHandler implements EventHandler {

 

So that your CustomReplicationEventHandler is called only for pages, which are part of EventConstants.EVENT_FILTER 

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
March 14, 2024

Let's say your event hander has is replicating a node. You have added a custom property named "excludeEventHandler". Now if you have this property that exists, do not replicate, and run custom code.

 

Example Code:

 

public void handleEvent(Event event) { String path = (String) event.getProperty("path"); try (ResourceResolver resourceResolver = resolver) { Resource resource = resourceResolver.getResource(path); if (resource != null) { Node node = resource.adaptTo(Node.class); if (node != null && !node.hasProperty("excludeEventHandler")) { // Your custom logic here System.out.println("Handling event for: " + path); } } } catch (Exception e) { e.printStackTrace(); } }

 

 

joerghoh
Adobe Employee
Adobe Employee
March 18, 2024

The OSGI event which is sent by the replication cannot be customized in a way, that you can provide a flag to the replication call, which then shows up in the event properties. Never heard of such a requirement before.

 

That means you need to place the logic in the event handler.

kautuk_sahni
Community Manager
Community Manager
March 20, 2024

@sateeshkreddy 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