Skip to content

Commit

Permalink
Fire STARTING lifecycle event after listeners are registered
Browse files Browse the repository at this point in the history
  • Loading branch information
metanet committed Dec 29, 2016
1 parent 6679cc9 commit 90016f4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Expand Up @@ -89,7 +89,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static com.hazelcast.core.LifecycleEvent.LifecycleState.STARTING;
import static com.hazelcast.util.Preconditions.checkNotNull;

@PrivateApi
Expand Down Expand Up @@ -132,9 +131,9 @@ protected HazelcastInstanceImpl(String name, Config config, NodeContext nodeCont

try {
logger = node.getLogger(getClass().getName());
lifecycleService.fireLifecycleEvent(STARTING);

node.start();

if (!node.isRunning()) {
throw new IllegalStateException("Node failed to start!");
}
Expand Down
2 changes: 2 additions & 0 deletions hazelcast/src/main/java/com/hazelcast/instance/Node.java
Expand Up @@ -30,6 +30,7 @@
import com.hazelcast.core.ClientListener;
import com.hazelcast.core.DistributedObjectListener;
import com.hazelcast.core.HazelcastInstanceAware;
import com.hazelcast.core.LifecycleEvent.LifecycleState;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.core.MembershipListener;
import com.hazelcast.core.MigrationListener;
Expand Down Expand Up @@ -371,6 +372,7 @@ public void setMasterAddress(final Address master) {
void start() {
nodeEngine.start();
initializeListeners(config);
hazelcastInstance.lifecycleService.fireLifecycleEvent(LifecycleState.STARTING);
connectionManager.start();
if (config.getNetworkConfig().getJoin().getMulticastConfig().isEnabled()) {
final Thread multicastServiceThread = new Thread(
Expand Down
Expand Up @@ -19,7 +19,9 @@
import com.hazelcast.config.Config;
import com.hazelcast.config.ListenerConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleEvent.LifecycleState;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
Expand All @@ -29,9 +31,12 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(HazelcastSerialClassRunner.class)
Expand All @@ -48,6 +53,45 @@ public void testListenerNoDeadLock() throws Exception {
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
public void testListenerInvocationWhenNodeStarts() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
final Config config = new Config();
final EventCountingListener listener = new EventCountingListener();
config.addListenerConfig(new ListenerConfig(listener));
factory.newHazelcastInstance(config);
assertEquals(LifecycleState.STARTING, listener.events.get(0));
assertEquals(LifecycleState.STARTED, listener.events.get(1));
}

@Test
public void testListenerInvocationWhenNodeShutsDown() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
final Config config = new Config();
final EventCountingListener listener = new EventCountingListener();
config.addListenerConfig(new ListenerConfig(listener));
HazelcastInstance instance = factory.newHazelcastInstance(config);

listener.events.clear();
instance.getLifecycleService().shutdown();
assertEquals(LifecycleState.SHUTTING_DOWN, listener.events.get(0));
assertEquals(LifecycleState.SHUTDOWN, listener.events.get(1));
}

@Test
public void testListenerInvocationWhenNodeTerminates() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
final Config config = new Config();
final EventCountingListener listener = new EventCountingListener();
config.addListenerConfig(new ListenerConfig(listener));
HazelcastInstance instance = factory.newHazelcastInstance(config);

listener.events.clear();
instance.getLifecycleService().terminate();
assertEquals(LifecycleState.SHUTTING_DOWN, listener.events.get(0));
assertEquals(LifecycleState.SHUTDOWN, listener.events.get(1));
}

static class MyLifecycleListener implements LifecycleListener {

private CountDownLatch latch;
Expand All @@ -58,10 +102,22 @@ static class MyLifecycleListener implements LifecycleListener {

@Override
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleEvent.LifecycleState.STARTED) {
if (event.getState() == LifecycleState.STARTED) {
Hazelcast.getHazelcastInstanceByName("_hzInstance_1_dev");
latch.countDown();
}
}
}

static class EventCountingListener implements LifecycleListener {

private final List<LifecycleState> events = new CopyOnWriteArrayList<LifecycleState>();

@Override
public void stateChanged(LifecycleEvent event) {
events.add(event.getState());
}

}

}

0 comments on commit 90016f4

Please sign in to comment.