org.wso2.carbon.hazelcast.multicast.MulticastBasedMembershipScheme Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2005-2011, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.hazelcast.multicast;
import com.hazelcast.config.MulticastConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cluster.Member;
import com.hazelcast.cluster.MembershipEvent;
import com.hazelcast.cluster.MembershipListener;
import org.apache.axis2.clustering.ClusteringFault;
import org.apache.axis2.clustering.ClusteringMessage;
import org.apache.axis2.description.Parameter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.hazelcast.HazelcastCarbonClusterImpl;
import org.wso2.carbon.hazelcast.HazelcastMembershipScheme;
import org.wso2.carbon.hazelcast.HazelcastUtil;
import org.wso2.carbon.hazelcast.util.MemberUtils;
import java.util.List;
import java.util.Map;
/**
* Multicast based membership scheme based on Hazelcast
*/
public class MulticastBasedMembershipScheme implements HazelcastMembershipScheme {
private static final Log log = LogFactory.getLog(MulticastBasedMembershipScheme.class);
private final Map parameters;
private String primaryDomain;
private MulticastConfig config;
private final List messageBuffer;
private HazelcastCarbonClusterImpl carbonCluster;
private HazelcastInstance primaryHazelcastInstance;
public MulticastBasedMembershipScheme(Map parameters,
String primaryDomain,
MulticastConfig config,
List messageBuffer) {
this.parameters = parameters;
this.primaryDomain = primaryDomain;
this.config = config;
this.messageBuffer = messageBuffer;
}
@Override
public void init() throws ClusteringFault {
config.setEnabled(true);
configureMulticastParameters();
}
private void configureMulticastParameters() throws ClusteringFault {
Parameter mcastAddress = getParameter(MulticastConstants.MULTICAST_ADDRESS);
if (mcastAddress != null) {
config.setMulticastGroup((String) mcastAddress.getValue());
}
Parameter mcastPort = getParameter(MulticastConstants.MULTICAST_PORT);
if (mcastPort != null) {
config.setMulticastPort(Integer.parseInt(((String) (mcastPort.getValue())).trim()));
}
Parameter mcastTimeout = getParameter(MulticastConstants.MULTICAST_TIMEOUT);
if (mcastTimeout != null) {
config.setMulticastTimeoutSeconds(Integer.parseInt(((String) (mcastTimeout.getValue())).trim()));
}
Parameter mcastTTL = getParameter(MulticastConstants.MULTICAST_TTL);
if (mcastTTL != null) {
config.setMulticastTimeToLive(Integer.parseInt(((String) (mcastTTL.getValue())).trim()));
}
}
public Parameter getParameter(String name) {
return parameters.get(name);
}
@Override
public void joinGroup() throws ClusteringFault {
primaryHazelcastInstance.getCluster().addMembershipListener(new MulticastMembershipListener());
}
@Override
public void setPrimaryHazelcastInstance(HazelcastInstance primaryHazelcastInstance) {
this.primaryHazelcastInstance = primaryHazelcastInstance;
}
@Override
public void setLocalMember(Member localMember) {
// Nothing to do
}
@Override
public void setCarbonCluster(HazelcastCarbonClusterImpl hazelcastCarbonCluster) {
this.carbonCluster = hazelcastCarbonCluster;
}
private class MulticastMembershipListener implements MembershipListener {
private Map members;
public MulticastMembershipListener() {
members = MemberUtils.getMembersMap(primaryHazelcastInstance, primaryDomain);
}
@Override
public void memberAdded(MembershipEvent membershipEvent) {
Member member = membershipEvent.getMember();
// send all cluster messages
carbonCluster.memberAdded(member);
log.info("Member joined [" + member.getUuid() + "]: " + member.getSocketAddress().toString());
// Wait for sometime for the member to completely join before replaying messages
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) {
}
HazelcastUtil.sendMessagesToMember(messageBuffer, member, carbonCluster);
}
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
Member member = membershipEvent.getMember();
carbonCluster.memberRemoved(member);
log.info("Member left [" + member.getUuid() + "]: " + member.getSocketAddress().toString());
members.remove(member.getUuid());
}
}
}