Search in sources :

Example 1 with MembershipEvent

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

the class HazelcastInstanceConsumerTest method testRemoveInstance.

@Test
public void testRemoveInstance() throws InterruptedException {
    MockEndpoint removed = getMockEndpoint("mock:removed");
    removed.setExpectedMessageCount(1);
    when(member.getSocketAddress()).thenReturn(new InetSocketAddress("foo.bar", 12345));
    MembershipEvent event = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_REMOVED, null);
    argument.getValue().memberRemoved(event);
    assertMockEndpointsSatisfied(5000, TimeUnit.MILLISECONDS);
    // check headers
    Exchange ex = removed.getExchanges().get(0);
    Map<String, Object> headers = ex.getIn().getHeaders();
    this.checkHeaders(headers, HazelcastConstants.REMOVED);
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InetSocketAddress(java.net.InetSocketAddress) MembershipEvent(com.hazelcast.core.MembershipEvent) Test(org.junit.Test)

Example 2 with MembershipEvent

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

the class HazelcastInstanceConsumerTest method testAddInstance.

@Test
public void testAddInstance() throws InterruptedException {
    MockEndpoint added = getMockEndpoint("mock:added");
    added.setExpectedMessageCount(1);
    when(member.getSocketAddress()).thenReturn(new InetSocketAddress("foo.bar", 12345));
    MembershipEvent event = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_ADDED, null);
    argument.getValue().memberAdded(event);
    assertMockEndpointsSatisfied(5000, TimeUnit.MILLISECONDS);
    // check headers
    Exchange ex = added.getExchanges().get(0);
    Map<String, Object> headers = ex.getIn().getHeaders();
    this.checkHeaders(headers, HazelcastConstants.ADDED);
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InetSocketAddress(java.net.InetSocketAddress) MembershipEvent(com.hazelcast.core.MembershipEvent) Test(org.junit.Test)

Example 3 with MembershipEvent

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

the class ClientMembershipListener method memberRemoved.

private void memberRemoved(Member member) {
    members.remove(member);
    logger.info(membersString());
    final Connection connection = connectionManager.getConnection(member.getAddress());
    if (connection != null) {
        connection.close(null, newTargetDisconnectedExceptionCausedByMemberLeftEvent(connection));
    }
    MembershipEvent event = new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, unmodifiableSet(members));
    clusterService.handleMembershipEvent(event);
}
Also used : InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) Connection(com.hazelcast.nio.Connection)

Example 4 with MembershipEvent

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

the class MembershipListenerTest method whenMemberAdded_thenMemberAddedEvent.

@Test
public void whenMemberAdded_thenMemberAddedEvent() throws Exception {
    final HazelcastInstance server1 = hazelcastFactory.newHazelcastInstance();
    final MemberShipEventLogger listener = new MemberShipEventLogger();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    client.getCluster().addMembershipListener(listener);
    //start a second server and verify that the listener receives it.
    final HazelcastInstance server2 = hazelcastFactory.newHazelcastInstance();
    //verify that the listener receives member added event.
    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 added", MembershipEvent.MEMBER_ADDED, event.getEventType());
            assertEquals(server2.getCluster().getLocalMember(), event.getMember());
            assertEquals(getMembers(server1, server2), event.getMembers());
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with MembershipEvent

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

the class ClientTxnTest method testTxnRollbackOnServerCrash.

@Test
public void testTxnRollbackOnServerCrash() throws Exception {
    final String queueName = randomString();
    final TransactionContext context = client.newTransactionContext();
    CountDownLatch txnRollbackLatch = new CountDownLatch(1);
    final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
    context.beginTransaction();
    final TransactionalQueue queue = context.getQueue(queueName);
    queue.offer(randomString());
    client.getCluster().addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    server.getLifecycleService().terminate();
    try {
        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());
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.core.MembershipEvent) MembershipAdapter(com.hazelcast.core.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

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