Şu satırı dahil ederiz
import com.hazelcast.core.EntryListener;
EntryListener bir sürü başka arayüzden oluşuyor. Kod şöyle
/**
* Map Entry listener to get notified when a map entry is added,
* removed, updated, evicted or expired. Events will fire as a result
* of operations carried out via the IMap
* interface. Events will not fire, for example, for an entry
* that comes into the Map via the MapLoader lifecycle.
*
* This interface is here for backward compatibility reasons.
* For a most appropriate alternative please use/check
* com.hazelcast.map.listener.MapListener interface.
*
* @param <K> the type of key.
* @param <V> the type of value.
* @see IMap#addEntryListener
* @see com.hazelcast.map.listener.MapListener
*/
public interface EntryListener<K, V>
extends EntryAddedListener<K, V>,
EntryUpdatedListener<K, V>,
EntryRemovedListener<K, V>,
EntryEvictedListener<K, V>,
EntryExpiredListener<K, V>,
MapClearedListener,
MapEvictedListener {
}
Not : EntryListener arayüzündeki bir sürü metodu tek bir metoda yönlendiren ve kullanması daha basit olan EntryAdapter tercih edilebilir.
Örnek
EntryListener yerine kalıttığı arayüzleri teker teker kullanmak için şöyle yaparız
map.addEntryListener((EntryExpiredListener<Integer, Integer>) event -> {...}, 1, false);
map.addEntryListener((EntryEvictedListener<Integer, Integer>) event -> {...}, 2, false);
Örnek
Şöyle yaparız
import com.hazelcast.core.EntryEvent;import com.hazelcast.core.EntryListener;import com.hazelcast.map.MapEvent;public class MapEntryListener implements EntryListener {@Overridepublic void entryAdded(EntryEvent entryEvent) {logger.info("key {} and value {}",entryEvent.getKey(),entryEvent.getValue());}@Overridepublic void entryEvicted(EntryEvent entryEvent) {logger.info("Map Entry was evicted : {}",entryEvent);}@Overridepublic void entryRemoved(EntryEvent entryEvent) {logger.info("Object with key {} removed from map.",entryEvent.getKey());}@Overridepublic void entryUpdated(EntryEvent entryEvent) {logger.info("key {} updated from {} to {}.", entryEvent.getKey(),
entryEvent.getOldValue(),entryEvent.getValue());}@Overridepublic void mapCleared(MapEvent mapEvent) {logger.info("Map was cleared : {}",mapEvent);}@Overridepublic void mapEvicted(MapEvent mapEvent) {logger.info("Map was evicted: {}",mapEvent);}@Overridepublic void entryExpired(EntryEvent entryEvent) {}}IMap<String,String> hazelcastMap = ...;hazelcastMap.addEntryListener(mapEntryListener,true);
Hiç yorum yok:
Yorum Gönder