Search in sources :

Example 1 with InitialMembershipEvent

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

the class ClusterMembershipTest method assertInitialMembershipEvent.

public void assertInitialMembershipEvent(EventObject e, Member... expectedMembers) {
    assertTrue(e instanceof InitialMembershipEvent);
    InitialMembershipEvent initialMembershipEvent = (InitialMembershipEvent) e;
    Set<Member> foundMembers = initialMembershipEvent.getMembers();
    assertEquals(new HashSet<Member>(Arrays.asList(expectedMembers)), foundMembers);
}
Also used : Member(com.hazelcast.core.Member) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent)

Example 2 with InitialMembershipEvent

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

the class MembershipListenerTest method initialMemberEvents_whenAddedViaConfig.

@Test
public void initialMemberEvents_whenAddedViaConfig() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    hazelcastFactory.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    final InitialMemberShipEventLogger listener = new InitialMemberShipEventLogger();
    clientConfig.addListenerConfig(new ListenerConfig().setImplementation(listener));
    hazelcastFactory.newHazelcastClient(clientConfig);
    EventObject eventObject = listener.events.poll(ASSERT_TRUE_EVENTUALLY_TIMEOUT, TimeUnit.SECONDS);
    assertInstanceOf(InitialMembershipEvent.class, eventObject);
    InitialMembershipEvent event = (InitialMembershipEvent) eventObject;
    assertEquals(2, event.getMembers().size());
    assertEquals(0, listener.events.size());
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) EventObject(java.util.EventObject) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with InitialMembershipEvent

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

the class MembershipListenerTest method givenMixOfListenerExists_whenConnect_thenCallInitialMembershipListener.

@Test
public void givenMixOfListenerExists_whenConnect_thenCallInitialMembershipListener() throws Exception {
    hazelcastFactory.newHazelcastInstance();
    final ClientConfig config = new ClientConfig();
    // first add bunch of *regular* MembershipListener. They do not implement InitialMembershipListener
    config.addListenerConfig(new ListenerConfig().setImplementation(new MemberShipEventLogger()));
    config.addListenerConfig(new ListenerConfig().setImplementation(new MemberShipEventLogger()));
    config.addListenerConfig(new ListenerConfig().setImplementation(new MemberShipEventLogger()));
    // now add an InitialMembershipListener
    // if there is an exception thrown during event delivery to regular listeners
    // then no event will likely be delivered to InitialMemberShipEventLogger
    final InitialMemberShipEventLogger initialListener = new InitialMemberShipEventLogger();
    config.addListenerConfig(new ListenerConfig().setImplementation(initialListener));
    //connect to a grid
    hazelcastFactory.newHazelcastClient(config);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals("Expecting one event", 1, initialListener.events.size());
            InitialMembershipEvent event = (InitialMembershipEvent) initialListener.events.getLast();
            assertEquals(1, event.getMembers().size());
        }
    });
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) AssertTask(com.hazelcast.test.AssertTask) ClientConfig(com.hazelcast.client.config.ClientConfig) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with InitialMembershipEvent

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

the class MembershipListenerTest method initialMemberEvents_whenAddedAfterClientStarted.

@Test
public void initialMemberEvents_whenAddedAfterClientStarted() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    hazelcastFactory.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    final InitialMemberShipEventLogger listener = new InitialMemberShipEventLogger();
    client.getCluster().addMembershipListener(listener);
    EventObject eventObject = listener.events.poll(ASSERT_TRUE_EVENTUALLY_TIMEOUT, TimeUnit.SECONDS);
    assertInstanceOf(InitialMembershipEvent.class, eventObject);
    InitialMembershipEvent event = (InitialMembershipEvent) eventObject;
    assertEquals(2, event.getMembers().size());
    assertEquals(0, listener.events.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) EventObject(java.util.EventObject) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with InitialMembershipEvent

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

the class ClientClusterServiceImpl method initMembershipListener.

private void initMembershipListener(MembershipListener listener) {
    if (listener instanceof InitialMembershipListener) {
        Cluster cluster = client.getCluster();
        Collection<Member> memberCollection = members.get().values();
        LinkedHashSet<Member> members = new LinkedHashSet<Member>(memberCollection);
        InitialMembershipEvent event = new InitialMembershipEvent(cluster, members);
        ((InitialMembershipListener) listener).init(event);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InitialMembershipListener(com.hazelcast.core.InitialMembershipListener) Cluster(com.hazelcast.core.Cluster) Member(com.hazelcast.core.Member) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent)

Aggregations

InitialMembershipEvent (com.hazelcast.core.InitialMembershipEvent)7 ClientConfig (com.hazelcast.client.config.ClientConfig)3 Member (com.hazelcast.core.Member)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 ListenerConfig (com.hazelcast.config.ListenerConfig)2 InitialMembershipListener (com.hazelcast.core.InitialMembershipListener)2 EventObject (java.util.EventObject)2 Cluster (com.hazelcast.core.Cluster)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 MembershipEvent (com.hazelcast.core.MembershipEvent)1 AbstractMember (com.hazelcast.instance.AbstractMember)1 EventRegistration (com.hazelcast.spi.EventRegistration)1 EventService (com.hazelcast.spi.EventService)1 AssertTask (com.hazelcast.test.AssertTask)1 LinkedHashSet (java.util.LinkedHashSet)1