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());
}
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());
}
});
}
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);
}
}
}
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;
}
}
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());
}
Aggregations