Search in sources :

Example 6 with MapEvent

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);
        }
    }
}
Also used : AbstractReplicatedRecordStore(com.hazelcast.replicatedmap.impl.record.AbstractReplicatedRecordStore) MapEvent(com.hazelcast.map.MapEvent) MapEventData(com.hazelcast.map.impl.event.MapEventData) EntryListener(com.hazelcast.core.EntryListener) EntryEventType(com.hazelcast.core.EntryEventType) LocalReplicatedMapStatsImpl(com.hazelcast.internal.monitor.impl.LocalReplicatedMapStatsImpl) AbstractReplicatedRecordStore(com.hazelcast.replicatedmap.impl.record.AbstractReplicatedRecordStore) ReplicatedRecordStore(com.hazelcast.replicatedmap.impl.record.ReplicatedRecordStore) DataAwareEntryEvent(com.hazelcast.map.impl.DataAwareEntryEvent) EntryEvent(com.hazelcast.core.EntryEvent) ReplicatedMapConfig(com.hazelcast.config.ReplicatedMapConfig) EntryEventData(com.hazelcast.map.impl.event.EntryEventData) Member(com.hazelcast.cluster.Member)

Example 7 with MapEvent

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());
}
Also used : MapEvent(com.hazelcast.map.MapEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with MapEvent

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());
}
Also used : MapEvent(com.hazelcast.map.MapEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with MapEvent

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);
}
Also used : IMap(com.hazelcast.map.IMap) Config(com.hazelcast.config.Config) MapEvent(com.hazelcast.map.MapEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with MapEvent

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);
}
Also used : IMap(com.hazelcast.map.IMap) Config(com.hazelcast.config.Config) MapEvent(com.hazelcast.map.MapEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

MapEvent (com.hazelcast.map.MapEvent)11 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)7 QuickTest (com.hazelcast.test.annotation.QuickTest)7 Test (org.junit.Test)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 Member (com.hazelcast.cluster.Member)4 IMapEvent (com.hazelcast.map.IMapEvent)3 Config (com.hazelcast.config.Config)2 IMap (com.hazelcast.map.IMap)2 DataAwareEntryEvent (com.hazelcast.map.impl.DataAwareEntryEvent)2 MapEventData (com.hazelcast.map.impl.event.MapEventData)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 ReplicatedMapConfig (com.hazelcast.config.ReplicatedMapConfig)1 EntryAdapter (com.hazelcast.core.EntryAdapter)1 EntryEvent (com.hazelcast.core.EntryEvent)1 EntryEventType (com.hazelcast.core.EntryEventType)1 EntryListener (com.hazelcast.core.EntryListener)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 LocalReplicatedMapStatsImpl (com.hazelcast.internal.monitor.impl.LocalReplicatedMapStatsImpl)1 Data (com.hazelcast.internal.serialization.Data)1