hazelcast Map eviction listener doesnot work after hazelcast4.0

21 views
Skip to first unread message

sandhya b

unread,
Nov 2, 2022, 10:46:01 AM11/2/22
to Hazelcast
Hi,

We have upgraded from Hazelcast 3 to Hazelcast 5.

The Eviction Listener doesnt work after the TTL for the given entry is expired. This Listener was working fine in Hazelcast 3.x.x and has stopped working form Hazelcast 4.0. We also do not see any Events getting logged in Hazelcast Mancenter, when the event is getting evicted. 

From Hazelcast4.0, the event gets removed after the TTL time but anything written in EvictionListener doesnt get picked up. Have attached the sample code to reproduce the issue along with the logs.

We have tried this with default Map configuration and different Eviction Policy and options, but for all the configuration, we see the same behavior.

Please can some one suggest if this functionality is removed or any specific configuration needs to be done to get it working.
Listen_hazelcast_5.1.0.txt
Listen_hazelcast_3.12.13.txt
Listen_hazelcast_4.1.3.txt

me...@hazelcast.com

unread,
Nov 2, 2022, 11:12:18 AM11/2/22
to Hazelcast
First, you have to call setTTL after you have put the data into the map because it will be a no-op if no such key exists in the first place.

Second, you should listen for expiration events, not eviction. So, a code like the below will work fine in the 5.x series:

public class Main {
public static void main( String[] args ) {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String, String> map = hz.getMap( "map" );
map.addEntryListener(new Listener(), true);

long now = System.nanoTime();
String key = "" + now;
String value = "1";
map.put(key, value);
map.setTtl(key, 6, TimeUnit.SECONDS);
}

private static class Listener implements EntryAddedListener<String, String>, EntryExpiredListener<String, String> {
@Override
public void entryAdded(EntryEvent<String, String> event) {
System.out.println("Entry added: " + event);
}

@Override
public void entryExpired(EntryEvent<String, String> event) {
System.out.println("Entry expired: " + event);
}
}
}

(Sorry for the formatting) 

You can look more into the Javadocs of the interfaces and methods I used to learn more about them

sandhya b

unread,
Nov 2, 2022, 12:52:03 PM11/2/22
to Hazelcast
Thanks this worked !!
Reply all
Reply to author
Forward
0 new messages