Listener Configurations

You can add or remove event listeners to/from the related object using the Hazelcast API.

The downside of attaching listeners using this API is the possibility of missing events between the creation of an object and registering the listener. To overcome this race condition, Hazelcast allows you to register listeners in configuration. You can register listeners using declarative, programmatic, or Spring configuration.

MembershipListener

  • Declarative Configuration
<listeners>
  <listener>com.hazelcast.examples.MembershipListener</listener>
</listeners>
  • Programmatic Configuration
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>

DistributedObjectListener

  • Declarative Configuration
<listeners>
  <listener>com.hazelcast.examples.DistributedObjectListener</listener>
</listeners>
  • Programmatic Configuration
config.addListenerConfig(
new ListenerConfig( "com.hazelcast.examples.DistributedObjectListener" ) );
  • Spring XML configuration
<hz:listeners>
  <hz:listener class-name="com.hazelcast.spring.DummyDistributedObjectListener"/>
  <hz:listener implementation="dummyDistributedObjectListener"/>
</hz:listeners>

MigrationListener

  • Declarative Configuration
<listeners>
  <listener>com.hazelcast.examples.MigrationListener</listener>
</listeners>
  • Programmatic Configuration
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

  • Declarative Configuration
<listeners>
  <listener>com.hazelcast.examples.LifecycleListener</listener>
</listeners>
  • Programmatic Configuration
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

  • Declarative Configuration
<map name="default">
  ...
  <entry-listeners>
    <entry-listener include-value="true" local="false">
    com.hazelcast.examples.EntryListener
    </entry-listener>
  </entry-listeners>
</map>
  • Programmatic Configuration
mapConfig.addEntryListenerConfig(
new EntryListenerConfig( "com.hazelcast.examples.EntryListener", false, false ) );
  • Spring XML configuration
<hz:map name="default">
  <hz:entry-listeners>
    <hz:entry-listener include-value="true"
    class-name="com.hazelcast.spring.DummyEntryListener"/>
    <hz:entry-listener implementation="dummyEntryListener" local="true"/>
  </hz:entry-listeners>
</hz:map>

EntryListener for MultiMap

  • Declarative 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>
  • Programmatic Configuration
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 include-value="true"
    class-name="com.hazelcast.spring.DummyEntryListener"/>
    <hz:entry-listener implementation="dummyEntryListener" local="true"/>
  </hz:entry-listeners>
</hz:multimap>

ItemListener for IQueue

  • Declarative Configuration
<queue name="default">
  ...
  <item-listeners>
     <item-listener include-value="true">
       com.hazelcast.examples.ItemListener
     </item-listener>
  </item-listeners>
</queue>
  • Programmatic Configuration
queueConfig.addItemListenerConfig(
new ItemListenerConfig( "com.hazelcast.examples.ItemListener", true ) );
  • Spring XML configuration
<hz:queue name="default" >
  <hz:item-listeners>
    <hz:item-listener include-value="true"
      class-name="com.hazelcast.spring.DummyItemListener"/>
  </hz:item-listeners>
</hz:queue>

MessageListener for ITopic

  • Declarative Configuration
<topic name="default">
  <message-listeners>
    <message-listener>
     com.hazelcast.examples.MessageListener
    </message-listener>
  </message-listeners>
</topic>
  • Programmatic Configuration
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>

ClientListener

  • Declarative Configuration
<listeners>
  <listener>com.hazelcast.examples.ClientListener</listener>
</listeners>
  • Programmatic Configuration
topicConfig.addMessageListenerConfig(
new ListenerConfig( "com.hazelcast.examples.ClientListener" ) );
  • Spring XML configuration
<hz:listeners>
 <hz:listener class-name="com.hazelcast.spring.DummyClientListener"/>
 <hz:listener implementation="dummyClientListener"/>
</hz:listeners>