Search in sources :

Example 11 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project hazelcast by hazelcast.

the class CacheBasicAbstractTest method testRemovingSameEntryTwiceShouldTriggerEntryListenerOnlyOnce.

@Test
public void testRemovingSameEntryTwiceShouldTriggerEntryListenerOnlyOnce() {
    String cacheName = randomString();
    CacheConfig<Integer, String> config = createCacheConfig();
    final CacheFromDifferentNodesTest.SimpleEntryListener<Integer, String> listener = new CacheFromDifferentNodesTest.SimpleEntryListener<Integer, String>();
    MutableCacheEntryListenerConfiguration<Integer, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<Integer, String>(FactoryBuilder.factoryOf(listener), null, true, true);
    config.addCacheEntryListenerConfiguration(listenerConfiguration);
    Cache<Integer, String> cache = cacheManager.createCache(cacheName, config);
    assertNotNull(cache);
    Integer key = 1;
    String value = "value";
    cache.put(key, value);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.created.get());
        }
    });
    cache.removeAll();
    cache.removeAll();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.removed.get());
        }
    });
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) AssertTask(com.hazelcast.test.AssertTask) EntryProcessorException(javax.cache.processor.EntryProcessorException) NoSuchElementException(java.util.NoSuchElementException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 12 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project hazelcast by hazelcast.

the class CacheBasicAbstractTest method testCompletionEvent.

@Test
public void testCompletionEvent() {
    String cacheName = randomString();
    CacheConfig<Integer, String> config = createCacheConfig();
    final CacheFromDifferentNodesTest.SimpleEntryListener<Integer, String> listener = new CacheFromDifferentNodesTest.SimpleEntryListener<Integer, String>();
    MutableCacheEntryListenerConfiguration<Integer, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<Integer, String>(FactoryBuilder.factoryOf(listener), null, true, true);
    config.addCacheEntryListenerConfiguration(listenerConfiguration);
    Cache<Integer, String> cache = cacheManager.createCache(cacheName, config);
    assertNotNull(cache);
    Integer key1 = 1;
    String value1 = "value1";
    cache.put(key1, value1);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.created.get());
        }
    });
    Integer key2 = 2;
    String value2 = "value2";
    cache.put(key2, value2);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.created.get());
        }
    });
    Set<Integer> keys = new HashSet<Integer>();
    keys.add(key1);
    keys.add(key2);
    cache.removeAll(keys);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.removed.get());
        }
    });
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) EntryProcessorException(javax.cache.processor.EntryProcessorException) NoSuchElementException(java.util.NoSuchElementException) ExecutionException(java.util.concurrent.ExecutionException) AssertTask(com.hazelcast.test.AssertTask) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project hazelcast by hazelcast.

the class CacheFromDifferentNodesTest method testCompletionTestByPuttingAndRemovingFromDifferentNodes.

// Issue https://github.com/hazelcast/hazelcast/issues/5865
@Test
public void testCompletionTestByPuttingAndRemovingFromDifferentNodes() throws InterruptedException {
    String cacheName = "simpleCache";
    CacheManager cacheManager1 = cachingProvider1.getCacheManager();
    CacheManager cacheManager2 = cachingProvider2.getCacheManager();
    CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    final SimpleEntryListener<Integer, String> listener = new SimpleEntryListener<Integer, String>();
    MutableCacheEntryListenerConfiguration<Integer, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<Integer, String>(FactoryBuilder.factoryOf(listener), null, true, true);
    config.addCacheEntryListenerConfiguration(listenerConfiguration);
    Cache<Integer, String> cache1 = cacheManager1.createCache(cacheName, config);
    Cache<Integer, String> cache2 = cacheManager2.getCache(cacheName);
    assertNotNull(cache1);
    assertNotNull(cache2);
    Integer key1 = 1;
    String value1 = "value1";
    cache1.put(key1, value1);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.created.get());
        }
    });
    Integer key2 = 2;
    String value2 = "value2";
    cache1.put(key2, value2);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.created.get());
        }
    });
    Set<Integer> keys = new HashSet<Integer>();
    keys.add(key1);
    keys.add(key2);
    cache2.removeAll(keys);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.removed.get());
        }
    });
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) CacheConfig(com.hazelcast.config.CacheConfig) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 14 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project ignite by apache.

the class IgniteCacheEntryListenerExpiredEventsTest method checkExpiredEvents.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void checkExpiredEvents(CacheConfiguration<Object, Object> ccfg) throws Exception {
    IgniteCache<Object, Object> cache = ignite(0).createCache(ccfg);
    try {
        evtCntr = new AtomicInteger();
        CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new ExpiredListenerFactory(), null, true, false);
        cache.registerCacheEntryListener(lsnrCfg);
        IgniteCache<Object, Object> expiryCache = cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 500)));
        expiryCache.put(1, 1);
        for (int i = 0; i < 10; i++) cache.get(i);
        boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return evtCntr.get() > 0;
            }
        }, 5000);
        assertTrue(wait);
        U.sleep(100);
        assertEquals(1, evtCntr.get());
    } finally {
        ignite(0).destroyCache(cache.getName());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Duration(javax.cache.expiry.Duration) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Example 15 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project ignite by apache.

the class GridCacheReplicatedPreloadSelfTest method testExternalClassesAtConfiguration.

/**
     * @throws Exception If test failed.
     */
public void testExternalClassesAtConfiguration() throws Exception {
    try {
        extClassloadingAtCfg = true;
        useExtClassLoader = true;
        Ignite g1 = startGrid(1);
        // Checks deserialization at node join.
        Ignite g2 = startGrid(2);
        isClient = true;
        Ignite g3 = startGrid(3);
        IgniteCache<Integer, Object> cache1 = g1.cache(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Object> cache2 = g2.cache(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Object> cache3 = g3.cache(DEFAULT_CACHE_NAME);
        final Class<CacheEntryListener> cls1 = (Class<CacheEntryListener>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentCacheEntryListener");
        final Class<CacheEntryEventSerializableFilter> cls2 = (Class<CacheEntryEventSerializableFilter>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentCacheEntryEventSerializableFilter");
        CacheEntryListenerConfiguration<Integer, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Integer, Object>>() {

            @Override
            public CacheEntryListener<Integer, Object> create() {
                try {
                    return cls1.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }, new ClassFilterFactory(cls2), true, true);
        cache1.registerCacheEntryListener(lsnrCfg);
        cache1.put(1, 1);
        assertEquals(1, cache2.get(1));
        assertEquals(1, cache3.get(1));
    } finally {
        extClassloadingAtCfg = false;
        isClient = false;
        useExtClassLoader = false;
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryListener(javax.cache.event.CacheEntryListener) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject)

Aggregations

MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)15 Test (org.junit.Test)7 AssertTask (com.hazelcast.test.AssertTask)5 CacheConfig (com.hazelcast.config.CacheConfig)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 CacheManager (javax.cache.CacheManager)4 Duration (javax.cache.expiry.Duration)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CacheEntryListener (javax.cache.event.CacheEntryListener)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 ModifiedExpiryPolicy (javax.cache.expiry.ModifiedExpiryPolicy)3 CachingProvider (javax.cache.spi.CachingProvider)3 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)2 URI (java.net.URI)2 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2