Search in sources :

Example 1 with EntryEvent

use of com.hazelcast.core.EntryEvent in project Openfire by igniterealtime.

the class ClusterListener method simulateCacheInserts.

private void simulateCacheInserts(Cache cache) {
    EntryListener EntryListener = EntryListeners.get(cache);
    if (EntryListener != null) {
        if (cache instanceof CacheWrapper) {
            Cache wrapped = ((CacheWrapper) cache).getWrappedCache();
            if (wrapped instanceof ClusteredCache) {
                ClusteredCache clusteredCache = (ClusteredCache) wrapped;
                for (Map.Entry entry : (Set<Map.Entry>) cache.entrySet()) {
                    EntryEvent event = new EntryEvent(clusteredCache.map.getName(), cluster.getLocalMember(), EntryEventType.ADDED.getType(), entry.getKey(), null, entry.getValue());
                    EntryListener.entryAdded(event);
                }
            }
        }
    }
}
Also used : CacheWrapper(org.jivesoftware.util.cache.CacheWrapper) HashSet(java.util.HashSet) Set(java.util.Set) EntryEvent(com.hazelcast.core.EntryEvent) EntryListener(com.hazelcast.core.EntryListener) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Cache(org.jivesoftware.util.cache.Cache)

Example 2 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class MultiMapListenerTest method testListeners.

@Test
public void testListeners() throws Exception {
    int count = 4;
    String name = randomMapName();
    Config config = new Config();
    config.getMultiMapConfig(name).setValueCollectionType(MultiMapConfig.ValueCollectionType.LIST);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(count);
    HazelcastInstance[] instances = factory.newInstances(config);
    final Set<String> keys = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
    EntryListener<String, String> listener = new EntryAdapter<String, String>() {

        public void entryAdded(EntryEvent<String, String> event) {
            keys.add(event.getKey());
        }

        public void entryRemoved(EntryEvent<String, String> event) {
            keys.remove(event.getKey());
        }

        @Override
        public void mapCleared(MapEvent event) {
            keys.clear();
        }
    };
    final MultiMap<String, String> multiMap = instances[0].getMultiMap(name);
    final String id = multiMap.addLocalEntryListener(listener);
    multiMap.put("key1", "val1");
    multiMap.put("key2", "val2");
    multiMap.put("key3", "val3");
    multiMap.put("key4", "val4");
    multiMap.put("key5", "val5");
    multiMap.put("key6", "val6");
    multiMap.put("key7", "val7");
    multiMap.put("key8", "val8");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertContainsAll(multiMap.localKeySet(), keys);
        }
    });
    if (keys.size() != 0) {
        multiMap.remove(keys.iterator().next());
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertContainsAll(multiMap.localKeySet(), keys);
        }
    });
    multiMap.removeEntryListener(id);
    getMultiMap(instances, name).clear();
    keys.clear();
    final String id2 = multiMap.addEntryListener(listener, true);
    getMultiMap(instances, name).put("key3", "val3");
    getMultiMap(instances, name).put("key3", "val33");
    getMultiMap(instances, name).put("key4", "val4");
    getMultiMap(instances, name).remove("key3", "val33");
    assertSizeEventually(1, keys);
    getMultiMap(instances, name).clear();
    assertSizeEventually(0, keys);
    multiMap.removeEntryListener(id2);
    multiMap.addEntryListener(listener, "key7", true);
    getMultiMap(instances, name).put("key2", "val2");
    getMultiMap(instances, name).put("key3", "val3");
    getMultiMap(instances, name).put("key7", "val7");
    assertSizeEventually(1, keys);
}
Also used : MultiMapConfig(com.hazelcast.config.MultiMapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) EntryAdapter(com.hazelcast.core.EntryAdapter) MapEvent(com.hazelcast.core.MapEvent) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class ClientEvictionTest method testMaxSizeEvictionWorks.

