Search in sources :

Example 21 with IgniteBiPredicate

use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.

the class RocketMQStreamerTest method testStreamer.

/**
 * Tests data is properly injected into the grid.
 *
 * @throws Exception If fails.
 */
public void testStreamer() throws Exception {
    RocketMQStreamer<String, byte[]> streamer = null;
    Ignite ignite = grid();
    try (IgniteDataStreamer<String, byte[]> dataStreamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
        dataStreamer.allowOverwrite(true);
        dataStreamer.autoFlushFrequency(10);
        streamer = new RocketMQStreamer<>();
        // configure.
        streamer.setIgnite(ignite);
        streamer.setStreamer(dataStreamer);
        streamer.setNameSrvAddr(TEST_IP + ":" + NAME_SERVER_PORT);
        streamer.setConsumerGrp(CONSUMER_GRP);
        streamer.setTopic(TOPIC_NAME);
        streamer.setMultipleTupleExtractor(new TestTupleExtractor());
        streamer.start();
        IgniteCache<String, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
        assertEquals(0, cache.size(CachePeekMode.PRIMARY));
        final CountDownLatch latch = new CountDownLatch(EVT_NUM);
        IgniteBiPredicate<UUID, CacheEvent> putLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {

            @Override
            public boolean apply(UUID uuid, CacheEvent evt) {
                assert evt != null;
                latch.countDown();
                return true;
            }
        };
        ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(putLsnr, null, EVT_CACHE_OBJECT_PUT);
        produceData();
        assertTrue(latch.await(30, TimeUnit.SECONDS));
        assertEquals(EVT_NUM, cache.size(CachePeekMode.PRIMARY));
    } finally {
        if (streamer != null)
            streamer.stop();
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 22 with IgniteBiPredicate

use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.

the class IgniteJmsStreamerTest method subscribeToPutEvents.

/**
 * @param expect Expected events number.
 * @return Event receive latch.
 */
private CountDownLatch subscribeToPutEvents(int expect) {
    Ignite ignite = grid();
    // Listen to cache PUT events and expect as many as messages as test data items
    final CountDownLatch latch = new CountDownLatch(expect);
    @SuppressWarnings("serial") IgniteBiPredicate<UUID, CacheEvent> cb = new IgniteBiPredicate<UUID, CacheEvent>() {

        @Override
        public boolean apply(UUID uuid, CacheEvent evt) {
            latch.countDown();
            return true;
        }
    };
    ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(cb, null, EVT_CACHE_OBJECT_PUT);
    return latch;
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 23 with IgniteBiPredicate

use of org.apache.ignite.lang.IgniteBiPredicate in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method testScanQueryEvents.

/**
 * @throws Exception If failed.
 */
public void testScanQueryEvents() throws Exception {
    final Map<Integer, Integer> map = new ConcurrentHashMap<>();
    final IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
    final boolean evtsDisabled = cache.getConfiguration(CacheConfiguration.class).isEventsDisabled();
    final CountDownLatch latch = new CountDownLatch(evtsDisabled ? 0 : 10);
    final CountDownLatch execLatch = new CountDownLatch(evtsDisabled ? 0 : cacheMode() == REPLICATED ? 1 : gridCount());
    IgnitePredicate[] objReadLsnrs = new IgnitePredicate[gridCount()];
    IgnitePredicate[] qryExecLsnrs = new IgnitePredicate[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryReadEvent;
                if (evtsDisabled)
                    fail("Cache events are disabled");
                CacheQueryReadEvent<Integer, Integer> qe = (CacheQueryReadEvent<Integer, Integer>) evt;
                assertEquals(SCAN.name(), qe.queryType());
                assertEquals(cache.getName(), qe.cacheName());
                assertNull(qe.className());
                assertNull(null, qe.clause());
                assertNotNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertNull(qe.arguments());
                map.put(qe.key(), qe.value());
                latch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(pred, EVT_CACHE_QUERY_OBJECT_READ);
        objReadLsnrs[i] = pred;
        IgnitePredicate<Event> execPred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryExecutedEvent;
                if (evtsDisabled)
                    fail("Cache events are disabled");
                CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
                assertEquals(SCAN.name(), qe.queryType());
                assertEquals(cache.getName(), qe.cacheName());
                assertNull(qe.className());
                assertNull(null, qe.clause());
                assertNotNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertNull(qe.arguments());
                execLatch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(execPred, EVT_CACHE_QUERY_EXECUTED);
        qryExecLsnrs[i] = execPred;
    }
    try {
        for (int i = 0; i < 20; i++) cache.put(i, i);
        IgniteBiPredicate<Integer, Integer> filter = new IgniteBiPredicate<Integer, Integer>() {

            @Override
            public boolean apply(Integer k, Integer v) {
                return k >= 10;
            }
        };
        QueryCursor<Cache.Entry<Integer, Integer>> q = cache.query(new ScanQuery<>(filter));
        q.getAll();
        assert latch.await(1000, MILLISECONDS);
        assert execLatch.await(1000, MILLISECONDS);
        if (!evtsDisabled) {
            assertEquals(10, map.size());
            for (int i = 10; i < 20; i++) assertEquals(i, map.get(i).intValue());
        }
    } finally {
        for (int i = 0; i < gridCount(); i++) {
            grid(i).events().stopLocalListen(objReadLsnrs[i]);
            grid(i).events().stopLocalListen(qryExecLsnrs[i]);
        }
    }
}
Also used : CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)23 Ignite (org.apache.ignite.Ignite)18 UUID (java.util.UUID)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 CacheEvent (org.apache.ignite.events.CacheEvent)8 IgniteCache (org.apache.ignite.IgniteCache)6 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)5 Cache (javax.cache.Cache)4 IgniteException (org.apache.ignite.IgniteException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ScanQuery (org.apache.ignite.cache.query.ScanQuery)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 Map (java.util.Map)2 CacheException (javax.cache.CacheException)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 Ignition (org.apache.ignite.Ignition)2