Search in sources :

Example 1 with CacheEntryEventFilter

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

the class CacheContinuousQueryOperationP2PTest method testContinuousQuery.

/**
 * @param ccfg Cache configuration.
 * @param isClient Client.
 * @throws Exception If failed.
 */
protected void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    QueryCursor<?> cur = null;
    final Class<Factory<CacheEntryEventFilter>> evtFilterFactory = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    final CountDownLatch latch = new CountDownLatch(10);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    TestLocalListener localLsnr = new TestLocalListener() {

        @Override
        public void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                latch.countDown();
                log.info("Received event: " + evt);
            }
        }
    };
    MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(localLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactory.newInstance(), true, true);
    qry.setLocalListener(localLsnr);
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactory.newInstance());
    IgniteCache<Integer, Integer> cache = null;
    try {
        if (isClient)
            cache = grid(NODES - 1).cache(ccfg.getName());
        else
            cache = grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
        cur = cache.query(qry);
        cache.registerCacheEntryListener(lsnrCfg);
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertTrue(latch.await(3, TimeUnit.SECONDS));
    } finally {
        if (cur != null)
            cur.close();
        if (cache != null)
            cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 2 with CacheEntryEventFilter

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

the class CacheContinuousQueryHandler method register.

/**
 * {@inheritDoc}
 */
@Override
public RegisterStatus register(final UUID nodeId, final UUID routineId, final GridKernalContext ctx) throws IgniteCheckedException {
    assert nodeId != null;
    assert routineId != null;
    assert ctx != null;
    initLocalListener(locLsnr, ctx);
    if (initFut == null) {
        initFut = p2pUnmarshalFut.chain((fut) -> {
            try {
                fut.get();
                initRemoteFilter(getEventFilter0(), ctx);
                IgniteClosure trans = getTransformer0();
                if (trans != null)
                    ctx.resource().injectGeneric(trans);
            } catch (IgniteCheckedException | ExceptionInInitializerError e) {
                throw new IgniteException("Failed to initialize a continuous query.", e);
            }
            return null;
        });
    }
    if (initFut.error() != null)
        throw new IgniteCheckedException("Failed to initialize a continuous query.", initFut.error());
    entryBufs = new ConcurrentHashMap<>();
    ackBuf = new CacheContinuousQueryAcknowledgeBuffer();
    rcvs = new ConcurrentHashMap<>();
    this.nodeId = nodeId;
    this.routineId = routineId;
    this.ctx = ctx;
    final boolean loc = nodeId.equals(ctx.localNodeId());
    assert !skipPrimaryCheck || loc;
    log = ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY);
    CacheContinuousQueryListener<K, V> lsnr = new CacheContinuousQueryListener<K, V>() {

        @Override
        public void onBeforeRegister() {
            GridCacheContext<K, V> cctx = cacheContext(ctx);
            if (cctx != null && !cctx.isLocal())
                cctx.topology().readLock();
        }

        @Override
        public void onAfterRegister() {
            GridCacheContext<K, V> cctx = cacheContext(ctx);
            if (cctx != null && !cctx.isLocal())
                cctx.topology().readUnlock();
        }

        @Override
        public void onRegister() {
            GridCacheContext<K, V> cctx = cacheContext(ctx);
            if (cctx != null && !cctx.isLocal())
                locInitUpdCntrs = toCountersMap(cctx.topology().localUpdateCounters(false));
        }

        @Override
        public boolean keepBinary() {
            return keepBinary;
        }

        @Override
        public void onEntryUpdated(final CacheContinuousQueryEvent<K, V> evt, boolean primary, final boolean recordIgniteEvt, GridDhtAtomicAbstractUpdateFuture fut) {
            if (ignoreExpired && evt.getEventType() == EventType.EXPIRED)
                return;
            if (log.isDebugEnabled())
                log.debug("Entry updated on affinity node [evt=" + evt + ", primary=" + primary + ']');
            final GridCacheContext<K, V> cctx = cacheContext(ctx);
            // Check that cache stopped.
            if (cctx == null)
                return;
            if (!needNotify(false, cctx, -1, -1, evt))
                return;
            // skipPrimaryCheck is set only when listen locally for replicated cache events.
            assert !skipPrimaryCheck || (cctx.isReplicated() && ctx.localNodeId().equals(nodeId));
            if (asyncCb) {
                ContinuousQueryAsyncClosure clsr = new ContinuousQueryAsyncClosure(primary, evt, recordIgniteEvt, fut);
                ctx.pools().asyncCallbackPool().execute(clsr, evt.partitionId());
            } else {
                final boolean notify = filter(evt);
                if (log.isDebugEnabled())
                    log.debug("Filter invoked for event [evt=" + evt + ", primary=" + primary + ", notify=" + notify + ']');
                if (primary || skipPrimaryCheck)
                    onEntryUpdate(evt, notify, loc, recordIgniteEvt);
                else
                    handleBackupEntry(cctx, evt.entry());
            }
        }

        @Override
        public void onUnregister() {
            try {
                CacheEntryEventFilter filter = getEventFilter();
                if (filter instanceof PlatformContinuousQueryFilter)
                    ((PlatformContinuousQueryFilter) filter).onQueryUnregister();
            } catch (IgniteCheckedException e) {
                if (log.isDebugEnabled()) {
                    log.debug("Failed to execute the onUnregister callback " + "on the continuoue query listener. " + "[nodeId=" + nodeId + ", routineId=" + routineId + ", cacheName=" + cacheName + ", err=" + e + "]");
                }
            }
        }

        @Override
        public void cleanupOnAck(Map<Integer, Long> updateCntrs) {
            for (Map.Entry<Integer, Long> e : updateCntrs.entrySet()) {
                CacheContinuousQueryEventBuffer buf = entryBufs.get(e.getKey());
                if (buf != null)
                    buf.cleanupOnAck(e.getValue());
            }
        }

        @Override
        public void flushOnExchangeDone(GridKernalContext ctx, AffinityTopologyVersion topVer) {
            assert topVer != null;
            try {
                GridCacheContext<K, V> cctx = cacheContext(ctx);
                ClusterNode node = ctx.discovery().node(nodeId);
                for (Map.Entry<Integer, CacheContinuousQueryEventBuffer> bufE : entryBufs.entrySet()) {
                    CacheContinuousQueryEventBuffer buf = bufE.getValue();
                    Collection<CacheContinuousQueryEntry> entries = buf.flushOnExchange((cntr, filtered) -> createFilteredEntry(cctx.cacheId(), bufE.getKey(), topVer, cntr, filtered));
                    if (entries == null || node == null)
                        continue;
                    for (CacheContinuousQueryEntry e : entries) {
                        e.markBackup();
                        if (!e.isFiltered())
                            prepareEntry(cctx, nodeId, e);
                    }
                    ctx.continuous().addBackupNotification(nodeId, routineId, entries, topic);
                }
            } catch (IgniteCheckedException e) {
                U.error(ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY), "Failed to send backup event notification to node: " + nodeId, e);
            }
        }

        @Override
        public void acknowledgeBackupOnTimeout(GridKernalContext ctx) {
            sendBackupAcknowledge(ackBuf.acknowledgeOnTimeout(), routineId, ctx);
        }

        @Override
        public void skipUpdateEvent(CacheContinuousQueryEvent<K, V> evt, AffinityTopologyVersion topVer, boolean primary) {
            assert evt != null;
            CacheContinuousQueryEntry e = evt.entry();
            e.markFiltered();
            onEntryUpdated(evt, primary, false, null);
        }

        @Override
        public CounterSkipContext skipUpdateCounter(final GridCacheContext cctx, @Nullable CounterSkipContext skipCtx, int part, long cntr, AffinityTopologyVersion topVer, boolean primary) {
            if (skipCtx == null)
                skipCtx = new CounterSkipContext(part, cntr, topVer);
            if (!needNotify(true, cctx, part, cntr, null))
                return skipCtx;
            if (loc) {
                assert !locOnly;
                final Collection<CacheEntryEvent<? extends K, ? extends V>> evts = handleEvent(ctx, skipCtx.entry());
                if (!evts.isEmpty()) {
                    if (asyncCb) {
                        ctx.pools().asyncCallbackPool().execute(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    notifyLocalListener(evts, getTransformer());
                                } catch (IgniteCheckedException ex) {
                                    U.error(ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY), "Failed to notify local listener.", ex);
                                }
                            }
                        }, part);
                    } else
                        skipCtx.addProcessClosure(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    notifyLocalListener(evts, getTransformer());
                                } catch (IgniteCheckedException ex) {
                                    U.error(ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY), "Failed to notify local listener.", ex);
                                }
                            }
                        });
                }
                return skipCtx;
            }
            CacheContinuousQueryEventBuffer buf = partitionBuffer(cctx, part);
            final Object entryOrList = buf.processEntry(skipCtx.entry(), !primary);
            if (entryOrList != null) {
                skipCtx.addProcessClosure(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            ctx.continuous().addNotification(nodeId, routineId, entryOrList, topic, false, true);
                        } catch (ClusterTopologyCheckedException ex) {
                            if (log.isDebugEnabled())
                                log.debug("Failed to send event notification to node, node left cluster " + "[node=" + nodeId + ", err=" + ex + ']');
                        } catch (IgniteCheckedException ex) {
                            U.error(ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY), "Failed to send event notification to node: " + nodeId, ex);
                        }
                    }
                });
            }
            return skipCtx;
        }

        @Override
        public void onPartitionEvicted(int part) {
            entryBufs.remove(part);
        }

        @Override
        public boolean oldValueRequired() {
            return oldValRequired;
        }

        @Override
        public boolean notifyExisting() {
            return notifyExisting;
        }

        private String taskName() {
            return ctx.security().enabled() ? ctx.task().resolveTaskName(taskHash) : null;
        }

        @Override
        public boolean isPrimaryOnly() {
            return locOnly && !skipPrimaryCheck;
        }

        /**
         * Checks whether it is need to notify listeners.
         *
         * @param skipEvt {@code True} if this is a skip counter event.
         * @param cctx Cache context.
         * @param part Partition id.
         * @param cntr Update counter.
         * @param evt CQ event.
         * @return {@code True} if notification should happen immediately, or {@code false} if it should be delayed.
         */
        private boolean needNotify(boolean skipEvt, GridCacheContext cctx, int part, long cntr, CacheContinuousQueryEvent evt) {
            assert !skipEvt || evt == null;
            // part == -1 && cntr == -1 means skip counter.
            assert skipEvt || part == -1 && cntr == -1;
            if (!cctx.mvccEnabled() || cctx.isLocal())
                return true;
            assert locInitUpdCntrs != null;
            cntr = skipEvt ? cntr : evt.getPartitionUpdateCounter();
            part = skipEvt ? part : evt.partitionId();
            T2<Long, Long> initCntr = locInitUpdCntrs.get(part);
            // Do not notify listener if entry was updated before the query is started.
            return initCntr == null || cntr >= initCntr.get2();
        }
    };
    CacheContinuousQueryManager mgr = manager(ctx);
    if (mgr == null)
        return RegisterStatus.DELAYED;
    RegisterStatus regStatus = mgr.registerListener(routineId, lsnr, internal);
    if (regStatus == RegisterStatus.REGISTERED)
        initFut.listen(res -> sendQueryExecutedEvent());
    return regStatus;
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) ObjectOutput(java.io.ObjectOutput) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) JCacheQueryRemoteFilter(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager.JCacheQueryRemoteFilter) GridCacheDeploymentManager(org.apache.ignite.internal.processors.cache.GridCacheDeploymentManager) GridContinuousBatch(org.apache.ignite.internal.processors.continuous.GridContinuousBatch) CachePartitionPartialCountersMap.toCountersMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.CachePartitionPartialCountersMap.toCountersMap) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Map(java.util.Map) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridIoPolicy(org.apache.ignite.internal.managers.communication.GridIoPolicy) Externalizable(java.io.Externalizable) PlatformContinuousQueryFilter(org.apache.ignite.internal.processors.platform.cache.query.PlatformContinuousQueryFilter) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheContinuousQueryEntry.createFilteredEntry(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryEntry.createFilteredEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridContinuousHandler(org.apache.ignite.internal.processors.continuous.GridContinuousHandler) GridCacheAffinityManager(org.apache.ignite.internal.processors.cache.GridCacheAffinityManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) CU(org.apache.ignite.internal.util.typedef.internal.CU) ObjectInput(java.io.ObjectInput) IgniteStripedThreadPoolExecutor(org.apache.ignite.thread.IgniteStripedThreadPoolExecutor) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) NotNull(org.jetbrains.annotations.NotNull) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteLogger(org.apache.ignite.IgniteLogger) GridContinuousQueryBatch(org.apache.ignite.internal.processors.continuous.GridContinuousQueryBatch) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtAtomicAbstractUpdateFuture(org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicAbstractUpdateFuture) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheEntryListener(javax.cache.event.CacheEntryListener) S(org.apache.ignite.internal.util.typedef.internal.S) EVT_CACHE_QUERY_OBJECT_READ(org.apache.ignite.events.EventType.EVT_CACHE_QUERY_OBJECT_READ) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) Iterator(java.util.Iterator) SystemProperty(org.apache.ignite.SystemProperty) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IOException(java.io.IOException) T2(org.apache.ignite.internal.util.typedef.T2) GridDeploymentInfo(org.apache.ignite.internal.managers.deployment.GridDeploymentInfo) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheQueryType(org.apache.ignite.internal.processors.cache.query.CacheQueryType) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) IgniteAsyncCallback(org.apache.ignite.lang.IgniteAsyncCallback) Collections(java.util.Collections) EVT_CACHE_QUERY_EXECUTED(org.apache.ignite.events.EventType.EVT_CACHE_QUERY_EXECUTED) JCacheQueryLocalListener(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager.JCacheQueryLocalListener) IgniteClosure(org.apache.ignite.lang.IgniteClosure) PlatformContinuousQueryFilter(org.apache.ignite.internal.processors.platform.cache.query.PlatformContinuousQueryFilter) GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtAtomicAbstractUpdateFuture(org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicAbstractUpdateFuture) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) CachePartitionPartialCountersMap.toCountersMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.CachePartitionPartialCountersMap.toCountersMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Nullable(org.jetbrains.annotations.Nullable) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 3 with CacheEntryEventFilter

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

