||
Skip to end of metadata
Go to start of metadata

The ClusterMembershipListner interface is an extension point provided by Kernel to register for cluster membership events such as the following:

  • Addition of new members

  • Removal of existing members.

WSO2 Carbon-based products use hazelcast as the clustering agent. When running products in a clustered environment, there maybe a requirement to listen for cluster member events. Implementing the ClusterMembershipListner interface caters to those custom requirements. The following section explains how to implement the interface and listen for Cluster Membership events.

Implementing ClusterMembershipListner API

A sample implementation of the ClusterMembershipListner is given below, which is basically the implementation of the memberAdded and memberRemoved methods. 

package org.wso2.carbon.clustering.test;

import org.wso2.carbon.core.clustering.api.ClusterMember;
import org.wso2.carbon.core.clustering.api.ClusterMembershipListener;

public class ClusterMembershipListenerImpl implements ClusterMembershipListener {
   @Override
   public void memberAdded(ClusterMember clusterMember) {
       //Some important logic regarding the Member Addition
       System.out.println("New Member Added......");
       System.out.println("Member ID :" + clusterMember.getId() + " " + "INET Address :" +
                          clusterMember.getInetSocketAddress().toString());

   }
   @Override
   public void memberRemoved(ClusterMember clusterMember) {
       //Some important logic regarding the Member Removal
       System.out.println("Member Removed......");
       System.out.println("Member ID :" + clusterMember.getId() + " " + "INET Address :" +
                          clusterMember.getInetSocketAddress().toString());
   }
}

After implementing the ClusterMembershipListner interface, you need to register it with the CarbonCluster service (This ensures that the implementation is added to the server). This can be done by acquiring the CarbonCluster OSGi service and registering the interface. See the following example:

package org.wso2.carbon.clustering.test.internal;

import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.clustering.test.ClusterMembershipListenerImpl;
import org.wso2.carbon.core.clustering.api.CarbonCluster;

/**
* @scr.component name="org.wso2.carbon.clustering.test.internal.ClusterMembershipListenerSC"
* immediate="true"
*@scr.referencename="cluster.membership.service" *interface="org.wso2.carbon.core.clustering.api.CarbonCluster"
* cardinality="1..1" policy="dynamic"  bind="setCarbonCluster" unbind="unsetCarbonCluster"
*/
public class ClusterMembershipListenerSC {

   private CarbonCluster carbonCluster;

   protected void activate(ComponentContext ctx) {
       if (carbonCluster != null) {
           carbonCluster.addMembershipListener(new ClusterMembershipListenerImpl());
       }
   }

   public void setCarbonCluster(CarbonCluster carbonCluster) {
       this.carbonCluster = carbonCluster;
   }

   public void unsetCarbonCluster(CarbonCluster carbonCluster) {
       this.carbonCluster = null;
   }
}

Following dependency is required for the above implementations.

<dependency>
           <groupId>org.wso2.carbon</groupId>
           <artifactId>org.wso2.carbon.core</artifactId>
           <version>4.4.1</version>
</dependency>

After building the OSGi bundle, add it to the <PRODUCT_HOME>/repository/components/dropins directory of each server. As the nodes in the cluster keep joining and removing, you will be able to see the  ClusterMembershipListenerImpl events being executed.

  • No labels