Create a cluster-aware OSGi service


How do I create an OSGi service that is aware of whether it is running on a AEM 5.6.1 cluster as leader node or not?


In AEM 5.6.1 and later, use a new topology API. For details, see

In earlier versions of AEM, identify a master in a Cluster environment using either Repository descriptor "crx.cluster.master" or ClusterAware interface.


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component(metatype = true)
@Service(value = { TopologyEventListener.class })

public class MyClusterAwareService implements Runnable,TopologyEventListener {

    private final Logger log = LoggerFactory.getLogger(MyClusterAwareService.class);

   private Boolean isLeaderInstance = Boolean.FALSE;

     * @see
    public void handleTopologyEvent(final TopologyEvent event) {
        if ( event.getType() == TopologyEvent.Type.TOPOLOGY_CHANGED
                || event.getType() == TopologyEvent.Type.TOPOLOGY_INIT) {
            this.isLeaderInstance = event.getNewView().getLocalInstance().isLeader();
  "isLeader confirmed @@@@@@@@@@@@@@@@@@@"+ this.isLeaderInstance);

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
    public void run() {
        if (isLeaderInstance) {
            //TODO: your code here
  "isLeader run @@@@@@@@@@@@@@@@@@@"+ this.isLeaderInstance);



Download example package



Gaukite pagalbą greičiau ir lengviau

Naujas vartotojas?