the class UsingContinuousQueries method remoteFilterExample.

public static void remoteFilterExample() {
    try (Ignite ignite = Ignition.start()) {
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
        // tag::remoteFilter[]
        ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
        qry.setLocalListener(events -> events.forEach(event -> System.out.format("Entry: key=[%s] value=[%s]\n", event.getKey(), event.getValue())));
        qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {

            @Override
            public CacheEntryEventFilter<Integer, String> create() {
                return new CacheEntryEventFilter<Integer, String>() {

                    @Override
                    public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
                        System.out.format("the value for key [%s] was updated from [%s] to [%s]\n", e.getKey(), e.getOldValue(), e.getValue());
                        return true;
                    }
                };
            }
        });
        // end::remoteFilter[]
        cache.query(qry);
        cache.put(1, "1");
    }
}
Also used : Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) Ignition(org.apache.ignite.Ignition) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter)

Example 4 with CacheEntryEventFilter

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

the class CacheContinuousQueryExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws Exception If example execution failed.
 */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache continuous query example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            int keyCnt = 20;
            // These entries will be queried by initial predicate.
            for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
            // Create new continuous query.
            ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() {

                @Override
                public boolean apply(Integer key, String val) {
                    return key > 10;
                }
            }));
            // Callback that is called locally when update notifications are received.
            qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) {
                    for (CacheEntryEvent<? extends Integer, ? extends String> e : evts) System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                }
            });
            // This filter will be evaluated remotely on all nodes.
            // Entry that pass this filter will be sent to the caller.
            qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {

                @Override
                public CacheEntryEventFilter<Integer, String> create() {
                    return new CacheEntryEventFilter<Integer, String>() {

                        @Override
                        public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
                            return e.getKey() > 10;
                        }
                    };
                }
            });
            // Execute query.
            try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
                // Iterate through existing data.
                for (Cache.Entry<Integer, String> e : cur) System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                // Add a few more keys and watch more query notifications.
                for (int i = keyCnt; i < keyCnt + 10; i++) cache.put(i, Integer.toString(i));
                // Wait for a while while callback is notified about remaining puts.
                Thread.sleep(2000);
            }
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 5 with CacheEntryEventFilter

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

