Search in sources :

Example 16 with CacheEntryListenerException

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

the class CacheContinuousWithTransformerFailoverTest method testTransformerException.

/**
 * @throws Exception If failed.
 */
@Test
public void testTransformerException() throws Exception {
    try {
        startGrids(1);
        Ignite ignite = ignite(0);
        IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
        final CountDownLatch latch = new CountDownLatch(10);
        ContinuousQueryWithTransformer<Integer, Integer, Integer> qry = new ContinuousQueryWithTransformer<>();
        qry.setLocalListener(new EventListener<Integer>() {

            /**
             */
            @LoggerResource
            private IgniteLogger log;

            @Override
            public void onUpdated(Iterable<? extends Integer> evts) throws CacheEntryListenerException {
                for (Integer evt : evts) {
                    if (log.isDebugEnabled())
                        log.debug("" + evt);
                }
            }
        });
        qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Integer>, Integer>() {

            @Override
            public Integer apply(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
                latch.countDown();
                throw new RuntimeException("Test error.");
            }
        }));
        qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Integer>() {

            @Override
            public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
                return true;
            }
        }));
        try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
            for (int i = 0; i < 10; i++) cache.put(i, i);
            assertTrue(latch.await(10, SECONDS));
        }
    } finally {
        stopAllGrids();
    }
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 17 with CacheEntryListenerException

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

the class CacheContinuousWithTransformerReplicatedSelfTest method testTransformerReturnNull.

/**
 * @throws Exception If failed.
 */
@Test
public void testTransformerReturnNull() throws Exception {
    Ignite ignite = gridToRunQuery();
    IgniteCache<Integer, Employee> cache = ignite.cache(DEFAULT_CACHE_NAME);
    ContinuousQueryWithTransformer<Integer, Employee, String> qry = new ContinuousQueryWithTransformer<>();
    final AtomicInteger cnt = new AtomicInteger(0);
    qry.setLocalListener(new EventListener() {

        @Override
        public void onUpdated(Iterable events) throws CacheEntryListenerException {
            for (Object e : events) {
                assertNull(e);
                cnt.incrementAndGet();
            }
        }
    });
    qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Employee>, String>() {

        @Override
        public String apply(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
            return null;
        }
    }));
    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Employee>() {

        @Override
        public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
            return true;
        }
    }));
    try (QueryCursor<Cache.Entry<Integer, Employee>> ignored = cache.query(qry)) {
        for (int i = 0; i < 10; i++) cache.put(i, new Employee(JOHN_CONNOR, i));
        boolean evtsReceived = GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return cnt.get() == 10;
            }
        }, 20_000);
        assertTrue(evtsReceived);
    }
}
Also used : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 18 with CacheEntryListenerException

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

the class PlatformUtils method evaluateContinuousQueryEvent.

/**
 * Evaluate the filter.
 *
 * @param ctx Context.
 * @param filterPtr Native filter pointer.
 * @param evt Event.
 * @return Result.
 * @throws CacheEntryListenerException In case of failure.
 */
public static boolean evaluateContinuousQueryEvent(PlatformContext ctx, long filterPtr, CacheEntryEvent evt) throws CacheEntryListenerException {
    assert filterPtr != 0;
    try (PlatformMemory mem = ctx.memory().allocate()) {
        PlatformOutputStream out = mem.output();
        out.writeLong(filterPtr);
        writeCacheEntryEvent(ctx.writer(out), evt);
        out.synchronize();
        return ctx.gateway().continuousQueryFilterApply(mem.pointer()) == 1;
    } catch (Exception e) {
        throw toCacheEntryListenerException(e);
    }
}
Also used : PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) 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 19 with CacheEntryListenerException

use of javax.cache.event.CacheEntryListenerException 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 20 with CacheEntryListenerException

use of javax.cache.event.CacheEntryListenerException 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

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