English 中文(简体)
Hazelcast - Collection Listener
  • 时间:2024-11-03

Hazelcast - Collection Listener


Previous Page Next Page  

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

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

Example

So, let’s first implement the Producer −


pubpc class SetTimedProducer{
   pubpc static void main(String... args) throws IOException,
   InterruptedException {
      //initiapze 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 pstener −


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;

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

We will first run the producer −


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

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


java -cp .	argetdemo-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(pstener, 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 seriapze and deseriapze the entry to make it accessible to the pstener.

Advertisements