(クラスター化が有効になっている場合)コードがクラスターのマスターノードで実行されているかどうかを検出する方法について説明します。
リポジトリ記述子「crx.cluster.master」を使用します。ただし、一部のバージョンの CRX でクラスター化が無効になっている場合は、値が null になることがあります。次のコードを使用します(CRX 2.1.0.10+ を使用している CRX ベースのアプリケーションで動作します。)
package sample.demo; public interface ClusterService { public boolean isMaster(); }
package sample.demo; import sample.demo.ClusterService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Service; import org.apache.felix.scr.annotations.Reference; import javax.jcr.Repository; @Component(metatype = false) @Service(value = ClusterService.class) public class MyClusterAwareService implements ClusterService { private static final Logger LOGGER = LoggerFactory.getLogger(MyClusterAwareService.class); @Reference private Repository repository; public boolean isMaster() { String v = repository.getDescriptor("crx.cluster.master"); boolean isOnMaster = v == null || Boolean.parseBoolean(v); LOGGER.info("MyClusterAwareService isMaster" + isOnMaster ); return isOnMaster; } @Activate protected void activate() { LOGGER.info("service activated" ); } @Deactivate protected void deactivate() { LOGGER.info ("service deactivated"); } }
<% sample.demo.ClusterService clusterService = sling.getService(sample.demo.ClusterService.class); %> Am I master <%= clusterService.isMaster() %>
注意:CQ5 には、クラスター対応の OSGi サービスを実装するためのライブラリがいくつかあります。 ここを参照。
** CRX 2.1 が適用されるホットフィックス 2.1.0.10 が必要です
AEM 5.6.1(CRX 2.4.30)以降、新しいトポロジ API を使用します。詳細はこちらです https://helpx.adobe.com/jp/cq/kb/create-cluster-aware-osgi-service.html
アカウントにログイン