The Migration Listener interface has methods that are invoked for the following events:

  • migrationStarted: A partition migration is started.
  • migrationCompleted: A partition migration is completed.
  • migrationFailed: A partition migration failed.

To write a Migration Listener class, you implement the MigrationListener interface and its methods.

The following is an example Migration Listener class.

public class ClusterMigrationListener implements MigrationListener {
     @Override
     public void migrationStarted(MigrationEvent migrationEvent) {
       System.err.println("Started: " + migrationEvent);
     }
    @Override
     public void migrationCompleted(MigrationEvent migrationEvent) {
       System.err.println("Completed: " + migrationEvent);
     }
     @Override
     public void migrationFailed(MigrationEvent migrationEvent) {
       System.err.println("Failed: " + migrationEvent);
     }
}     

When a respective event is fired, the migration listener outputs the partition ID, status of the migration, the old member and the new member. The following is an example output.

Started: MigrationEvent{partitionId=98, oldOwner=Member [127.0.0.1]:5701,
newOwner=Member [127.0.0.1]:5702 this} 

Registering Migration Listeners

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

HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();

PartitionService partitionService = hazelcastInstance.getPartitionService();
partitionService.addMigrationListener( new ClusterMigrationListener );

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.ClusterMigrationListener" ) );

The following is an example of the equivalent declarative configuration.

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

The following is an example of the equivalent Spring configuration.

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