30 Kasım 2022 Çarşamba

HazelcastAPI EntryListener Arayüzü - Kullanmayın

Giriş
Şu satırı dahil ederiz
import com.hazelcast.core.EntryListener;
Not : MapListener arayüzü tercih edilmeli. 

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 {
  @Override
  public void entryAdded(EntryEvent entryEvent) {
    logger.info("key {} and value {}",entryEvent.getKey(),entryEvent.getValue());
  }
  @Override
  public void entryEvicted(EntryEvent entryEvent) {
    logger.info("Map Entry was evicted : {}",entryEvent);
  }
  @Override
  public void entryRemoved(EntryEvent entryEvent) {
    logger.info("Object with key {} removed from map.",entryEvent.getKey());
  }
  @Override
  public void entryUpdated(EntryEvent entryEvent) {
    logger.info("key {} updated from {} to {}.", entryEvent.getKey(),
entryEvent.getOldValue(),entryEvent.getValue());
  }
  @Override
  public void mapCleared(MapEvent mapEvent) {
    logger.info("Map was cleared : {}",mapEvent);
  }
  @Override
  public void mapEvicted(MapEvent mapEvent) {
    logger.info("Map was evicted: {}",mapEvent);
  }
  @Override
  public void entryExpired(EntryEvent entryEvent) {
  }
}
IMap<String,String> hazelcastMap = ...;
hazelcastMap.addEntryListener(mapEntryListener,true);


Hiç yorum yok:

Yorum Gönder

THIRD-PARTY.txt Dosyası

Kullanılan harici kütüphanelerin sürümleri bu dosyada Dosyanın yolu şöyle hazelcast/licenses/THIRD-PARTY.txt