Search in sources :

Example 6 with MutableCacheEntryListenerConfiguration

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

the class CacheContinuousQueryFactoryFilterRandomOperationTest method doTestContinuousQuery.

/**
 * {@inheritDoc}
 */
@Override
protected void doTestContinuousQuery(CacheConfiguration<Object, Object> ccfg, ContinuousDeploy deploy) throws Exception {
    ignite(0).createCache(ccfg);
    try {
        long seed = System.currentTimeMillis();
        Random rnd = new Random(seed);
        log.info("Random seed: " + seed);
        List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues = new ArrayList<>();
        Collection<QueryCursor<?>> curs = new ArrayList<>();
        Collection<T2<Integer, MutableCacheEntryListenerConfiguration>> lsnrCfgs = new ArrayList<>();
        if (deploy == CLIENT)
            evtsQueues.add(registerListener(ccfg.getName(), NODES - 1, curs, lsnrCfgs, rnd.nextBoolean()));
        else if (deploy == SERVER)
            evtsQueues.add(registerListener(ccfg.getName(), rnd.nextInt(NODES - 1), curs, lsnrCfgs, rnd.nextBoolean()));
        else {
            boolean isSync = rnd.nextBoolean();
            for (int i = 0; i < NODES - 1; i++) evtsQueues.add(registerListener(ccfg.getName(), i, curs, lsnrCfgs, isSync));
        }
        ConcurrentMap<Object, Object> expData = new ConcurrentHashMap<>();
        Map<Integer, Long> partCntr = new ConcurrentHashMap<>();
        try {
            for (int i = 0; i < ITERATION_CNT; i++) {
                if (i % 10 == 0)
                    log.info("Iteration: " + i);
                for (int idx = 0; idx < NODES; idx++) randomUpdate(rnd, evtsQueues, expData, partCntr, grid(idx).cache(ccfg.getName()));
            }
        } finally {
            for (QueryCursor<?> cur : curs) cur.close();
            for (T2<Integer, MutableCacheEntryListenerConfiguration> e : lsnrCfgs) grid(e.get1()).cache(ccfg.getName()).deregisterCacheEntryListener(e.get2());
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) ArrayList(java.util.ArrayList) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryCursor(org.apache.ignite.cache.query.QueryCursor) T2(org.apache.ignite.internal.util.typedef.T2)

Example 7 with MutableCacheEntryListenerConfiguration

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

the class CacheContinuousQueryOperationP2PTest method testContinuousQuery.

/**
 * @param ccfg Cache configuration.
 * @param isClient Client.
 * @throws Exception If failed.
 */
protected void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    QueryCursor<?> cur = null;
    final Class<Factory<CacheEntryEventFilter>> evtFilterFactory = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    final CountDownLatch latch = new CountDownLatch(10);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    TestLocalListener localLsnr = new TestLocalListener() {

        @Override
        public void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                latch.countDown();
                log.info("Received event: " + evt);
            }
        }
    };
    MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(localLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactory.newInstance(), true, true);
    qry.setLocalListener(localLsnr);
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactory.newInstance());
    IgniteCache<Integer, Integer> cache = null;
    try {
        if (isClient)
            cache = grid(NODES - 1).cache(ccfg.getName());
        else
            cache = grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
        cur = cache.query(qry);
        cache.registerCacheEntryListener(lsnrCfg);
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertTrue(latch.await(3, TimeUnit.SECONDS));
    } finally {
        if (cur != null)
            cur.close();
        if (cache != null)
            cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 8 with MutableCacheEntryListenerConfiguration

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

the class IgniteCacheEntryListenerAbstractTest method testConcurrentRegisterDeregister.

/**
 * @throws Exception If failed.
 */
public void testConcurrentRegisterDeregister() throws Exception {
    final int THREADS = 10;
    final CyclicBarrier barrier = new CyclicBarrier(THREADS);
    final IgniteCache<Object, Object> cache = jcache(0);
    GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            CacheEntryListenerConfiguration<Object, Object> cfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Object, Object>>() {

                @Override
                public CacheEntryListener<Object, Object> create() {
                    return new CreateUpdateRemoveExpireListener();
                }
            }, null, true, false);
            barrier.await();
            for (int i = 0; i < 100; i++) {
                cache.registerCacheEntryListener(cfg);
                cache.deregisterCacheEntryListener(cfg);
            }
            return null;
        }
    }, THREADS, "register-thread").get();
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheEntryListener(javax.cache.event.CacheEntryListener) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 9 with MutableCacheEntryListenerConfiguration

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

the class IgniteCacheEntryListenerAbstractTest method testSynchronousEvents.

/**
 * @throws Exception If failed.
 */
public void testSynchronousEvents() throws Exception {
    final CacheEntryCreatedListener<Object, Object> lsnr = new CreateUpdateRemoveExpireListener() {

        @Override
        public void onRemoved(Iterable<CacheEntryEvent<?, ?>> evts) {
            super.onRemoved(evts);
            awaitLatch();
        }

        @Override
        public void onCreated(Iterable<CacheEntryEvent<?, ?>> evts) {
            super.onCreated(evts);
            awaitLatch();
        }

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            super.onUpdated(evts);
            awaitLatch();
        }

        private void awaitLatch() {
            try {
                assertTrue(syncEvtLatch.await(5000, MILLISECONDS));
            } catch (InterruptedException e) {
                fail("Unexpected exception: " + e);
            }
        }
    };
    CacheEntryListenerConfiguration<Object, Object> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new Factory<CacheEntryListener<Object, Object>>() {

        @Override
        public CacheEntryListener<Object, Object> create() {
            return lsnr;
        }
    }, null, true, true);
    IgniteCache<Object, Object> cache = jcache();
    cache.registerCacheEntryListener(lsnrCfg);
    try {
        for (Integer key : keys()) {
            log.info("Check synchronous create event [key=" + key + ']');
            syncEvent(key, 1, cache, 1);
            checkEvent(evts.iterator(), key, CREATED, 1, null);
            log.info("Check synchronous update event [key=" + key + ']');
            syncEvent(key, 2, cache, 1);
            checkEvent(evts.iterator(), key, UPDATED, 2, 1);
            log.info("Check synchronous remove event [key=" + key + ']');
            syncEvent(key, null, cache, 1);
            checkEvent(evts.iterator(), key, REMOVED, null, 2);
            log.info("Check synchronous expire event [key=" + key + ']');
            syncEvent(key, 3, cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 1000))), eagerTtl() ? 2 : 1);
            checkEvent(evts.iterator(), key, CREATED, 3, null);
            if (!eagerTtl()) {
                U.sleep(1100);
                assertNull(primaryCache(key(key), cache.getName()).get(key(key)));
                evtsLatch.await(5000, MILLISECONDS);
                assertEquals(1, evts.size());
            }
            checkEvent(evts.iterator(), key, EXPIRED, null, 3);
            assertEquals(0, evts.size());
        }
    } finally {
        cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheEntryListener(javax.cache.event.CacheEntryListener) Duration(javax.cache.expiry.Duration) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Example 10 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)

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