Friday, May 2, 2014

CQ5/AEM How do I detect from a SlingServlet whether it is running on the Master or Slave instance on an author cluster?

The Problem

You have a Sling Servlet performing a task on the author instance, and you are running as a cluster. The code is executing on both the master author and the slave author instance, when it should only be running on one.

How do you detect whether the instance is the master instance?

The Solution

private SlingRepository repository;

public boolean isMasterRepository(){
    final String isMaster = repository.getDescriptor("crx.cluster.master");
    return StringUtils.isNotBlank(isMaster) && Boolean.parseBoolean(isMaster);

With this code, we can detect whether the current instance is running as the master.

In the execution function, the first conditional should be to detect whether it is running as the master instance or not, and if it is not, it should terminate the function call.

1 comment:

  1. If we are good and honest in our work we must get the real fan. Never do any mistakes or correction in our work. It may happen that is common for all. Good musician or a good writer at any good custom essay writing service or any job we need to put our 100%. If it is there we must get back the result.