Search in sources :

Example 51 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 52 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testAllEntries.

/**
     * @throws Exception If failed.
     */
public void testAllEntries() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, List<Integer>> map = new HashMap<>();
    final CountDownLatch latch = new CountDownLatch(5);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                synchronized (map) {
                    List<Integer> vals = map.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map.put(e.getKey(), vals);
                    }
                    vals.add(e.getValue());
                }
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        cache.put(1, 1);
        cache.put(2, 2);
        cache.put(3, 3);
        cache.remove(2);
        cache.put(1, 10);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(3, map.size());
        List<Integer> vals = map.get(1);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(1, (int) vals.get(0));
        assertEquals(10, (int) vals.get(1));
        vals = map.get(2);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(2, (int) vals.get(0));
        assertNull(vals.get(1));
        vals = map.get(3);
        assertNotNull(vals);
        assertEquals(1, vals.size());
        assertEquals(3, (int) vals.get(0));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) List(java.util.List) ArrayList(java.util.ArrayList)

Example 53 with CacheEntryEvent

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

the class GridCacheContinuousQueryAbstractSelfTest method testInternalKey.

/**
     * @throws Exception If failed.
     */
public void testInternalKey() throws Exception {
    if (atomicityMode() == ATOMIC)
        return;
    IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final Map<Object, Object> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(2);
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent<?, ?> e : evts) {
                map.put(e.getKey(), e.getValue());
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
        cache.put(new GridCacheInternalKeyImpl("test"), 1);
        cache.put(1, 1);
        cache.put(2, 2);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, map.size());
        assertEquals(1, (int) map.get(1));
        assertEquals(2, (int) map.get(2));
    }
}
Also used : GridCacheInternalKeyImpl(org.apache.ignite.internal.processors.datastructures.GridCacheInternalKeyImpl) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery)

Example 54 with CacheEntryEvent

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

the class PlatformUtils method applyContinuousQueryEvents.

/**
     * Apply continuous query events to listener.
     *
     * @param ctx Context.
     * @param lsnrPtr Listener pointer.
     * @param evts Events.
     * @throws javax.cache.event.CacheEntryListenerException In case of failure.
     */
public static void applyContinuousQueryEvents(PlatformContext ctx, long lsnrPtr, Iterable<CacheEntryEvent> evts) throws CacheEntryListenerException {
    assert lsnrPtr != 0;
    assert evts != null;
    try (PlatformMemory mem = ctx.memory().allocate()) {
        PlatformOutputStream out = mem.output();
        BinaryRawWriterEx writer = ctx.writer(out);
        writer.writeLong(lsnrPtr);
        int cntPos = writer.reserveInt();
        int cnt = 0;
        for (CacheEntryEvent evt : evts) {
            writeCacheEntryEvent(writer, evt);
            cnt++;
        }
        writer.writeInt(cntPos, cnt);
        out.synchronize();
        ctx.gateway().continuousQueryListenerApply(mem.pointer());
    } catch (Exception e) {
        throw toCacheEntryListenerException(e);
    }
}
Also used : PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) PlatformExtendedException(org.apache.ignite.internal.processors.platform.PlatformExtendedException) CacheException(javax.cache.CacheException) PlatformNativeException(org.apache.ignite.internal.processors.platform.PlatformNativeException)

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

Aggregations

CacheEntryEvent (javax.cache.event.CacheEntryEvent)55 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 CountDownLatch (java.util.concurrent.CountDownLatch)24 ArrayList (java.util.ArrayList)18 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)14 Ignite (org.apache.ignite.Ignite)13 HashMap (java.util.HashMap)11 List (java.util.List)11 IgniteCache (org.apache.ignite.IgniteCache)8 PA (org.apache.ignite.internal.util.typedef.PA)8 QueryCursor (org.apache.ignite.cache.query.QueryCursor)7 T2 (org.apache.ignite.internal.util.typedef.T2)7 IgniteException (org.apache.ignite.IgniteException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4