Time Based Eviction in Hazelcast

2019-08-20 22:56发布

问题:

I am working on a requirement where i'd have N hazelcast instances running in a cluster and also have kafka consumers running on all of them.

Now the ask is, each message that comes in on kafka, should be added to the distributed map and the entry must be evicted every 20 seconds, which i did by using a combination of time to live and max-idle seconds parameters in the map config.

But what i really want is that when the entry is evicted, only one of the nodes should process it, right now, the entry eviction is being informed to all the nodes.

Let me know if any more information is needed.

回答1:

You have to add a localEntryListener to your distributed map so that a member will only receive notifications for which it is an owner.

e.g.

if(map != null){
            map.addLocalEntryListener(new EntryAddedListener<Long, Long>() {
                @Override
                public void entryAdded(EntryEvent<Long, Long> event) {
                    log.info("LOCAL ENTRY ADDED : {} at {}", event, System.currentTimeMillis());
                }
            });

The above example is for the EntryAddedListener, you can similarly implement a EntryEvictedListener as well.



标签: hazelcast