Search in sources :

Example 6 with CacheEntryListenerException

use of javax.cache.event.CacheEntryListenerException in project ignite by apache.

the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.

/**
 * @throws Exception If failed.
 */
@Test
public void testContinuousQuery() throws Exception {
    runInAllDataModes(new TestRunnable() {

        @Override
        public void run() throws Exception {
            final AtomicInteger updCnt = new AtomicInteger();
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Object, Object>() {

                @Override
                public boolean apply(Object key, Object val) {
                    return valueOf(key) >= 3;
                }
            }));
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Object, ? extends Object>> evts) throws CacheEntryListenerException {
                    for (CacheEntryEvent<? extends Object, ? extends Object> evt : evts) {
                        int v = valueOf(evt.getKey());
                        // Check filter.
                        assertTrue("v=" + v, v >= 10 && v < 15);
                        updCnt.incrementAndGet();
                    }
                }
            });
            qry.setRemoteFilter(new TestCacheEntryEventSerializableFilter());
            IgniteCache<Object, Object> cache = jcache();
            for (int i = 0; i < 10; i++) cache.put(key(i), value(i));
            try (QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry)) {
                int cnt = 0;
                for (Cache.Entry<Object, Object> e : cur) {
                    cnt++;
                    int val = valueOf(e.getKey());
                    assertTrue("v=" + val, val >= 3);
                }
                assertEquals(7, cnt);
                for (int i = 10; i < 20; i++) cache.put(key(i), value(i));
                GridTestUtils.waitForCondition(new GridAbsPredicateX() {

                    @Override
                    public boolean applyx() throws IgniteCheckedException {
                        return updCnt.get() == 5;
                    }
                }, 30_000);
            }
        }
    });
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) MutableEntry(javax.cache.processor.MutableEntry) CacheEntry(org.apache.ignite.cache.CacheEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) IgniteCacheConfigVariationsAbstractTest(org.apache.ignite.testframework.junits.IgniteCacheConfigVariationsAbstractTest) Test(org.junit.Test)

Example 7 with CacheEntryListenerException

use of javax.cache.event.CacheEntryListenerException in project ignite by apache.

the class ContinuousQueryPeerClassLoadingTest method check.

/**
 * @param node1Name Node 1 name.
 * @param node2Name Node 2 name.
 * @param node3Name Node 3 name.
 */
private void check(String node1Name, String node2Name, String node3Name) throws Exception {
    final Ignite node1 = startGrid(node1Name);
    final IgniteCache<Integer, String> cache = node1.getOrCreateCache(CACHE_NAME);
    for (int i = 0; i < 10; i++) cache.put(i, String.valueOf(i));
    final Ignite node2 = node2Name.contains("client") ? startClientGrid(node2Name) : startGrid(node2Name);
    final ContinuousQuery<Integer, String> qry1 = new ContinuousQuery<>();
    final ContinuousQuery<Integer, String> qry2 = new ContinuousQuery<>();
    qry1.setRemoteFilterFactory(new DummyEventFilterFactory<>());
    qry2.setRemoteFilterFactory(new DummyEventFilterFactory<>());
    final CountDownLatch latch1 = new CountDownLatch(20);
    final CountDownLatch latch2 = new CountDownLatch(10);
    qry1.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

        @Override
        public void onUpdated(final Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) throws CacheEntryListenerException {
            System.out.println(">> Client 1 events " + evts);
            for (CacheEntryEvent<? extends Integer, ? extends String> evt : evts) latch1.countDown();
        }
    });
    qry2.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

        @Override
        public void onUpdated(final Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) throws CacheEntryListenerException {
            System.out.println(">> Client 2 events " + evts);
            for (CacheEntryEvent<? extends Integer, ? extends String> evt : evts) latch2.countDown();
        }
    });
    final IgniteCache<Integer, String> cache1 = node2.cache(CACHE_NAME);
    cache1.query(qry1);
    for (int i = 10; i < 20; i++) cache.put(i, String.valueOf(i));
    // Fail on start second client.
    final Ignite node3 = node3Name.contains("client") ? startClientGrid(node3Name) : startGrid(node3Name);
    final IgniteCache<Integer, String> cache2 = node3.cache(CACHE_NAME);
    cache2.query(qry2);
    for (int i = 20; i < 30; i++) cache.put(i, String.valueOf(i));
    assert latch1.await(5, TimeUnit.SECONDS) : latch1.getCount();
    assert latch2.await(5, TimeUnit.SECONDS) : latch2.getCount();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite)

