Hazelcast provides various event listener extensions to receive specific event types. These are
MembershipListener for cluster membership events
InstanceListener for distributed instance creation and destroy events
MigrationListener for partition migration start and complete events
LifecycleListener for HazelcastInstance lifecycle events
EntryListener for IMap and MultiMap entry events
ItemListener for IQueue, ISet and IList item events
MessageListener for ITopic message events
These listeners can be added to and removed from related object using Hazelcast API. Such as
MembershipListener listener = new MyMembershipListener(); hazelcastInstance.getCluster().addMembershipListener(listener); hazelcastInstance.getCluster().removeMembershipListener(listener);
EntryListener listener = new MyEntryListener(); IMap map = hazelcastInstance.getMap("default"); map.addEntryListener(listener, true); map.removeEntryListener(listener);
ItemListener listener = new MyItemListener(); IQueue queue = hazelcastInstance.getQueue("default"); queue.addItemListener(listener, true); queue.removeItemListener(listener);
Downside of attaching listeners using API is possibility of missing events between creation of object and registering listener. To overcome this race condition Hazelcast introduces registration of listeners in configuration. Listeners can be registered using either Hazelcast XML configuration, Config API or Spring configuration.
MembershipListener
Hazelcast XML configuration
<listeners> <listener>com.hazelcast.examples.MembershipListener</listener> </listeners>
Config API
config.addListenerConfig(new ListenerConfig("com.hazelcast.examples.MembershipListener"));
Spring XML configuration
<hz:listeners> <hz:listener class-name="com.hazelcast.spring.DummyMembershipListener"/> <hz:listener implementation="dummyMembershipListener"/> </hz:listeners>
InstanceListener
Hazelcast XML configuration
<listeners> <listener>com.hazelcast.examples.InstanceListener</listener> </listeners>
Config API
config.addListenerConfig(new ListenerConfig("com.hazelcast.examples.InstanceListener"));
Spring XML configuration
<hz:listeners> <hz:listener class-name="com.hazelcast.spring.DummyInstanceListener"/> <hz:listener implementation="dummyInstanceListener"/> </hz:listeners>
MigrationListener
Hazelcast XML configuration
<listeners> <listener>com.hazelcast.examples.MigrationListener</listener> </listeners>
Config API
config.addListenerConfig(new ListenerConfig("com.hazelcast.examples.MigrationListener"));
Spring XML configuration
<hz:listeners> <hz:listener class-name="com.hazelcast.spring.DummyMigrationListener"/> <hz:listener implementation="dummyMigrationListener"/> </hz:listeners>
LifecycleListener
Hazelcast XML configuration
<listeners> <listener>com.hazelcast.examples.LifecycleListener</listener> </listeners>
Config API
config.addListenerConfig(new ListenerConfig("com.hazelcast.examples.LifecycleListener"));
Spring XML configuration
<hz:listeners> <hz:listener class-name="com.hazelcast.spring.DummyLifecycleListener"/> <hz:listener implementation="dummyLifecycleListener"/> </hz:listeners>
EntryListener for IMap
Hazelcast XML configuration
<map name="default"> ... <entry-listeners> <entry-listener include-value="true" local="false">com.hazelcast.examples.EntryListener</entry-listener> </entry-listeners> </map>
Config API
mapConfig.addEntryListenerConfig(new EntryListenerConfig("com.hazelcast.examples.EntryListener", false, false));
Spring XML configuration
<hz:map name="default"> <hz:entry-listeners> <hz:entry-listener class-name="com.hazelcast.spring.DummyEntryListener" include-value="true"/> <hz:entry-listener implementation="dummyEntryListener" local="true"/> </hz:entry-listeners> </hz:map>
EntryListener for MultiMap
Hazelcast XML configuration
<multimap name="default"> <value-collection-type>SET</value-collection-type> <entry-listeners> <entry-listener include-value="true" local="false">com.hazelcast.examples.EntryListener</entry-listener> </entry-listeners> </multimap>
Config API
multiMapConfig.addEntryListenerConfig(new EntryListenerConfig("com.hazelcast.examples.EntryListener", false, false));
Spring XML configuration
<hz:multimap name="default" value-collection-type="LIST"> <hz:entry-listeners> <hz:entry-listener class-name="com.hazelcast.spring.DummyEntryListener" include-value="true"/> <hz:entry-listener implementation="dummyEntryListener" local="true"/> </hz:entry-listeners> </hz:multimap>
ItemListener for IQueue
Hazelcast XML configuration
<queue name="default"> ... <item-listeners> <item-listener include-value="true">com.hazelcast.examples.ItemListener</item-listener> </item-listeners> </queue>
Config API
queueConfig.addItemListenerConfig(new ItemListenerConfig("com.hazelcast.examples.ItemListener", true));
Spring XML configuration
<hz:queue name="default" max-size-per-jvm="1000" backing-map-ref="default"> <hz:item-listeners> <hz:item-listener class-name="com.hazelcast.spring.DummyItemListener" include-value="true"/> </hz:item-listeners> </hz:queue>
MessageListener for ITopic
Hazelcast XML configuration
<topic name="default"> <message-listeners> <message-listener>com.hazelcast.examples.MessageListener</message-listener> </message-listeners> </topic>
Config API
topicConfig.addMessageListenerConfig(new ListenerConfig("com.hazelcast.examples.MessageListener"));
Spring XML configuration
<hz:topic name="default"> <hz:message-listeners> <hz:message-listener class-name="com.hazelcast.spring.DummyMessageListener"/> </hz:message-listeners> </hz:topic>