Search in sources :

Example 1 with MapEvent

use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.

the class EventPublisherHelper method createIMapEvent.

public static IMapEvent createIMapEvent(EventData eventData, EventFilter filter, Member member, SerializationService serializationService) {
    String source = eventData.getSource();
    int eventType = eventData.getEventType();
    if (eventType == EventLostEvent.EVENT_TYPE) {
        LocalEntryEventData localEventData = (LocalEntryEventData) eventData;
        int partitionId = localEventData.getPartitionId();
        return new EventLostEvent(source, null, partitionId);
    }
    if (eventType == EntryEventType.CLEAR_ALL.getType() || eventType == EntryEventType.EVICT_ALL.getType()) {
        LocalCacheWideEventData localCacheWideEventData = (LocalCacheWideEventData) eventData;
        int numberOfEntriesAffected = localCacheWideEventData.getNumberOfEntriesAffected();
        return new MapEvent(source, null, eventType, numberOfEntriesAffected);
    }
    LocalEntryEventData localEntryEventData = (LocalEntryEventData) eventData;
    Data dataKey = localEntryEventData.getKeyData();
    Data dataNewValue = localEntryEventData.getValueData();
    Data dataOldValue = localEntryEventData.getOldValueData();
    boolean includeValue = isIncludeValue(filter);
    return new DataAwareEntryEvent(member, eventType, source, dataKey, (includeValue ? dataNewValue : null), (includeValue ? dataOldValue : null), null, serializationService);
}
Also used : LocalEntryEventData(com.hazelcast.map.impl.querycache.event.LocalEntryEventData) IMapEvent(com.hazelcast.map.IMapEvent) MapEvent(com.hazelcast.map.MapEvent) EventLostEvent(com.hazelcast.map.EventLostEvent) Data(com.hazelcast.internal.serialization.Data) LocalCacheWideEventData(com.hazelcast.map.impl.querycache.event.LocalCacheWideEventData) LocalEntryEventData(com.hazelcast.map.impl.querycache.event.LocalEntryEventData) EventData(com.hazelcast.map.impl.event.EventData) DataAwareEntryEvent(com.hazelcast.map.impl.DataAwareEntryEvent) LocalCacheWideEventData(com.hazelcast.map.impl.querycache.event.LocalCacheWideEventData)

Example 2 with MapEvent

use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.

the class MultiMapEventsDispatcher method dispatchMapEventData.

private void dispatchMapEventData(EventData eventData, EntryListener listener) {
    MapEventData mapEventData = (MapEventData) eventData;
    Member member = getMemberOrNull(eventData);
    if (member == null) {
        return;
    }
    MapEvent event = createMapEvent(mapEventData, member);
    dispatch0(event, listener);
    incrementEventStats(event);
}
Also used : IMapEvent(com.hazelcast.map.IMapEvent) MapEvent(com.hazelcast.map.MapEvent) MapEventData(com.hazelcast.map.impl.event.MapEventData) Member(com.hazelcast.cluster.Member)

Example 3 with MapEvent

use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.

the class MapEventPublishingService method dispatchMapEventData.

private void dispatchMapEventData(MapEventData mapEventData, ListenerAdapter listener) {
    Member member = getMember(mapEventData);
    MapEvent event = createMapEvent(mapEventData, member);
    callListener(listener, event);
}
Also used : EventPublisherHelper.createIMapEvent(com.hazelcast.map.impl.querycache.subscriber.EventPublisherHelper.createIMapEvent) BatchIMapEvent(com.hazelcast.map.impl.querycache.event.BatchIMapEvent) IMapEvent(com.hazelcast.map.IMapEvent) SingleIMapEvent(com.hazelcast.map.impl.querycache.event.SingleIMapEvent) MapEvent(com.hazelcast.map.MapEvent) Member(com.hazelcast.cluster.Member)

Example 4 with MapEvent

use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.

the class QueryCacheListenerTest method listenerShouldReceive_CLEAR_ALL_Event_whenIMapCleared.

@Test
public void listenerShouldReceive_CLEAR_ALL_Event_whenIMapCleared() {
    IMap<Integer, Employee> map = getIMapWithDefaultConfig(TRUE_PREDICATE, useNaturalFilteringStrategy);
    int entryCount = 1000;
    final AtomicInteger clearAllEventCount = new AtomicInteger();
    final QueryCache<Integer, Employee> queryCache = map.getQueryCache(cacheName, new EntryAdapter() {

        @Override
        public void mapCleared(MapEvent e) {
            clearAllEventCount.incrementAndGet();
        }
    }, TRUE_PREDICATE, false);
    populateMap(map, entryCount);
    assertQueryCacheSizeEventually(entryCount, queryCache);
    map.clear();
    assertTrueEventually(() -> {
        // expecting at least 1 event
        assertTrue(clearAllEventCount.get() >= 1);
        assertEquals(0, queryCache.size());
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Employee(com.hazelcast.map.impl.querycache.utils.Employee) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryAdapter(com.hazelcast.core.EntryAdapter) MapEvent(com.hazelcast.map.MapEvent) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with MapEvent

use of com.hazelcast.map.MapEvent in project hazelcast by hazelcast.

the class MultiMapListenerTest method testMultiMapEntryListener.

@Test
public void testMultiMapEntryListener() {
    HazelcastInstance instance = createHazelcastInstance();
    MultiMap<String, String> multiMap = instance.getMultiMap("testMultiMapEntryListener");
    final Set<String> expectedValues = new CopyOnWriteArraySet<String>();
    expectedValues.add("hello");
    expectedValues.add("world");
    expectedValues.add("again");
    final CountDownLatch latchAdded = new CountDownLatch(3);
    final CountDownLatch latchRemoved = new CountDownLatch(1);
    final CountDownLatch latchCleared = new CountDownLatch(1);
    multiMap.addEntryListener(new EntryAdapter<String, String>() {

        public void entryAdded(EntryEvent<String, String> event) {
            String key = event.getKey();
            String value = event.getValue();
            if ("2".equals(key)) {
                assertEquals("again", value);
            } else {
                assertEquals("1", key);
            }
            assertContains(expectedValues, value);
            expectedValues.remove(value);
            latchAdded.countDown();
        }

        public void entryRemoved(EntryEvent<String, String> event) {
            assertEquals("2", event.getKey());
            assertEquals("again", event.getOldValue());
            latchRemoved.countDown();
        }

        public void entryUpdated(EntryEvent<String, String> event) {
            throw new AssertionError("MultiMap cannot get update event!");
        }

        public void entryEvicted(EntryEvent<String, String> event) {
            entryRemoved(event);
        }

        @Override
        public void mapEvicted(MapEvent event) {
        }

        @Override
        public void mapCleared(MapEvent event) {
            latchCleared.countDown();
        }
    }, true);
    multiMap.put("1", "hello");
    multiMap.put("1", "world");
    multiMap.put("2", "again");
    Collection<String> values = multiMap.get("1");
    assertEquals(2, values.size());
    assertContains(values, "hello");
    assertContains(values, "world");
    assertEquals(1, multiMap.get("2").size());
    assertEquals(3, multiMap.size());
    multiMap.remove("2");
    assertEquals(2, multiMap.size());
    multiMap.clear();
    assertOpenEventually(latchAdded);
    assertOpenEventually(latchRemoved);
    assertOpenEventually(latchCleared);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapEvent(com.hazelcast.map.MapEvent) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) CountDownLatch(java.util.concurrent.CountDownLatch) 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