Example 8 with CacheEntryListenerException

use of javax.cache.event.CacheEntryListenerException in project ignite by apache.

the class GridCacheContinuousQueryReplicatedTxOneNodeTest method doTestOneNode.

/**
 * @throws Exception If failed.
 */
private void doTestOneNode(boolean loc) throws Exception {
    try {
        IgniteCache<String, Integer> cache = startGrid(0).cache(DEFAULT_CACHE_NAME);
        ContinuousQuery<String, Integer> qry = new ContinuousQuery<>();
        final AtomicInteger cnt = new AtomicInteger();
        final CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) cache.put("key" + i, i);
        if (atomicMode() != CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT)
            cache.clear();
        else {
            // TODO IGNITE-7952. Remove "else" clause - do cache.clear() instead of iteration.
            for (Iterator it = cache.iterator(); it.hasNext(); ) {
                it.next();
                it.remove();
            }
        }
        qry.setLocalListener(new CacheEntryUpdatedListener<String, Integer>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends String, ? extends Integer>> evts) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends String, ? extends Integer> evt : evts) {
                    cnt.incrementAndGet();
                    latch.countDown();
                }
            }
        });
        cache.query(qry.setLocal(loc));
        for (int i = 0; i < 10; i++) cache.put("key" + i, i);
        assert latch.await(5000, TimeUnit.MILLISECONDS);
        assertEquals(10, cnt.get());
    } finally {
        stopAllGrids();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator)

Example 9 with CacheEntryListenerException

use of javax.cache.event.CacheEntryListenerException in project ignite by apache.

the class GridServiceContinuousQueryRedeployTest method testServiceRedeploymentAfterCancel.

/**
 * @throws Exception If failed.
 */
@Test
public void testServiceRedeploymentAfterCancel() throws Exception {
    final Ignite ignite = startGrid(0);
    final IgniteCache<Object, Object> managementCache = ignite.getOrCreateCache(CACHE_NAME);
    final ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final List<Object> evts = Collections.synchronizedList(new ArrayList<>());
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> iterable) throws CacheEntryListenerException {
            for (CacheEntryEvent<?, ?> event : iterable) evts.add(event);
        }
    });
    int iterations = 100;
    while (iterations-- > 0) {
        QueryCursor quorumCursor = managementCache.query(qry);
        IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                System.out.println("Deploy " + SERVICE_NAME);
                deployService(ignite);
                return null;
            }
        });
        IgniteInternalFuture<?> fut2 = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                System.out.println("Undeploy " + SERVICE_NAME);
                ignite.services().cancel(SERVICE_NAME);
                return null;
            }
        });
        fut1.get();
        fut2.get();
        U.sleep(100);
        assert evts.size() <= 1 : evts.size();
        ignite.services().cancel("service1");
        evts.clear();
        quorumCursor.close();
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 10 with CacheEntryListenerException

use of javax.cache.event.CacheEntryListenerException in project ignite by apache.

the class CacheContinuousQueryExecuteInPrimaryTest method doTestWithEventsEntries.

/**
 * @throws Exception If failed.
 */
public void doTestWithEventsEntries(CacheConfiguration<Integer, String> ccfg) throws Exception {
    try (IgniteCache<Integer, String> cache = grid(0).createCache(ccfg)) {
        ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
        final CountDownLatch latch = new CountDownLatch(16);
        final AtomicInteger cnt = new AtomicInteger(0);
        qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> iterable) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends Integer, ? extends String> e : iterable) {
                    cnt.incrementAndGet();
                    latch.countDown();
                }
            }
        });
        qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, String>() {

            @Override
            public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) throws CacheEntryListenerException {
                return e.getKey() % 2 == 0;
            }
        }));
        // Execute query.
        executeQuery(cache, qry, ccfg.getAtomicityMode() != ATOMIC);
        assertTrue(latch.await(LATCH_TIMEOUT, MILLISECONDS));
        assertEquals(16, cnt.get());
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)22 CacheEntryEvent (javax.cache.event.CacheEntryEvent)15 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)15 Ignite (org.apache.ignite.Ignite)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)5 QueryCursor (org.apache.ignite.cache.query.QueryCursor)5 Test (org.junit.Test)5 CacheException (javax.cache.CacheException)4 IgniteException (org.apache.ignite.IgniteException)4 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)4 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)3 PlatformMemory (org.apache.ignite.internal.processors.platform.memory.PlatformMemory)3 PlatformOutputStream (org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Cache (javax.cache.Cache)2