the class GridP2PContinuousDeploymentClientDisconnectTest method testContinuousQueryRemoteFilterFactory.

/**
 * Test starts 1 server node and 1 client node. Class-loading request for the {@link #P2P_TEST_OBJ_RSRC_NAME}
 * resource blocks on the client node. The client node tries to deploy CQ with remote filter factory for
 * the cache {@link #DEFAULT_CACHE_NAME}.
 * Expected that exception with 'Failed to initialize a continuous query.' error message will be thrown and
 * the server node wouldn't be failed.
 *
 * @throws Exception If failed.
 */
@Test
public void testContinuousQueryRemoteFilterFactory() throws Exception {
    final Class<Factory<? extends CacheEntryEventFilter<Integer, Integer>>> rmtFilterFactoryCls = (Class<Factory<? extends CacheEntryEventFilter<Integer, Integer>>>) getExternalClassLoader().loadClass(REMOTE_FILTER_FACTORY_CLS_NAME);
    AbstractContinuousQuery<Integer, Integer> qry = new ContinuousQuery<Integer, Integer>().setLocalListener(evts -> {
    // No-op.
    }).setRemoteFilterFactory(rmtFilterFactoryCls.newInstance());
    IgniteEx client = grid(1);
    LogListener lsnr = LogListener.matches("Failed to initialize a continuous query.").build();
    testLog.registerListener(lsnr);
    IgniteCache<Integer, Integer> cache = client.cache(DEFAULT_CACHE_NAME);
    cache.query(qry);
    assertTrue(lsnr.check());
    // Check that the failure handler was not called.
    assertFalse(failure.get());
}
Also used : ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) LogListener(org.apache.ignite.testframework.LogListener) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) IgniteEx(org.apache.ignite.internal.IgniteEx) TOPIC_CLASSLOAD(org.apache.ignite.internal.GridTopic.TOPIC_CLASSLOAD) GridKernalContext(org.apache.ignite.internal.GridKernalContext) GridDeploymentManager(org.apache.ignite.internal.managers.deployment.GridDeploymentManager) AbstractFailureHandler(org.apache.ignite.failure.AbstractFailureHandler) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) FailureContext(org.apache.ignite.failure.FailureContext) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) GridDeploymentRequest(org.apache.ignite.internal.managers.deployment.GridDeploymentRequest) CacheException(javax.cache.CacheException) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Factory(javax.cache.configuration.Factory) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EventType(org.apache.ignite.events.EventType) IgniteException(org.apache.ignite.IgniteException) Event(org.apache.ignite.events.Event) Test(org.junit.Test) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LogListener(org.apache.ignite.testframework.LogListener) IgniteEx(org.apache.ignite.internal.IgniteEx) Factory(javax.cache.configuration.Factory) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)14 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)11 Factory (javax.cache.configuration.Factory)8 CacheEntryEvent (javax.cache.event.CacheEntryEvent)8 IgniteCache (org.apache.ignite.IgniteCache)8 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 Test (org.junit.Test)6 Ignite (org.apache.ignite.Ignite)5 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)5 Cache (javax.cache.Cache)4 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)4 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 U (org.apache.ignite.internal.util.typedef.internal.U)4 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 IgniteClosure (org.apache.ignite.lang.IgniteClosure)3