Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

EventHandler - restrict for Preview server

Avatar

Level 2

We've a eventHandler that does some task on activate/deactivate. Now we've a preview server as well in place and want to understand on how to restrict tasks if destination server is preview server instead of publish server.

1 Accepted Solution

Avatar

Correct answer by
Level 2

AEM preview servers repliaction is  nothing more than replication to custom agents.
Inside the handleEvent function of EventHandler use,

event.getProperty("agentIds")


It will return you array which has Publish as an entry if the replication event is trigegrd for publish and preview if the page is just previewd.

View solution in original post

6 Replies

Avatar

Community Advisor

Hello @amargupt 

You can check the runmode before triggering the event.

You can get runmode in js in this way then you can decide what to do with the event handler. Is your runmode is satisfied to trigger the event or not.

var SlingSettingsService = Packages.org.apache.sling.settings.SlingSettingsService;

use(function () {
    // Get runmodes and transform them into an object that is easier to read for Sightly
    var runmodesObj = {};
    var runmodesSet = sling.getService(SlingSettingsService).getRunModes();
    var iterator = runmodesSet.iterator();
    var isauthor=sling.getService(SlingSettingsService).getRunModes().contains("author");
    var ispublish=sling.getService(SlingSettingsService).getRunModes().contains("publish");
    while (iterator.hasNext()) {
        runmodesObj[iterator.next()] = true;
    }
 
    return {
        runmodes: runmodesObj,
        author: isauthor,
        publish: ispublish
    }
});

 Refence: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/get-runmodes-value-in-clie...

Avatar

Level 2

My eventHandler will be executed in author server. Is there a way to find the destination server based on "Event".

Avatar

Community Advisor

Hi @amargupt ,

you can set run mode configuration through an OSGi configurations and check for the server before triggering the handler. I would suggest the server name as an attribute to your handler triggers.

@AsifChowdhury  SlignSettingsService is deprecated and no longer to be used under best practices clause. The alternative for that is yet to be decided and can be followed here : https://github.com/Adobe-Consulting-Services/acs-aem-commons/issues/2476 

Regards,

Sravan

Avatar

Community Advisor

@amargupt 

You can create one OSGI config and add 'policy = ConfigurationPolicy.REQUIRE' in the code.

After that, add OSGI configurations in all required AEM instances. 

The event handler will be active only in those AEM instances where it will be able to find out osgi configs.
Since you only need to restrict it in the preview instances, you can skip these OSGI configs there.

 

ACS commons has a similar code, please find the link for your reference.

https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad...

 

I hope it helps!

Avatar

Employee Advisor

I don't think that this is possible right now. Can you raise a feature request with Adobe support for that?

Avatar

Correct answer by
Level 2

AEM preview servers repliaction is  nothing more than replication to custom agents.
Inside the handleEvent function of EventHandler use,

event.getProperty("agentIds")


It will return you array which has Publish as an entry if the replication event is trigegrd for publish and preview if the page is just previewd.