Hazelcast – Collection Listener


Hazelcast – Collection Listener



”;


Hazelcast supports addition of listeners when a given collection, for example, queue, set, list, etc. is updated. Typical events include entry added and entry removed.

Let”s see how to implement a set listener via an example. So, let”s say we want to implement a listener which tracks the number of elements in a set.

Example

So, let’s first implement the Producer −


public class SetTimedProducer{
   public static void main(String... args) throws IOException,
   InterruptedException {
      //initialize hazelcast instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      Thread.sleep(5000);
      // create a set
      ISet<String> hzFruits = hazelcast.getSet("fruits");
      hzFruits.add("Mango");
      Thread.sleep(2000);
      hzFruits.add("Apple");
      Thread.sleep(2000);
      hzFruits.add("Banana");
      System.exit(0);
   }
}

Now let”s implement the listener −


package com.example.demo;

import java.io.IOException;
import com.hazelcast.core.ISet;
import com.hazelcast.core.ItemEvent;
import com.hazelcast.core.ItemListener;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

public class SetListener{
   public static void main(String... args) throws IOException, InterruptedException {
      //initialize hazelcast instance
      HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
      // create a set
      ISet<String> hzFruits = hazelcast.getSet("fruits");
      ItemListener<String> listener = new FruitListener<String>();
      hzFruits.addItemListener(listener, true);
      System.exit(0);
   }
   private static class FruitListener<String> implements ItemListener<String> {
      private int count = 0;
      @Override
      public void itemAdded(ItemEvent<String> item) {
         System.out.println("item added" + item);
         count ++;
         System.out.println("Total elements" + count);
      }
      @Override
      public void itemRemoved(ItemEvent<String> item) {
         count --;
      }
   }
}

We will first run the producer −


java -cp .targetdemo-0.0.1-SNAPSHOT.jar com.example.demo.SetTimedProducer

And then, we run the listeners and let it run indefinitely −


java -cp .targetdemo-0.0.1-SNAPSHOT.jar com.example.demo.SetListener

Output

The output from the Listener is as follows −


item added: ItemEvent{
   event=ADDED, item=Mango, member=Member [localhost]:5701-c28a60b7-3259-44bf-8793-54063d244394 this}
Total elements: 1

item added: ItemEvent{
   event=ADDED, item=Apple, member=Member [localhost]:5701-c28a60b7-3259-44bf-8793-54063d244394 this}
Total elements: 2

item added: ItemEvent{
   event=ADDED, item=Banana, member=Member [localhost]:5701-c28a60b7-3259-44bf-8793-54063d244394 this}
Total elements: 3

The call with hzFruits.addItemListener(listener, true) tells Hazelcast to provide member information. If set to false, we will just be notified that an entry was added/removed. This helps in avoiding the need to serialize and deserialize the entry to make it accessible to the listener.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *