Search in sources :

Example 56 with CacheEntryEvent

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

the class GridCacheContinuousQueryNodesFilteringTest method startNodeWithCache.

/**
 * Start first, attribute-bearing, node, create new cache and launch continuous query on it.
 *
 * @return Node.
 * @throws Exception if failed.
 */
private Ignite startNodeWithCache() throws Exception {
    Ignite node1 = startGrid("node1", getConfiguration("node1", true, null));
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setName("attrsTestCache");
    ccfg.setNodeFilter(new IgnitePredicate<ClusterNode>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean apply(ClusterNode node) {
            return "data".equals(node.attribute("node-type"));
        }
    });
    IgniteCache<Integer, Integer> cache = node1.createCache(ccfg);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setRemoteFilterFactory(new RemoteFilterFactory());
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
        // No-op.
        }
    });
    RemoteFilterFactory.clsLdr = getExternalClassLoader();
    cache.query(qry);
    // Switch class loader before starting the second node.
    RemoteFilterFactory.clsLdr = getClass().getClassLoader();
    return node1;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 57 with CacheEntryEvent

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

the class GridCacheContinuousQueryReplicatedSelfTest method testCrossCallback.

/**
 * Ensure that every node see every update.
 *
 * @throws Exception If failed.
 */
public void testCrossCallback() throws Exception {
    // Prepare.
    IgniteCache<Integer, Integer> cache1 = grid(0).cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, Integer> cache2 = grid(1).cache(DEFAULT_CACHE_NAME);
    final int key1 = primaryKey(cache1);
    final int key2 = primaryKey(cache2);
    final CountDownLatch latch1 = new CountDownLatch(2);
    final CountDownLatch latch2 = new CountDownLatch(2);
    ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<>();
    qry1.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                log.info("Update in cache 1: " + evt);
                if (evt.getKey() == key1 || evt.getKey() == key2)
                    latch1.countDown();
            }
        }
    });
    ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<>();
    qry2.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                log.info("Update in cache 2: " + evt);
                if (evt.getKey() == key1 || evt.getKey() == key2)
                    latch2.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache2.query(qry1);
        QueryCursor<Cache.Entry<Integer, Integer>> ignore = cache2.query(qry2)) {
        cache1.put(key1, key1);
        cache1.put(key2, key2);
        assert latch1.await(LATCH_TIMEOUT, MILLISECONDS);
        assert latch2.await(LATCH_TIMEOUT, MILLISECONDS);
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent)

Example 58 with CacheEntryEvent

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

the class GridCacheContinuousQueryReplicatedSelfTest method testRemoteNodeCallback.

/**
 * @throws Exception If failed.
 */
public void testRemoteNodeCallback() throws Exception {
    IgniteCache<Integer, Integer> cache1 = grid(0).cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, Integer> cache2 = grid(1).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final AtomicReference<Integer> val = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            Iterator<CacheEntryEvent<? extends Integer, ? extends Integer>> it = evts.iterator();
            CacheEntryEvent<? extends Integer, ? extends Integer> e = it.next();
            assert !it.hasNext();
            log.info("Event: " + e);
            val.set(e.getValue());
            latch.countDown();
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache2.query(qry)) {
        cache1.put(1, 10);
        latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(10, val.get().intValue());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Iterator(java.util.Iterator)

Example 59 with CacheEntryEvent

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

the class GridCacheContinuousQueryReplicatedTxOneNodeTest method doTest.

/**
 * @throws Exception If failed.
 */
private void doTest(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);
        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));
        startGrid(1);
        awaitPartitionMapExchange();
        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)

Example 60 with CacheEntryEvent

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

the class CacheContinuousQueryExecuteInPrimaryTest method doTestWithoutEventsEntries.

/**
 * @throws Exception If failed.
 */
private void doTestWithoutEventsEntries(CacheConfiguration<Integer, String> ccfg) throws Exception {
    try (IgniteCache<Integer, String> cache = grid(0).createCache(ccfg)) {
        int ITERATION_CNT = 100;
        final AtomicBoolean noOneListen = new AtomicBoolean(true);
        for (int i = 0; i < ITERATION_CNT; i++) {
            ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
            qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> iterable) throws CacheEntryListenerException {
                    noOneListen.set(false);
                }
            });
            qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, String>() {

                @Override
                public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> cacheEntryEvent) throws CacheEntryListenerException {
                    return false;
                }
            }));
            executeQuery(cache, qry, ccfg.getAtomicityMode() == TRANSACTIONAL);
        }
        assertTrue(noOneListen.get());
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery)

Aggregations

CacheEntryEvent (javax.cache.event.CacheEntryEvent)64 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 CountDownLatch (java.util.concurrent.CountDownLatch)29 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)19 Ignite (org.apache.ignite.Ignite)19 ArrayList (java.util.ArrayList)18 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)16 HashMap (java.util.HashMap)12 List (java.util.List)11 IgniteCache (org.apache.ignite.IgniteCache)11 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)10 QueryCursor (org.apache.ignite.cache.query.QueryCursor)10 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)8 PA (org.apache.ignite.internal.util.typedef.PA)8 T2 (org.apache.ignite.internal.util.typedef.T2)8 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)6 Cache (javax.cache.Cache)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteException (org.apache.ignite.IgniteException)6