use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.
the class ReplicatedMapEventPublishingService method dispatchEvent.
@Override
public void dispatchEvent(Object event, Object listener) {
if ((event instanceof EntryEventData)) {
EntryEventData entryEventData = (EntryEventData) event;
Member member = getMember(entryEventData);
EntryEvent entryEvent = createDataAwareEntryEvent(entryEventData, member);
EntryListener entryListener = (EntryListener) listener;
switch(entryEvent.getEventType()) {
case ADDED:
entryListener.entryAdded(entryEvent);
break;
case EVICTED:
entryListener.entryEvicted(entryEvent);
break;
case UPDATED:
entryListener.entryUpdated(entryEvent);
break;
case REMOVED:
entryListener.entryRemoved(entryEvent);
break;
default:
throw new IllegalArgumentException("event type " + entryEvent.getEventType() + " not supported");
}
String mapName = ((EntryEventData) event).getMapName();
Boolean statisticsEnabled = statisticsMap.get(mapName);
if (statisticsEnabled == null) {
ReplicatedMapConfig mapConfig = config.findReplicatedMapConfig(mapName);
statisticsEnabled = mapConfig.isStatisticsEnabled();
statisticsMap.put(mapName, statisticsEnabled);
}
if (statisticsEnabled) {
int partitionId = nodeEngine.getPartitionService().getPartitionId(entryEventData.getDataKey());
ReplicatedRecordStore recordStore = replicatedMapService.getPartitionContainer(partitionId).getRecordStore(mapName);
if (recordStore instanceof AbstractReplicatedRecordStore) {
LocalReplicatedMapStatsImpl stats = ((AbstractReplicatedRecordStore) recordStore).getStats();
stats.incrementReceivedEvents();
}
}
} else if (event instanceof MapEventData) {
MapEventData mapEventData = (MapEventData) event;
Member member = getMember(mapEventData);
MapEvent mapEvent = new MapEvent(mapEventData.getMapName(), member, mapEventData.getEventType(), mapEventData.getNumberOfEntries());
EntryListener entryListener = (EntryListener) listener;
EntryEventType type = EntryEventType.getByType(mapEventData.getEventType());
if (type == EntryEventType.CLEAR_ALL) {
entryListener.mapCleared(mapEvent);
} else {
throw new IllegalArgumentException("Unsupported EntryEventType: " + type);
}
}
}
use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.
the class ClientMapEvictAllTest method evictAll_firesOnlyOneEvent.
@Test
public void evictAll_firesOnlyOneEvent() throws Exception {
final String mapName = randomMapName();
final IMap<Object, Object> map = client.getMap(mapName);
final CountDownLatch eventCount = new CountDownLatch(2);
map.addEntryListener(new EntryAdapter<Object, Object>() {
@Override
public void mapEvicted(MapEvent event) {
eventCount.countDown();
}
}, true);
map.put(1, 1);
map.put(2, 1);
map.put(3, 1);
map.evictAll();
assertFalse(eventCount.await(10, TimeUnit.SECONDS));
assertEquals(1, eventCount.getCount());
}
use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.
the class ClientMapEvictAllTest method evictAll_firesEvent.
@Test
public void evictAll_firesEvent() throws Exception {
final String mapName = randomMapName();
final IMap<Object, Object> map = client.getMap(mapName);
final CountDownLatch evictedEntryCount = new CountDownLatch(3);
map.addEntryListener(new EntryAdapter<Object, Object>() {
@Override
public void mapEvicted(MapEvent event) {
final int affected = event.getNumberOfEntriesAffected();
for (int i = 0; i < affected; i++) {
evictedEntryCount.countDown();
}
}
}, true);
map.put(1, 1);
map.put(2, 1);
map.put(3, 1);
map.evictAll();
assertOpenEventually(evictedEntryCount);
assertEquals(0, map.size());
}
use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.
the class EntryAdapterTest method testEntryAdapterMapCleared.
@Test
public void testEntryAdapterMapCleared() {
String mapName = randomMapName();
Config cfg = new Config();
TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(1);
HazelcastInstance instance = instanceFactory.newHazelcastInstance(cfg);
IMap map = instance.getMap(mapName);
map.put(1, 1);
map.put(2, 2);
final CountDownLatch clearLatch = new CountDownLatch(1);
map.addEntryListener(new EntryAdapter() {
@Override
public void mapCleared(MapEvent event) {
clearLatch.countDown();
}
}, false);
map.clear();
assertOpenEventually(clearLatch);
}
use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.
the class EntryAdapterTest method testEntryAdapterMapEvicted.
@Test
public void testEntryAdapterMapEvicted() {
String mapName = randomMapName();
Config cfg = new Config();
TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(1);
HazelcastInstance instance = instanceFactory.newHazelcastInstance(cfg);
IMap map = instance.getMap(mapName);
map.put(1, 1);
map.put(2, 2);
final CountDownLatch evictionLatch = new CountDownLatch(1);
map.addEntryListener(new EntryAdapter() {
public void mapEvicted(final MapEvent event) {
evictionLatch.countDown();
}
}, false);
map.evictAll();
assertOpenEventually(evictionLatch);
}
Aggregations