Search in sources :

Example 6 with MembershipListener

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MembershipEvent(com.hazelcast.cluster.MembershipEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with MembershipListener

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));
}
Also used : Address(com.hazelcast.cluster.Address) MembershipEvent(com.hazelcast.cluster.MembershipEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 8 with MembershipListener

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);
        }
    }
}
Also used : LifecycleListener(com.hazelcast.core.LifecycleListener) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) ProxyServiceImpl(com.hazelcast.spi.impl.proxyservice.impl.ProxyServiceImpl) ClientListener(com.hazelcast.client.ClientListener) ListenerConfig(com.hazelcast.config.ListenerConfig) PartitionLostListener(com.hazelcast.partition.PartitionLostListener) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) MigrationInterceptor(com.hazelcast.internal.partition.impl.MigrationInterceptor) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) MembershipListener(com.hazelcast.cluster.MembershipListener) MigrationListener(com.hazelcast.partition.MigrationListener) CPGroupAvailabilityListener(com.hazelcast.cp.event.CPGroupAvailabilityListener)

Aggregations

MembershipListener (com.hazelcast.cluster.MembershipListener)8 MembershipEvent (com.hazelcast.cluster.MembershipEvent)6 Member (com.hazelcast.cluster.Member)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Test (org.junit.Test)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 ClientConfig (com.hazelcast.client.config.ClientConfig)2 Config (com.hazelcast.config.Config)2 ListenerConfig (com.hazelcast.config.ListenerConfig)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ClientListener (com.hazelcast.client.ClientListener)1 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)1 Address (com.hazelcast.cluster.Address)1 Cluster (com.hazelcast.cluster.Cluster)1 InitialMembershipEvent (com.hazelcast.cluster.InitialMembershipEvent)1 InitialMembershipListener (com.hazelcast.cluster.InitialMembershipListener)1 VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)1 DistributedObjectListener (com.hazelcast.core.DistributedObjectListener)1