อัปเดตครั้งล่าสุดเมื่อ
21 ธ.ค. 2021
|
และยังใช้กับ CQ 5.6
Issue
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?
Solution
In AEM 5.6.1 and later, use a new topology API. For details, see http://sling.apache.org/documentation/bundles/discovery-api-and-impl.html
In earlier versions of AEM, identify a master in a Cluster environment using either Repository descriptor "crx.cluster.master" or ClusterAware interface.
Sample
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.sling.discovery.TopologyEvent; import org.apache.sling.discovery.TopologyEventListener; 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 org.apache.sling.discovery.TopologyEventListener#handleTopologyEvent(org.apache.sling.discovery.TopologyEvent) */ 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(); log.info("isLeader confirmed @@@@@@@@@@@@@@@@@@@"+ this.isLeaderInstance); } } /* (non-Javadoc) * @see java.lang.Runnable#run() */ public void run() { if (isLeaderInstance) { //TODO: your code here log.info("isLeader run @@@@@@@@@@@@@@@@@@@"+ this.isLeaderInstance); } } }
Download example package
ดาวน์โหลด