use of com.hazelcast.core.MembershipEvent in project hazelcast by hazelcast.
the class ClientMembershipListener method detectMembershipEvents.
private List<MembershipEvent> detectMembershipEvents(Map<String, Member> prevMembers) {
List<MembershipEvent> events = new LinkedList<MembershipEvent>();
Set<Member> eventMembers = unmodifiableSet(members);
List<Member> newMembers = new LinkedList<Member>();
for (Member member : members) {
Member former = prevMembers.remove(member.getUuid());
if (former == null) {
newMembers.add(member);
}
}
// removal events should be added before added events
for (Member member : prevMembers.values()) {
events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, eventMembers));
Address address = member.getAddress();
if (clusterService.getMember(address) == null) {
Connection connection = connectionManager.getConnection(address);
if (connection != null) {
connection.close(null, newTargetDisconnectedExceptionCausedByMemberLeftEvent(connection));
}
}
}
for (Member member : newMembers) {
events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_ADDED, eventMembers));
}
return events;
}
use of com.hazelcast.core.MembershipEvent in project hazelcast by hazelcast.
the class ClientReconnectTest method testReconnectToNewInstanceAtSameAddress.
@Test
public void testReconnectToNewInstanceAtSameAddress() throws InterruptedException {
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
Address localAddress = instance.getCluster().getLocalMember().getAddress();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
client.getCluster().addMembershipListener(new MembershipAdapter() {
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
memberRemovedLatch.countDown();
}
});
instance.shutdown();
final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance(localAddress);
assertOpenEventually(memberRemovedLatch);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(1, client.getCluster().getMembers().size());
Iterator<Member> iterator = client.getCluster().getMembers().iterator();
Member member = iterator.next();
assertEquals(instance2.getCluster().getLocalMember(), member);
}
});
}
use of com.hazelcast.core.MembershipEvent in project hazelcast by hazelcast.
the class ClientMembershipListener method handle.
@Override
public void handle(Collection<Member> initialMembers) {
Map<String, Member> prevMembers = Collections.emptyMap();
if (!members.isEmpty()) {
prevMembers = new HashMap<String, Member>(members.size());
for (Member member : members) {
prevMembers.put(member.getUuid(), member);
}
members.clear();
}
for (Member initialMember : initialMembers) {
members.add(initialMember);
}
if (prevMembers.isEmpty()) {
//this means this is the first time client connected to server
logger.info(membersString());
clusterService.handleInitialMembershipEvent(new InitialMembershipEvent(client.getCluster(), unmodifiableSet(members)));
initialListFetchedLatch.countDown();
return;
}
List<MembershipEvent> events = detectMembershipEvents(prevMembers);
logger.info(membersString());
fireMembershipEvent(events);
initialListFetchedLatch.countDown();
}
use of com.hazelcast.core.MembershipEvent in project hazelcast by hazelcast.
the class ClientMembershipListener method memberAdded.
private void memberAdded(Member member) {
members.add(member);
logger.info(membersString());
MembershipEvent event = new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_ADDED, unmodifiableSet(members));
clusterService.handleMembershipEvent(event);
}
use of com.hazelcast.core.MembershipEvent in project hazelcast by hazelcast.
the class ClientTxnTest method testTxnRollback.
@Test
public void testTxnRollback() throws Exception {
final String queueName = randomString();
final TransactionContext context = client.newTransactionContext();
CountDownLatch txnRollbackLatch = new CountDownLatch(1);
final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
client.getCluster().addMembershipListener(new MembershipAdapter() {
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
memberRemovedLatch.countDown();
}
});
try {
context.beginTransaction();
assertNotNull(context.getTxnId());
final TransactionalQueue queue = context.getQueue(queueName);
queue.offer(randomString());
server.shutdown();
context.commitTransaction();
fail("commit should throw exception!!!");
} catch (Exception e) {
context.rollbackTransaction();
txnRollbackLatch.countDown();
}
assertOpenEventually(txnRollbackLatch);
assertOpenEventually(memberRemovedLatch);
final IQueue<Object> q = client.getQueue(queueName);
assertNull(q.poll());
assertEquals(0, q.size());
}
Aggregations