Search in sources :

Example 21 with MembershipEvent

use of com.hazelcast.core.MembershipEvent in project hazelcast by hazelcast.

the class MembershipListenerTest method whenMemberRemoved_thenMemberRemovedEvent.

@Test
public void whenMemberRemoved_thenMemberRemovedEvent() throws Exception {
    final HazelcastInstance server1 = hazelcastFactory.newHazelcastInstance();
    final MemberShipEventLogger listener = new MemberShipEventLogger();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    //start a second server and verify that hazelcastFactory listener receives it.
    final HazelcastInstance server2 = hazelcastFactory.newHazelcastInstance();
    client.getCluster().addMembershipListener(listener);
    final Member server2Member = server2.getCluster().getLocalMember();
    server2.shutdown();
    //verify that the correct member removed event was received.
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertNotEquals("Expecting one or more events", 0, listener.events.size());
            MembershipEvent event = (MembershipEvent) listener.events.getLast();
            assertEquals("Last event should be member removed", MembershipEvent.MEMBER_REMOVED, event.getEventType());
            assertEquals(server2Member, event.getMember());
            assertEquals(getMembers(server1), event.getMembers());
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) AssertTask(com.hazelcast.test.AssertTask) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 22 with MembershipEvent

use of com.hazelcast.core.MembershipEvent in project hazelcast by hazelcast.

the class DefaultPublisherContext method handleSubscriberAddRemove.

private void handleSubscriberAddRemove() {
    ClusterServiceImpl clusterService = (ClusterServiceImpl) nodeEngine.getClusterService();
    clusterService.addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            Member member = membershipEvent.getMember();
            String uuid = member.getUuid();
            handleDisconnectedSubscriber(uuid);
        }

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            Member member = membershipEvent.getMember();
            String uuid = member.getUuid();
            handleConnectedSubscriber(uuid);
        }
    });
}
Also used : MembershipEvent(com.hazelcast.core.MembershipEvent) ClusterServiceImpl(com.hazelcast.internal.cluster.impl.ClusterServiceImpl) MembershipAdapter(com.hazelcast.core.MembershipAdapter) Member(com.hazelcast.core.Member)

Aggregations

MembershipEvent (com.hazelcast.core.MembershipEvent)22 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 InitialMembershipEvent (com.hazelcast.core.InitialMembershipEvent)9 MembershipAdapter (com.hazelcast.core.MembershipAdapter)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 Member (com.hazelcast.core.Member)6 ParallelTest (com.hazelcast.test.annotation.ParallelTest)6 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Config (com.hazelcast.config.Config)5 ListenerConfig (com.hazelcast.config.ListenerConfig)5 JoinConfig (com.hazelcast.config.JoinConfig)4 NetworkConfig (com.hazelcast.config.NetworkConfig)4 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)4 AssertTask (com.hazelcast.test.AssertTask)4 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)3 MembershipListener (com.hazelcast.core.MembershipListener)3 Node (com.hazelcast.instance.Node)3 FirewallingTcpIpConnectionManager (com.hazelcast.nio.tcp.FirewallingTcpIpConnectionManager)3 TransactionalQueue (com.hazelcast.core.TransactionalQueue)2