The Lifecycle Listener notifies for the following events:

  • STARTING: A member is starting.
  • STARTED: A member started.
  • SHUTTING_DOWN: A member is shutting down.
  • SHUTDOWN: A member's shutdown has completed.
  • MERGING: A member is merging with the cluster.
  • MERGED: A member's merge operation has completed.
  • CLIENT_CONNECTED: A Hazelcast Client connected to the cluster.
  • CLIENT_DISCONNECTED: A Hazelcast Client disconnected from the cluster.

The following is an example Lifecycle Listener class.

public class NodeLifecycleListener implements LifecycleListener {
     @Override
     public void stateChanged(LifecycleEvent event) {
       System.err.println(event);
     }
}

This listener is local to an individual member. It notifies the application that uses Hazelcast about the events mentioned above for a particular member.

Registering Lifecycle Listeners

After you create your class, you can configure your cluster to include lifecycle listeners. Below is an example using the method addLifecycleListener.

HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
hazelcastInstance.getLifecycleService().addLifecycleListener( new NodeLifecycleListener() );

With the above approach, there is the possibility of missing events between the creation of the instance and registering the listener. To overcome this race condition, Hazelcast allows you to register the listeners in the configuration. You can register listeners using declarative, programmatic, or Spring configuration, as shown below.

The following is an example programmatic configuration.

config.addListenerConfig(
new ListenerConfig( "com.your-package.NodeLifecycleListener" ) );

The following is an example of the equivalent declarative configuration.

<hazelcast>
   ...
   <listeners>
	  <listener>
	  com.your-package.NodeLifecycleListener
      </listener>
   </listeners>
   ...
</hazelcast>

The following is an example of the equivalent Spring configuration.

<hz:listeners>
   <hz:listener class-name="com.your-package.NodeLifecycleListener"/>
   <hz:listener implementation="LifecycleListener"/>
</hz:listeners>