use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.
the class ClientClusterRestartEventTest method testSingleMemberRestart.
@Test
public void testSingleMemberRestart() {
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(newConfig());
Member oldMember = instance.getCluster().getLocalMember();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(newClientConfig());
final CountDownLatch memberAdded = new CountDownLatch(1);
final CountDownLatch memberRemoved = new CountDownLatch(1);
final AtomicReference<Member> addedMemberReference = new AtomicReference<Member>();
final AtomicReference<Member> removedMemberReference = new AtomicReference<Member>();
client.getCluster().addMembershipListener(new MembershipListener() {
@Override
public void memberAdded(MembershipEvent membershipEvent) {
addedMemberReference.set(membershipEvent.getMember());
memberAdded.countDown();
}
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
removedMemberReference.set(membershipEvent.getMember());
memberRemoved.countDown();
}
});
instance.shutdown();
// Allow same addresses to be used to test hot restart correctly
hazelcastFactory.cleanup();
instance = hazelcastFactory.newHazelcastInstance(newConfig());
Member newMember = instance.getCluster().getLocalMember();
assertOpenEventually(memberRemoved);
assertEquals(oldMember, removedMemberReference.get());
assertOpenEventually(memberAdded);
assertEquals(newMember, addedMemberReference.get());
Set<Member> members = client.getCluster().getMembers();
assertContains(members, newMember);
assertEquals(1, members.size());
}
use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.
the class AdvancedNetworkClientIntegrationTest method testClientMembershipEvent.
@Test
public void testClientMembershipEvent() {
client = HazelcastClient.newHazelcastClient(getClientConfig());
AtomicReference<Member> memberAdded = new AtomicReference<>();
AtomicReference<Member> memberRemoved = new AtomicReference<>();
Address removedMemberAddress = instances[2].getCluster().getLocalMember().getAddressMap().get(CLIENT);
client.getCluster().addMembershipListener(new MembershipListener() {
@Override
public void memberAdded(MembershipEvent membershipEvent) {
memberAdded.set(membershipEvent.getMember());
}
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
memberRemoved.set(membershipEvent.getMember());
}
});
instances[2].shutdown();
assertClusterSizeEventually(2, instances[0]);
assertTrueEventually(() -> assertNotNull(memberRemoved.get()));
assertEquals(memberRemoved.get().getAddress(), removedMemberAddress);
instances[2] = Hazelcast.newHazelcastInstance(getConfig());
assertClusterSizeEventually(3, instances);
assertTrueEventually(() -> assertNotNull(memberAdded.get()));
assertEquals(memberAdded.get().getAddress(), instances[2].getCluster().getLocalMember().getAddressMap().get(CLIENT));
}
use of com.hazelcast.cluster.MembershipListener in project hazelcast by hazelcast.
the class Node method initializeListeners.
@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity", "checkstyle:methodlength" })
private void initializeListeners(Config config) {
for (final ListenerConfig listenerCfg : config.getListenerConfigs()) {
Object listener = listenerCfg.getImplementation();
if (listener == null) {
try {
listener = ClassLoaderUtil.newInstance(configClassLoader, listenerCfg.getClassName());
} catch (Exception e) {
logger.severe(e);
}
}
if (listener instanceof HazelcastInstanceAware) {
((HazelcastInstanceAware) listener).setHazelcastInstance(hazelcastInstance);
}
boolean known = false;
if (listener instanceof DistributedObjectListener) {
final ProxyServiceImpl proxyService = (ProxyServiceImpl) nodeEngine.getProxyService();
proxyService.addProxyListener((DistributedObjectListener) listener);
known = true;
}
if (listener instanceof MembershipListener) {
clusterService.addMembershipListener((MembershipListener) listener);
known = true;
}
if (listener instanceof MigrationListener) {
partitionService.addMigrationListener((MigrationListener) listener);
known = true;
}
if (listener instanceof PartitionLostListener) {
partitionService.addPartitionLostListener((PartitionLostListener) listener);
known = true;
}
if (listener instanceof LifecycleListener) {
hazelcastInstance.lifecycleService.addLifecycleListener((LifecycleListener) listener);
known = true;
}
if (listener instanceof ClientListener) {
String serviceName = ClientEngineImpl.SERVICE_NAME;
nodeEngine.getEventService().registerLocalListener(serviceName, serviceName, listener);
known = true;
}
if (listener instanceof MigrationInterceptor) {
partitionService.setMigrationInterceptor((MigrationInterceptor) listener);
known = true;
}
if (listener instanceof CPMembershipListener) {
hazelcastInstance.cpSubsystem.addMembershipListener((CPMembershipListener) listener);
known = true;
}
if (listener instanceof CPGroupAvailabilityListener) {
hazelcastInstance.cpSubsystem.addGroupAvailabilityListener((CPGroupAvailabilityListener) listener);
known = true;
}
if (nodeExtension.registerListener(listener)) {
known = true;
}
if (listener != null && !known) {
final String error = "Unknown listener type: " + listener.getClass();
Throwable t = new IllegalArgumentException(error);
logger.warning(error, t);
}
}
}
Aggregations