Hazelcast JCache CacheEntryListener过于频繁地触发

时间:2015-12-08 16:43:57

标签: java listener hazelcast jcache

在我看来,我遇到了JCache EntryListener的回调机制问题。我正在尝试设置几个jcache成员(hazelcast实现),每个成员在我们的本地计算机上作为单个java应用程序运行(现在在Ecipse中为每个节点手动启动主程序)。

节点在开头<Long, SpecificDataType>只保留一个空缓存。使用默认的hazelcast-default.xml,我自己以编程方式准备了一个最小配置,主要是注册一个CacheEntryListener,它为创建/更新/删除/过期的条目生成一个简单的sysout。当我启动几个(三个或更多)成员时,我希望每个成员只打印一次修改后的键/值对,其中包含条目(作为操作或备份条目)。问题是,在某些情况下,sysout会出现多次,在我看来,监听器被频繁触发(不止一次)。

例如,在一个简单的客户端(只是获取缓存并将带有密钥123689的条目放入缓存中)创建一些条目时,sysout“CREATE EVENT RECEIVED”在第三个中显示4次成员,甚至更常见于第四......等等。

CREATE EVENT RECEIVED:
Key: 123689, Value:SpecificDataType[...]

CREATE EVENT RECEIVED:
Key: 123689, Value:SpecificDataType[...]

CREATE EVENT RECEIVED: 
Key: 123689, Value:SpecificDataType[...]

似乎听众以某种方式取代......我做错了什么?我是否会错过任何有关配置的内容?

代码:

public static void main(String[] args) {
    Member member = Member.getInstance();

    try {
        Cache<Long, SpecificDataType> cache = member.getCache("SpecificDataTypeCache", SpecificDataType.class);
        System.out.println(cache);
    } catch (Exception e) {
        e.printStackTrace();

    }

    // shutdown loop
    boolean shutdown = false;
    while (!shutdown) {
        if (new Scanner(System.in).nextLine().equals("shutdown")) {
            shutdown = true;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    // cache.clear();
    member.shutdown();

}

public <T> Cache<Long, T> getCache(String name, Class<T> clazz) {

    // configure the cache
    MutableConfiguration<Long, T> config = new MutableConfiguration<Long, T>();
    config.setStoreByValue(true).setTypes(Long.class, clazz)
            .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(TEN_SEC))
            .setStatisticsEnabled(false);

    // create / get cache
    Cache<Long, T> cache = cacheManager.getCache(name, Long.class, clazz);
    if (cache == null) {
        System.out.println("create cache");
        cache = cacheManager.createCache(name, config);
        // create the EntryListener
        MyCacheEntryListener<Long, T> clientListener = new MyCacheEntryListener<Long, T>();

        // using out listener, lets create a configuration
        CacheEntryListenerConfiguration<Long, T> conf = new MutableCacheEntryListenerConfiguration<Long, T>(
                FactoryBuilder.factoryOf(clientListener), null, false, true);

        // register to cache
        cache.registerCacheEntryListener(conf);
    } else {
        System.out.println("get cache");
    }

    return cache;
}

听众尽可能简单:

public class MyCacheEntryListener<K, V> implements CacheEntryCreatedListener<K, V>,
    CacheEntryUpdatedListener<K, V>, CacheEntryExpiredListener<K, V>,
    CacheEntryRemovedListener<K, V>, Serializable {


@Override
public void onCreated(Iterable<CacheEntryEvent<? extends K, ? extends V>> cacheEntryEvents)
        throws CacheEntryListenerException {
    System.out.println("CREATE EVENT RECEIVED: ");
    System.out.println(printEvent(cacheEntryEvents));
}


@Override
public void onExpired(Iterable<CacheEntryEvent<? extends K, ? extends V>> cacheEntryEvents)
        throws CacheEntryListenerException {
    System.out.println("EXPIRE EVENT RECEIVED: ");
    System.out.println(printEvent(cacheEntryEvents));
}


@Override
public void onRemoved(Iterable<CacheEntryEvent<? extends K, ? extends V>> cacheEntryEvents)
        throws CacheEntryListenerException {
    System.out.println("REMOVE EVENT RECEIVED: ");
    System.out.println(printEvent(cacheEntryEvents));
}


@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends K, ? extends V>> cacheEntryEvents)
        throws CacheEntryListenerException {
    System.out.println("UPDATE EVENT RECEIVED: ");
    System.out.println(printEvent(cacheEntryEvents));
}


private String printEvent(Iterable<CacheEntryEvent<? extends K, ? extends V>> cacheEntryEvents) {
    StringBuilder sb = new StringBuilder();
    final Iterator<CacheEntryEvent<? extends K, ? extends V>> iterator = cacheEntryEvents
            .iterator();
    while (iterator.hasNext()) {
        final CacheEntryEvent<? extends K, ? extends V> next = iterator.next();
        sb.append("Key: ");
        sb.append(next.getKey());
        sb.append(", Value:");
        sb.append(next.getValue());
        sb.append("\n");
    }
    return sb.toString();
}

}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

我已经尝试了4个实例和3个备份计数的样本,并且只按预期通知了一次监听器。

  • 您确定多次注册不同的侦听器吗?
  • 您可以在MyCacheEntryListener消息之后打印CREATE EVENT RECEIVED吗?
  • 您使用哪种Hazelcast版本?

问候。