April 27, 2020
Estimated Post Reading Time ~

How to execute the Quartz scheduler job only in master author node

While we are deploying the scheduler in AEM, the scheduler will be active in master/slave author nodes and all the publish nodes(based on our deployment configuration).

But sometimes there will be the scenario the scheduled job should be only executed in the master author node.

The below code snippet can be used to restrict the job getting executed only in the master author node.

@Reference
SlingRepository repository;
private void sampleScheduledJob() {
       if(isRunMode("author") && isMasterRepository()){
            //execute the job functionality here
       }
}

private Boolean isRunMode(String mode) {
        Set<String> runModes = slingSettings.getRunModes();
        for (String runMode : runModes) {
                if (runMode.equalsIgnoreCase(mode)) {
                         log.debug("Current Runmode is : " + runMode);
                         return true;
                 }
        }
        return false;
}
               
public boolean isMasterRepository(){
          final String isMaster = repository.getDescriptor("crx.cluster.master");
          log.debug("isMaster.."+isMaster);
          return StringUtils.isNotBlank(isMaster) && Boolean.parseBoolean(isMaster);
}


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.