Search in sources :

Example 11 with LifecycleListener

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

the class BatchInvalidator method registerNodeShutdownListener.

/**
     * Sends remaining invalidation events in this invalidator's queues to the recipients.
     */
private String registerNodeShutdownListener() {
    HazelcastInstance node = nodeEngine.getHazelcastInstance();
    LifecycleService lifecycleService = node.getLifecycleService();
    return lifecycleService.addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (event.getState() == SHUTTING_DOWN) {
                Set<Map.Entry<String, InvalidationQueue>> entries = invalidationQueues.entrySet();
                for (Map.Entry<String, InvalidationQueue> entry : entries) {
                    pollAndSendInvalidations(entry.getKey(), entry.getValue());
                }
            }
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Set(java.util.Set) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleService(com.hazelcast.core.LifecycleService) LifecycleListener(com.hazelcast.core.LifecycleListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 12 with LifecycleListener

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

the class RestClusterTest method testShutdownNode.

@Test
public void testShutdownNode() throws Exception {
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HTTPCommunicator communicator = new HTTPCommunicator(instance);
    final CountDownLatch shutdownLatch = new CountDownLatch(1);
    instance.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (event.getState() == LifecycleEvent.LifecycleState.SHUTDOWN) {
                shutdownLatch.countDown();
            }
        }
    });
    try {
        assertEquals("{\"status\":\"success\"}", communicator.shutdownMember("dev", "dev-pass"));
    } catch (ConnectException ignored) {
    // if node shuts down before response is received, `java.net.ConnectException: Connection refused` is expected
    }
    assertOpenEventually(shutdownLatch);
    assertFalse(instance.getLifecycleService().isRunning());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectException(java.net.ConnectException) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 13 with LifecycleListener

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

the class ClientHeartbeatTest method testClientEndpointsDelaySeconds_whenHeartbeatResumed.

@Test
public void testClientEndpointsDelaySeconds_whenHeartbeatResumed() throws Exception {
    int delaySeconds = 2;
    Config config = new Config();
    config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(delaySeconds));
    HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance(config);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setProperty(ClientProperty.HEARTBEAT_TIMEOUT.getName(), "4000");
    clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "1000");
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    LifecycleService lifecycleService = client.getLifecycleService();
    lifecycleService.addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
                disconnectedLatch.countDown();
            }
        }
    });
    blockMessagesFromInstance(hazelcastInstance, client);
    //Wait for client to disconnect because of hearBeat problem.
    assertOpenEventually(disconnectedLatch);
    final CountDownLatch connectedLatch = new CountDownLatch(1);
    final AtomicLong stateChangeCount = new AtomicLong();
    lifecycleService.addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            stateChangeCount.incrementAndGet();
            Logger.getLogger(this.getClass()).info("state event : " + event);
            if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
                connectedLatch.countDown();
            }
        }
    });
    unblockMessagesFromInstance(hazelcastInstance, client);
    //Wait for client to connect back after heartbeat issue is resolved
    assertOpenEventually(connectedLatch);
    //After client connected, there should not be further change in client state
    //We are specifically testing for scheduled ClientDisconnectionOperation not to take action when run
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, stateChangeCount.get());
        }
    }, delaySeconds * 2);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleService(com.hazelcast.core.LifecycleService) LifecycleListener(com.hazelcast.core.LifecycleListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ExpectedException(org.junit.rules.ExpectedException) ExecutionException(java.util.concurrent.ExecutionException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 14 with LifecycleListener

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

the class AbstractListenersOnReconnectTest method testListenersWhenClientDisconnectedOperationRuns_whenOwnerConnectionRemoved.

@Test
public void testListenersWhenClientDisconnectedOperationRuns_whenOwnerConnectionRemoved() {
    Config config = new Config();
    int endpointDelaySeconds = 2;
    config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(endpointDelaySeconds));
    config.setProperty(GroupProperty.CLIENT_HEARTBEAT_TIMEOUT_SECONDS.getName(), "4");
    HazelcastInstance ownerServer = factory.newHazelcastInstance(config);
    ClientConfig smartClientConfig = getSmartClientConfig();
    smartClientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "500");
    client = factory.newHazelcastClient(smartClientConfig);
    factory.newHazelcastInstance(config);
    setupListener();
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    final CountDownLatch connectedLatch = new CountDownLatch(1);
    client.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
                disconnectedLatch.countDown();
            }
            if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
                connectedLatch.countDown();
            }
        }
    });
    blockMessagesToInstance(ownerServer, client);
    assertOpenEventually(disconnectedLatch);
    sleepAtLeastMillis(TimeUnit.SECONDS.toMillis(endpointDelaySeconds) * 2);
    unblockMessagesToInstance(ownerServer, client);
    assertOpenEventually(connectedLatch);
    validateRegistrationsAndListenerFunctionality();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with LifecycleListener

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

the class AbstractListenersOnReconnectTest method testListenersWhenClientDisconnectedOperationRuns_whenOwnerMemberRemoved.

//--------------------------------------------------------------------------------- //
@Test
public void testListenersWhenClientDisconnectedOperationRuns_whenOwnerMemberRemoved() {
    Config config = new Config();
    int endpointDelaySeconds = 2;
    config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(endpointDelaySeconds));
    HazelcastInstance ownerServer = factory.newHazelcastInstance(config);
    client = factory.newHazelcastClient(getSmartClientConfig());
    HazelcastInstance server2 = factory.newHazelcastInstance(config);
    setupListener();
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    final CountDownLatch connectedLatch = new CountDownLatch(1);
    client.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
                disconnectedLatch.countDown();
            }
            if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
                connectedLatch.countDown();
            }
        }
    });
    blockMessagesToInstance(server2, client);
    ownerServer.shutdown();
    sleepAtLeastMillis(TimeUnit.SECONDS.toMillis(endpointDelaySeconds) * 2);
    unblockMessagesToInstance(server2, client);
    assertOpenEventually(disconnectedLatch);
    assertOpenEventually(connectedLatch);
    clusterSize = clusterSize - 1;
    validateRegistrationsAndListenerFunctionality();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

LifecycleListener (com.hazelcast.core.LifecycleListener)27 LifecycleEvent (com.hazelcast.core.LifecycleEvent)25 HazelcastInstance (com.hazelcast.core.HazelcastInstance)23 CountDownLatch (java.util.concurrent.CountDownLatch)23 Test (org.junit.Test)21 ClientConfig (com.hazelcast.client.config.ClientConfig)14 ParallelTest (com.hazelcast.test.annotation.ParallelTest)13 QuickTest (com.hazelcast.test.annotation.QuickTest)13 NightlyTest (com.hazelcast.test.annotation.NightlyTest)10 Config (com.hazelcast.config.Config)9 ListenerConfig (com.hazelcast.config.ListenerConfig)7 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)5 AssertTask (com.hazelcast.test.AssertTask)5 JoinConfig (com.hazelcast.config.JoinConfig)3 NetworkConfig (com.hazelcast.config.NetworkConfig)3 DistributedObject (com.hazelcast.core.DistributedObject)3 ExecutionException (java.util.concurrent.ExecutionException)3 LifecycleService (com.hazelcast.core.LifecycleService)2 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)2 Node (com.hazelcast.instance.Node)2