Search in sources :

Example 1 with InitialMembershipEvent

use of com.hazelcast.cluster.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.cluster.InitialMembershipEvent) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with InitialMembershipEvent

use of com.hazelcast.cluster.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.cluster.InitialMembershipEvent) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with InitialMembershipEvent

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

the class ClientClusterServiceImpl method applyInitialState.

private void applyInitialState(int version, Collection<MemberInfo> memberInfos) {
    MemberListSnapshot snapshot = createSnapshot(version, memberInfos);
    translateToPublicAddress.refresh(client.getClusterDiscoveryService().current().getAddressProvider(), memberInfos);
    memberListSnapshot.set(snapshot);
    logger.info(membersString(snapshot));
    Set<Member> members = toUnmodifiableHasSet(snapshot.members.values());
    InitialMembershipEvent event = new InitialMembershipEvent(client.getCluster(), members);
    for (MembershipListener listener : listeners.values()) {
        if (listener instanceof InitialMembershipListener) {
            ((InitialMembershipListener) listener).init(event);
        }
    }
}
Also used : InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent)

Example 4 with InitialMembershipEvent

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

the class ClientClusterServiceImpl method addMembershipListener.

@Nonnull
@Override
public UUID addMembershipListener(@Nonnull MembershipListener listener) {
    checkNotNull(listener, "Listener can't be null");
    synchronized (clusterViewLock) {
        UUID id = addMembershipListenerWithoutInit(listener);
        if (listener instanceof InitialMembershipListener) {
            Cluster cluster = client.getCluster();
            Collection<Member> members = memberListSnapshot.get().members.values();
            // it will be redirected to listeners when it arrives see #handleInitialMembershipEvent
            if (!members.isEmpty()) {
                InitialMembershipEvent event = new InitialMembershipEvent(cluster, toUnmodifiableHasSet(members));
                ((InitialMembershipListener) listener).init(event);
            }
        }
        return id;
    }
}
Also used : InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) Cluster(com.hazelcast.cluster.Cluster) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent) Nonnull(javax.annotation.Nonnull)

Example 5 with InitialMembershipEvent

use of com.hazelcast.cluster.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.cluster.InitialMembershipEvent) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

InitialMembershipEvent (com.hazelcast.cluster.InitialMembershipEvent)7 ClientConfig (com.hazelcast.client.config.ClientConfig)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 InitialMembershipListener (com.hazelcast.cluster.InitialMembershipListener)3 EventObject (java.util.EventObject)3 Member (com.hazelcast.cluster.Member)2 ListenerConfig (com.hazelcast.config.ListenerConfig)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 Nonnull (javax.annotation.Nonnull)2 Cluster (com.hazelcast.cluster.Cluster)1 MembershipListener (com.hazelcast.cluster.MembershipListener)1 EventRegistration (com.hazelcast.spi.impl.eventservice.EventRegistration)1 EventService (com.hazelcast.spi.impl.eventservice.EventService)1 AssertTask (com.hazelcast.test.AssertTask)1 UUID (java.util.UUID)1