I'm running Hazelcast embedded in my JVM. I would like some simple logs that will log every time something changes in the cache. Something as simple as Cache changed: key = value
.
I could write these logs myself, but I would be surprised if this didn't already exist... However I can't seem to find the option.
Does this sort of logging already exist in Hazelcast?
There isn't one simple config like these change
event logging or handle another task. Hazelcast has EntryListener interfaces and you can implement according to which events needed. Here is the documentation.
You can try with following example code snippet.
Create a custom entryListener for what you need.
// Your custom entity listener.
@Slf4j
public class MyMapEventLogger implements EntryAddedListener<String, String>,
EntryRemovedListener<String, String>, EntryUpdatedListener<String, String>,
EntryEvictedListener<String, String>, MapEvictedListener, MapClearedListener {
@Override
public void entryAdded(EntryEvent<String, String> event) {
log.info("entryAdded: {}" , event);
}
@Override
public void entryRemoved(EntryEvent<String, String> event) {
log.info("entryRemoved: {}", event);
}
@Override
public void entryUpdated(EntryEvent<String, String> event) {
log.info("entryUpdated: {}", event);
}
@Override
public void entryEvicted(EntryEvent<String, String> event) {
log.info("entryEvicted: {}", event);
}
@Override
public void mapEvicted(MapEvent event) {
log.info("mapEvicted: {}", event);
}
@Override
public void mapCleared(MapEvent event) {
log.info("mapCleared: {}", event);
}
}
Bind your custom entryListener to yout map after created with addEntryListener
method.
public class ListenerSample {
public static void main(String[] args) {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String, String> map = hz.getMap("myMap");
MyMapEventLogger myMapEventLogger= new MyMapEventLogger();
map.addEntryListener(myMapEventLogger, true);
// Put, modify and then clear
map.put("1", "1");
map.put("1", "2");
map.clear();
}
}