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);
}
}
}
You can look more into the Javadocs of the interfaces and methods I used to learn more about them