Search in sources :

Example 1 with MapListener

use of com.hazelcast.map.listener.MapListener in project hazelcast by hazelcast.

the class MapListenerTest method main.

public static void main(String[] args) throws InterruptedException {
    // create Hazelcast instance
    Config config = new Config();
    config.setInstanceName("hz-maplistener");
    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
    config.getNetworkConfig().getInterfaces().setInterfaces(Arrays.asList(new String[] { "127.0.0.1" }));
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
    IMap<String, Person> map = hz.getMap("map");
    MapListener listener = new AllListener();
    map.addEntryListener(listener, new SqlPredicate("age > " + AGE_THRESHOLD), true);
    MapRandomizer mapRandomizer = new MapRandomizer(map);
    Thread t = new Thread(mapRandomizer);
    t.start();
    // let it run for 1 minute
    Thread.sleep(60000);
    mapRandomizer.setRunning(false);
    // assertions
    assertCount(ENTRIES, ENTRIES_OBSERVED, "entries");
    assertCount(EXITS, EXITS_OBSERVED, "exits");
    // dumpMap(map);
    hz.shutdown();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapListener(com.hazelcast.map.listener.MapListener) Config(com.hazelcast.config.Config) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 2 with MapListener

use of com.hazelcast.map.listener.MapListener in project hazelcast by hazelcast.

the class AbstractQueryCacheEndToEndConstructor method addListener.

private String addListener(QueryCacheRequest request) {
    MapListener listener = request.getListener();
    if (listener == null) {
        return null;
    }
    QueryCacheEventService eventService = subscriberContext.getEventService();
    return eventService.addListener(request.getMapName(), request.getCacheName(), listener);
}
Also used : QueryCacheEventService(com.hazelcast.map.impl.querycache.QueryCacheEventService) MapListener(com.hazelcast.map.listener.MapListener)

Example 3 with MapListener

use of com.hazelcast.map.listener.MapListener in project hazelcast by hazelcast.

the class AbstractQueryCacheConfigurator method setEntryListener.

protected void setEntryListener(String mapName, String cacheId, QueryCacheConfig config) {
    for (EntryListenerConfig listenerConfig : config.getEntryListenerConfigs()) {
        MapListener listener = getListener(listenerConfig);
        if (listener != null) {
            EventFilter filter = new EntryEventFilter(null, listenerConfig.isIncludeValue());
            eventService.addListener(mapName, cacheId, listener, filter);
        }
    }
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) EntryEventFilter(com.hazelcast.map.impl.EntryEventFilter) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) EntryEventFilter(com.hazelcast.map.impl.EntryEventFilter) EventFilter(com.hazelcast.spi.impl.eventservice.EventFilter)

Example 4 with MapListener

use of com.hazelcast.map.listener.MapListener in project hazelcast by hazelcast.

the class ListenerConfigHolder method asListenerConfig.

public <T extends ListenerConfig> T asListenerConfig(SerializationService serializationService) {
    validate();
    ListenerConfig listenerConfig = null;
    if (className != null) {
        switch(listenerType) {
            case GENERIC:
                listenerConfig = new ListenerConfig(className);
                break;
            case ITEM:
                listenerConfig = new ItemListenerConfig(className, includeValue);
                break;
            case ENTRY:
                listenerConfig = new EntryListenerConfig(className, local, includeValue);
                break;
            case SPLIT_BRAIN_PROTECTION:
                listenerConfig = new SplitBrainProtectionListenerConfig(className);
                break;
            case CACHE_PARTITION_LOST:
                listenerConfig = new CachePartitionLostListenerConfig(className);
                break;
            case MAP_PARTITION_LOST:
                listenerConfig = new MapPartitionLostListenerConfig(className);
                break;
            default:
        }
    } else {
        EventListener eventListener = serializationService.toObject(listenerImplementation);
        switch(listenerType) {
            case GENERIC:
                listenerConfig = new ListenerConfig(eventListener);
                break;
            case ITEM:
                listenerConfig = new ItemListenerConfig((ItemListener) eventListener, includeValue);
                break;
            case ENTRY:
                listenerConfig = new EntryListenerConfig((MapListener) eventListener, local, includeValue);
                break;
            case SPLIT_BRAIN_PROTECTION:
                listenerConfig = new SplitBrainProtectionListenerConfig((SplitBrainProtectionListener) eventListener);
                break;
            case CACHE_PARTITION_LOST:
                listenerConfig = new CachePartitionLostListenerConfig((CachePartitionLostListener) eventListener);
                break;
            case MAP_PARTITION_LOST:
                listenerConfig = new MapPartitionLostListenerConfig((MapPartitionLostListener) eventListener);
                break;
            default:
        }
    }
    return (T) listenerConfig;
}
Also used : SplitBrainProtectionListener(com.hazelcast.splitbrainprotection.SplitBrainProtectionListener) CachePartitionLostListener(com.hazelcast.cache.impl.event.CachePartitionLostListener) MapPartitionLostListenerConfig(com.hazelcast.config.MapPartitionLostListenerConfig) CachePartitionLostListenerConfig(com.hazelcast.config.CachePartitionLostListenerConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) CachePartitionLostListenerConfig(com.hazelcast.config.CachePartitionLostListenerConfig) ListenerConfig(com.hazelcast.config.ListenerConfig) SplitBrainProtectionListenerConfig(com.hazelcast.config.SplitBrainProtectionListenerConfig) MapPartitionLostListenerConfig(com.hazelcast.config.MapPartitionLostListenerConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) SplitBrainProtectionListenerConfig(com.hazelcast.config.SplitBrainProtectionListenerConfig) MapPartitionLostListener(com.hazelcast.map.listener.MapPartitionLostListener) MapListener(com.hazelcast.map.listener.MapListener) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) ItemListener(com.hazelcast.collection.ItemListener) EventListener(java.util.EventListener)

