String serviceType = clustercreatedEvent.getServiceName();
//acquire read lock
TopologyManager.acquireReadLock();
try {
Cluster cluster = TopologyManager.getTopology().getService(serviceType).getCluster(clustercreatedEvent.getClusterId());
TopologyClusterInformationModel.getInstance().addCluster(cluster);
} finally {
//release read lock
TopologyManager.releaseReadLock();
}
}
});
//Cluster Removed event listner
topologyEventReceiver.addEventListener(new ClusterRemovedEventListener() {
@Override
protected void onEvent(Event event) {
log.info("[ClusterRemovedEventListener] Received: " + event.getClass());
ClusterRemovedEvent clusterRemovedEvent = (ClusterRemovedEvent) event;
TopologyClusterInformationModel.getInstance().removeCluster(clusterRemovedEvent.getClusterId());
}
});
//Instance Spawned event listner
topologyEventReceiver.addEventListener(new InstanceSpawnedEventListener() {
@Override
protected void onEvent(Event event) {
log.info("[InstanceSpawnedEventListener] Received: " + event.getClass());
InstanceSpawnedEvent instanceSpawnedEvent = (InstanceSpawnedEvent) event;
String clusterDomain = instanceSpawnedEvent.getClusterId();
String serviceType = instanceSpawnedEvent.getServiceName();
//acquire read lock
TopologyManager.acquireReadLock();
try {
Cluster cluster = TopologyManager.getTopology().getService(serviceType).getCluster(clusterDomain);
TopologyClusterInformationModel.getInstance().addCluster(cluster);
} finally {
//release read lock
TopologyManager.releaseReadLock();
}
}
});
//Member Started event listner
topologyEventReceiver.addEventListener(new MemberStartedEventListener() {
@Override
protected void onEvent(Event event) {
log.info("[MemberStartedEventListener] Received: " + event.getClass());
MemberStartedEvent memberStartedEvent = (MemberStartedEvent) event;
String clusterDomain = memberStartedEvent.getClusterId();
String serviceType = memberStartedEvent.getServiceName();
//acquire read lock
TopologyManager.acquireReadLock();
try {
Cluster cluster = TopologyManager.getTopology().getService(serviceType).getCluster(clusterDomain);
TopologyClusterInformationModel.getInstance().addCluster(cluster);
} finally {
//release read lock
TopologyManager.releaseReadLock();
}
}
});
//Member Activated event listner
topologyEventReceiver.addEventListener(new MemberActivatedEventListener() {
@Override
protected void onEvent(Event event) {
log.info("[MemberActivatedEventListener] Received: " + event.getClass());
MemberActivatedEvent memberActivatedEvent = (MemberActivatedEvent) event;
String clusterDomain = memberActivatedEvent.getClusterId();
String serviceType = memberActivatedEvent.getServiceName();
//acquire read lock
TopologyManager.acquireReadLock();
try {
Cluster cluster = TopologyManager.getTopology().getService(serviceType).getCluster(clusterDomain);
TopologyClusterInformationModel.getInstance().addCluster(cluster);
} finally {
//release read lock
TopologyManager.releaseReadLock();
}
}
});
//Member Suspended event listner
topologyEventReceiver.addEventListener(new MemberSuspendedEventListener() {
@Override
protected void onEvent(Event event) {
log.info("[MemberSuspendedEventListener] Received: " + event.getClass());
MemberSuspendedEvent memberSuspendedEvent = (MemberSuspendedEvent) event;
String clusterDomain = memberSuspendedEvent.getClusterId();
String serviceType = memberSuspendedEvent.getServiceName();
//acquire read lock
TopologyManager.acquireReadLock();
try {
Cluster cluster = TopologyManager.getTopology().getService(serviceType).getCluster(clusterDomain);
TopologyClusterInformationModel.getInstance().addCluster(cluster);
} finally {
//release read lock
TopologyManager.releaseReadLock();
}
}
});
//Member Terminated event listner
topologyEventReceiver.addEventListener(new MemberTerminatedEventListener() {
@Override
protected void onEvent(Event event) {
log.info("[MemberTerminatedEventListener] Received: " + event.getClass());
MemberTerminatedEvent memberTerminatedEvent = (MemberTerminatedEvent) event;
String clusterDomain = memberTerminatedEvent.getClusterId();
String serviceType = memberTerminatedEvent.getServiceName();
//acquire read lock
TopologyManager.acquireReadLock();
try {
Cluster cluster = TopologyManager.getTopology().getService(serviceType).getCluster(clusterDomain);
// check and remove terminated member
if (cluster.memberExists(memberTerminatedEvent.getMemberId())) {
// release the read lock and acquire the write lock
TopologyManager.releaseReadLock();
TopologyManager.acquireWriteLock();
try {
// re-check the state; another thread might have acquired the write lock and modified
if (cluster.memberExists(memberTerminatedEvent.getMemberId())) {
// remove the member from the cluster
Member terminatedMember = cluster.getMember(memberTerminatedEvent.getMemberId());
cluster.removeMember(terminatedMember);
if (log.isDebugEnabled()) {
log.debug("Removed the terminated member with id " + memberTerminatedEvent.getMemberId() + " from the cluster");
}
}