Expand my Community achievements bar.

SOLVED

By pass event handler from a service impl

Avatar

Level 1

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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();
        }
    }

 

 

View solution in original post

6 Replies

Avatar

Level 4

Hi @user54123 ,

 

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.

Avatar

Level 10

@user54123 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-ex...

 

Avatar

Level 8

Hi @user54123 

 

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 

Avatar

Correct answer by
Community Advisor

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();
        }
    }

 

 

Avatar

Employee Advisor

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.

Avatar

Administrator

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