Search in sources :

Example 1 with EventListener

use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener in project ignite by apache.

the class IgniteCacheProxyImpl method queryContinuous.

/**
 * Executes continuous query.
 *
 * @param qry Query.
 * @param loc Local flag.
 * @param keepBinary Keep binary flag.
 * @return Initial iteration cursor.
 */
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(AbstractContinuousQuery qry, boolean loc, boolean keepBinary) {
    assert qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer;
    if (qry.getInitialQuery() instanceof ContinuousQuery || qry.getInitialQuery() instanceof ContinuousQueryWithTransformer) {
        throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
    }
    CacheEntryUpdatedListener locLsnr = null;
    EventListener locTransLsnr = null;
    CacheEntryEventSerializableFilter rmtFilter = null;
    Factory<? extends IgniteClosure> rmtTransFactory = null;
    if (qry instanceof ContinuousQuery) {
        ContinuousQuery<K, V> qry0 = (ContinuousQuery<K, V>) qry;
        if (qry0.getLocalListener() == null)
            throw new IgniteException("Mandatory local listener is not set for the query: " + qry);
        if (qry0.getRemoteFilter() != null && qry0.getRemoteFilterFactory() != null)
            throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
        locLsnr = qry0.getLocalListener();
        rmtFilter = qry0.getRemoteFilter();
    } else {
        ContinuousQueryWithTransformer<K, V, ?> qry0 = (ContinuousQueryWithTransformer<K, V, ?>) qry;
        if (qry0.getLocalListener() == null)
            throw new IgniteException("Mandatory local transformed event listener is not set for the query: " + qry);
        if (qry0.getRemoteTransformerFactory() == null)
            throw new IgniteException("Mandatory RemoteTransformerFactory is not set for the query: " + qry);
        Collection<ClusterNode> nodes = context().grid().cluster().nodes();
        for (ClusterNode node : nodes) {
            if (node.version().compareTo(CONT_QRY_WITH_TRANSFORMER_SINCE) < 0) {
                throw new IgniteException("Can't start ContinuousQueryWithTransformer, " + "because some nodes in cluster doesn't support this feature: " + node);
            }
        }
        locTransLsnr = qry0.getLocalListener();
        rmtTransFactory = qry0.getRemoteTransformerFactory();
    }
    try {
        final UUID routineId = ctx.continuousQueries().executeQuery(locLsnr, locTransLsnr, rmtFilter, qry.getRemoteFilterFactory(), rmtTransFactory, qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
        final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
        return new QueryCursor<Cache.Entry<K, V>>() {

            @Override
            public Iterator<Cache.Entry<K, V>> iterator() {
                return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
            }

            @Override
            public List<Cache.Entry<K, V>> getAll() {
                return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
            }

            @Override
            public void close() {
                if (cur != null)
                    cur.close();
                try {
                    ctx.kernalContext().continuous().stopRoutine(routineId).get();
                } catch (IgniteCheckedException e) {
                    throw U.convertException(e);
                }
            }
        };
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntry(org.apache.ignite.cache.CacheEntry) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) IgniteException(org.apache.ignite.IgniteException) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 2 with EventListener

use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener in project ignite by apache.

the class CacheContinuousWithTransformerReplicatedSelfTest method testTransformerReturnNull.

/**
 * @throws Exception If failed.
 */
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)

Aggregations

CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)2 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)2 EventListener (org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener)2 UUID (java.util.UUID)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Cache (javax.cache.Cache)1 CacheEntryEvent (javax.cache.event.CacheEntryEvent)1 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)1 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)1 Ignite (org.apache.ignite.Ignite)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 CacheEntry (org.apache.ignite.cache.CacheEntry)1 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)1 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)1 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)1 QueryCursor (org.apache.ignite.cache.query.QueryCursor)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1