Example 5 with MapListener

use of com.hazelcast.map.listener.MapListener in project hazelcast by hazelcast.

the class AbstractMapNullTest method testNullability.

@Test
public void testNullability() {
    EntryAddedListener sampleMapListener = event -> {
    };
    Predicate<Object, Object> samplePredicate = Predicates.alwaysTrue();
    TimeUnit sampleTimeUnit = TimeUnit.SECONDS;
    assertThrowsNPE(m -> m.putAll(null));
    assertThrowsNPE(m -> m.containsKey(null));
    assertThrowsNPE(m -> m.containsValue(null));
    assertThrowsNPE(m -> m.get(null));
    assertThrowsNPE(m -> m.put(null, ""));
    assertThrowsNPE(m -> m.put("", null));
    assertThrowsNPE(m -> m.remove(null));
    assertThrowsNPE(m -> m.remove(null, ""));
    assertThrowsNPE(m -> m.remove("", null));
    assertThrowsNPE(m -> m.removeAll(null));
    assertThrowsNPE(m -> m.delete(null));
    assertThrowsNPE(m -> m.loadAll(null, true));
    assertThrowsNPE(m -> m.getAsync(null));
    assertThrowsNPE(m -> m.putAsync(null, ""));
    assertThrowsNPE(m -> m.putAsync("", null));
    assertThrowsNPE(m -> m.putAsync(null, "", -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putAsync("", null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putAsync("", "", -1, null));
    assertThrowsNPE(m -> m.putAsync(null, "", -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putAsync("", null, -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putAsync("", "", -1, null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putAsync("", "", -1, sampleTimeUnit, -1, null));
    assertThrowsNPE(m -> m.setAsync(null, ""));
    assertThrowsNPE(m -> m.setAsync("", null));
    assertThrowsNPE(m -> m.setAsync(null, "", -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.setAsync("", null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.setAsync("", "", -1, null));
    assertThrowsNPE(m -> m.setAsync(null, "", -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.setAsync("", null, -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.setAsync("", "", -1, null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.setAsync("", "", -1, sampleTimeUnit, -1, null));
    assertThrowsNPE(m -> m.tryRemove(null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.tryRemove("", -1, null));
    assertThrowsNPE(m -> m.tryPut(null, "", -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.tryPut("", null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.tryPut("", "", -1, null));
    assertThrowsNPE(m -> m.put(null, "", -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.put("", null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.put("", "", -1, null));
    assertThrowsNPE(m -> m.putTransient(null, "", -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putTransient("", null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putTransient("", "", -1, null));
    assertThrowsNPE(m -> m.putTransient(null, "", -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putTransient("", null, -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putTransient("", "", -1, null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putTransient("", "", -1, sampleTimeUnit, -1, null));
    assertThrowsNPE(m -> m.putIfAbsent(null, ""));
    assertThrowsNPE(m -> m.putIfAbsent("", null));
    assertThrowsNPE(m -> m.putIfAbsent(null, "", -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putIfAbsent("", null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putIfAbsent("", "", -1, null));
    assertThrowsNPE(m -> m.putIfAbsent(null, "", -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putIfAbsent("", null, -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putIfAbsent("", "", -1, null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.putIfAbsent("", "", -1, sampleTimeUnit, -1, null));
    assertThrowsNPE(m -> m.replace(null, "", ""));
    assertThrowsNPE(m -> m.replace("", null, ""));
    assertThrowsNPE(m -> m.replace("", "", null));
    assertThrowsNPE(m -> m.replace(null, ""));
    assertThrowsNPE(m -> m.replace("", null));
    assertThrowsNPE(m -> m.set(null, ""));
    assertThrowsNPE(m -> m.set("", null));
    assertThrowsNPE(m -> m.set(null, "", -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.set("", null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.set("", "", -1, null));
    assertThrowsNPE(m -> m.set(null, "", -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.set("", null, -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.set("", "", -1, null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.set("", "", -1, sampleTimeUnit, -1, null));
    assertThrowsNPE(m -> m.setAll(null));
    assertThrowsNPE(m -> m.setAllAsync(null));
    assertThrowsNPE(m -> m.lock(null));
    assertThrowsNPE(m -> m.lock(null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.tryLock(null));
    assertThrowsNPE(m -> m.tryLock(null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.tryLock(null, -1, sampleTimeUnit, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.unlock(null));
    assertThrowsNPE(m -> m.forceUnlock(null));
    if (isNotClient()) {
        assertThrowsNPE(m -> m.addLocalEntryListener((MapListener) null));
        assertThrowsNPE(m -> m.addLocalEntryListener((MapListener) null, samplePredicate, true));
        assertThrowsNPE(m -> m.addLocalEntryListener(sampleMapListener, null, true));
        assertThrowsNPE(m -> m.addLocalEntryListener(sampleMapListener, null, "", true));
        assertThrowsNPE(m -> m.localKeySet(null));
    }
    assertThrowsNPE(m -> m.addInterceptor(null));
    assertThrowsNPE(m -> m.removeInterceptor(null));
    assertThrowsNPE(m -> m.addEntryListener((MapListener) null, "", true));
    assertThrowsNPE(m -> m.addEntryListener((MapListener) null, (Object) null, true));
    assertThrowsNPE(m -> m.addEntryListener((MapListener) null, samplePredicate, true));
    assertThrowsNPE(m -> m.addEntryListener(sampleMapListener, null, true));
    assertThrowsNPE(m -> m.addEntryListener((MapListener) null, samplePredicate, "", true));
    assertThrowsNPE(m -> m.addEntryListener(sampleMapListener, null, "", true));
    assertThrowsNPE(m -> m.removeEntryListener(null));
    assertThrowsNPE(m -> m.addPartitionLostListener(null));
    assertThrowsNPE(m -> m.removePartitionLostListener(null));
    assertThrowsNPE(m -> m.getEntryView(null));
    assertThrowsNPE(m -> m.evict(null));
    assertThrowsNPE(m -> m.keySet(null));
    assertThrowsNPE(m -> m.entrySet(null));
    assertThrowsNPE(m -> m.values(null));
    assertThrows(NullPointerException.class, m -> m.addIndex(null, "attribute"));
    assertThrows(NullPointerException.class, m -> m.addIndex(IndexType.SORTED, null));
    assertThrows(NullPointerException.class, m -> m.addIndex(null));
    assertThrowsNPE(m -> m.aggregate(null));
    assertThrowsNPE(m -> m.aggregate(null, samplePredicate));
    assertThrowsNPE(m -> m.aggregate(new CountAggregator<>(), null));
    assertThrowsNPE(m -> m.project(null, samplePredicate));
    assertThrowsNPE(m -> m.project(Projections.identity(), null));
    assertThrowsNPE(m -> m.getQueryCache(null));
    assertThrowsNPE(m -> m.getQueryCache(null, samplePredicate, true));
    assertThrowsNPE(m -> m.getQueryCache("cache", null, true));
    assertThrowsNPE(m -> m.getQueryCache(null, sampleMapListener, samplePredicate, true));
    assertThrowsNPE(m -> m.getQueryCache("cache", null, samplePredicate, true));
    assertThrowsNPE(m -> m.getQueryCache("cache", sampleMapListener, null, true));
    assertThrowsNPE(m -> m.setTtl(null, -1, sampleTimeUnit));
    assertThrowsNPE(m -> m.setTtl("", -1, null));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastTestSupport(com.hazelcast.test.HazelcastTestSupport) ExceptionUtil(com.hazelcast.internal.util.ExceptionUtil) Test(org.junit.Test) EntryAddedListener(com.hazelcast.map.listener.EntryAddedListener) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MapListener(com.hazelcast.map.listener.MapListener) CountAggregator(com.hazelcast.aggregation.impl.CountAggregator) IndexType(com.hazelcast.config.IndexType) Predicates(com.hazelcast.query.Predicates) Assert.fail(org.junit.Assert.fail) Projections(com.hazelcast.projection.Projections) Assert(org.junit.Assert) Predicate(com.hazelcast.query.Predicate) MapListener(com.hazelcast.map.listener.MapListener) CountAggregator(com.hazelcast.aggregation.impl.CountAggregator) TimeUnit(java.util.concurrent.TimeUnit) EntryAddedListener(com.hazelcast.map.listener.EntryAddedListener) Test(org.junit.Test)

Aggregations

MapListener (com.hazelcast.map.listener.MapListener)33 Test (org.junit.Test)29 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)28 QuickTest (com.hazelcast.test.annotation.QuickTest)28 Predicate (com.hazelcast.query.Predicate)17 EntryEvent (com.hazelcast.core.EntryEvent)10 MapListenerAdapter (com.hazelcast.map.impl.MapListenerAdapter)10 AssertTask (com.hazelcast.test.AssertTask)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 CountAggregator (com.hazelcast.aggregation.impl.CountAggregator)1 CachePartitionLostListener (com.hazelcast.cache.impl.event.CachePartitionLostListener)1 ItemListener (com.hazelcast.collection.ItemListener)1 CachePartitionLostListenerConfig (com.hazelcast.config.CachePartitionLostListenerConfig)1 Config (com.hazelcast.config.Config)1 IndexType (com.hazelcast.config.IndexType)1 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)1 ListenerConfig (com.hazelcast.config.ListenerConfig)1 MapPartitionLostListenerConfig (com.hazelcast.config.MapPartitionLostListenerConfig)1