@Test
public void testMaxSizeEvictionWorks() throws Exception {
    final int maxSize = 1000;
    final int populationCount = 5000;
    String mapName = randomString();
    String cacheName = randomString();
    QueryCacheConfig cacheConfig = new QueryCacheConfig(cacheName);
    cacheConfig.getEvictionConfig().setSize(maxSize).setEvictionPolicy(EvictionPolicy.LFU).setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addQueryCacheConfig(mapName, cacheConfig);
    HazelcastInstance client = factory.newHazelcastClient(clientConfig);
    IMap<Integer, Integer> map = client.getMap(mapName);
    // expecting at least populationCount - maxSize + 10 evicted entries according to max size.
    // 10 states an error margin since eviction does not sweep precise number of entries.
    int margin = 10;
    final CountDownLatch evictedCount = new CountDownLatch(populationCount - maxSize - margin);
    final QueryCache<Integer, Integer> cache = map.getQueryCache(cacheName, TruePredicate.INSTANCE, true);
    String listener = cache.addEntryListener(new EntryEvictedListener() {

        @Override
        public void entryEvicted(EntryEvent event) {
            evictedCount.countDown();
        }
    }, false);
    for (int i = 0; i < populationCount; i++) {
        map.put(i, i);
    }
    assertOpenEventually(evictedCount);
    assertQueryCacheEvicted(maxSize, margin, cache);
    assertTrue(cache.removeEntryListener(listener));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) EntryEvent(com.hazelcast.core.EntryEvent) EntryEvictedListener(com.hazelcast.map.listener.EntryEvictedListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class ExpirationManagerTest method stops_running_when_clusterState_turns_passive.

@Test
public void stops_running_when_clusterState_turns_passive() throws Exception {
    Config config = new Config();
    config.setProperty(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, "1");
    HazelcastInstance node = createHazelcastInstance(config);
    final AtomicInteger expirationCounter = new AtomicInteger();
    IMap map = node.getMap("test");
    map.addEntryListener(new EntryExpiredListener() {

        @Override
        public void entryExpired(EntryEvent event) {
            expirationCounter.incrementAndGet();
        }
    }, true);
    map.put(1, 1, 3, TimeUnit.SECONDS);
    node.getCluster().changeClusterState(PASSIVE);
    // wait a little to see if any expiration is occurring
    sleepSeconds(3);
    int expirationCount = expirationCounter.get();
    assertEquals(format("Expecting no expiration but found:%d", expirationCount), 0, expirationCount);
}
Also used : IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryExpiredListener(com.hazelcast.map.listener.EntryExpiredListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(com.hazelcast.config.Config) EntryEvent(com.hazelcast.core.EntryEvent) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class ExpirationManagerTest method starts_running_when_clusterState_turns_active.

@Test
public void starts_running_when_clusterState_turns_active() throws Exception {
    Config config = new Config();
    config.setProperty(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, "1");
    HazelcastInstance node = createHazelcastInstance(config);
    final AtomicInteger expirationCounter = new AtomicInteger();
    IMap map = node.getMap("test");
    map.addEntryListener(new EntryExpiredListener() {

        @Override
        public void entryExpired(EntryEvent event) {
            expirationCounter.incrementAndGet();
        }
    }, true);
    map.put(1, 1, 3, SECONDS);
    node.getCluster().changeClusterState(PASSIVE);
    node.getCluster().changeClusterState(ACTIVE);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            int expirationCount = expirationCounter.get();
            assertEquals(format("Expecting 1 expiration but found:%d", expirationCount), 1, expirationCount);
        }
    });
}
Also used : IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryExpiredListener(com.hazelcast.map.listener.EntryExpiredListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(com.hazelcast.config.Config) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) ExpectedException(org.junit.rules.ExpectedException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

EntryEvent (com.hazelcast.core.EntryEvent)71 Test (org.junit.Test)62 QuickTest (com.hazelcast.test.annotation.QuickTest)47 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)39 EntryAdapter (com.hazelcast.core.EntryAdapter)22 HazelcastInstance (com.hazelcast.core.HazelcastInstance)22 Config (com.hazelcast.config.Config)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 AssertTask (com.hazelcast.test.AssertTask)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 MapListener (com.hazelcast.map.listener.MapListener)11 MapListenerAdapter (com.hazelcast.map.impl.MapListenerAdapter)10 MapConfig (com.hazelcast.config.MapConfig)9 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)8 Predicate (com.hazelcast.query.Predicate)8 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)8 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 EntryAddedListener (com.hazelcast.map.listener.EntryAddedListener)7 QueryCacheConfig (com.hazelcast.config.QueryCacheConfig)6