Hazelcast 简明教程

Hazelcast - Collection Listener

Hazelcast 在给定集合(例如队列、集合、列表等)更新时支持添加监听器。典型事件包括添加条目和删除条目。

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 .\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.SetTimedProducer

然后,我们运行监听器并使其无限期运行 −

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

java -cp .\target\demo-0.0.1-SNAPSHOT.jar com.example.demo.SetListener

Output

监听器的 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

hzFruits.addItemListener(listener, true) 的调用告诉 Hazelcast 提供成员信息。如果设置为 false,则我们只会被通知条目已添加/删除。这有助于避免对条目进行序列化和反序列化以使其可用于监听器。

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.