How to Create a Cluster Aware OSGi Service

Problem

How do I create an OSGi service that is aware of whether it is running on a CQ5/CRX cluster slave node or a master node?

Resolution

In CQ5, you can use the ClusterAware interface which will automatically handle periodically checking the descriptor for you.  This makes things easier if the node is elected as master:

import com.day.cq.jcrclustersupport.ClusterAware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype = false, policy = ConfigurationPolicy.IGNORE)
@Service(ClusterAware.class)
public class MyClusterAwareService implements ClusterAware {
  public void unbindRepository() {
    log.info("No Repository is bound or Repository is unbound");
  }

  public void bindRepository(String repositoryId, String clusterId, boolean isMaster) {
    log.info("Bound to Repository {} Node {} (Cluster: {})",
      new Object[] { (isMaster ? "Master" : "Slave"), repositoryId, clusterId });
    doSomethiing(isMaster);
  }

  private void doSomething(boolean isMaster) {
    //TODO: your code here
  }
}

Note: the ClusterAware feature only applies to CQ5.3 with Feature Pack 34071 installed, CQ5.4 or later version.

Applies to

CQ 5.3

CQ 5.4

CRX 2.1

CRX 2.2

** If you want to use this in CQ5.3 you need 5.3 Feature Pack 34071 and CRX hotfix 2.1.0.10+.  If you don't have these hotfixes then you can request them via daycare.

Note:

AEM 5.6.1 onward use new topology api. Details at http://helpx.adobe.com/cq/kb/create-cluster-aware-osgi-service.html

 Adobe

Get help faster and easier

New user?

Adobe MAX 2024

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX 2024

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online