EventHandler - restrict for Preview server | Community
Skip to main content
Level 2
May 12, 2023
Solved

EventHandler - restrict for Preview server

  • May 12, 2023
  • 5 replies
  • 1783 views

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.

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 VinayHegde

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.

5 replies

AsifChowdhury
Community Advisor
Community Advisor
May 12, 2023

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-client-side-js/m-p/326528/highlight/true#M23563

amarguptAuthor
Level 2
May 12, 2023

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

B_Sravan
Community Advisor
Community Advisor
May 12, 2023

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

Rohit_Utreja
Community Advisor
Community Advisor
May 12, 2023

@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/adobe/acs/commons/dam/impl/ReviewTaskAssetMoverHandler.java

 

I hope it helps!

joerghoh
Adobe Employee
Adobe Employee
May 13, 2023

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

VinayHegdeAccepted solution
Level 2
May 25, 2023

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.