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