use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class JavaThinClient method continuousQueries.
void continuousQueries() throws Exception {
ClientConfiguration clientCfg = new ClientConfiguration().setAddresses("127.0.0.1:10800");
try (IgniteClient client = Ignition.startClient(clientCfg)) {
// tag::continuous-queries[]
ClientCache<Integer, String> cache = client.getOrCreateCache("myCache");
ContinuousQuery<Integer, String> query = new ContinuousQuery<>();
query.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> events) throws CacheEntryListenerException {
// react to the update events here
}
});
ClientDisconnectListener disconnectListener = new ClientDisconnectListener() {
@Override
public void onDisconnected(Exception reason) {
// react to the disconnect event here
}
};
cache.query(query, disconnectListener);
// end::continuous-queries[]
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteCacheProxyImpl method query.
/**
* {@inheritDoc}
*/
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
GridCacheContext<K, V> ctx = getContextSafe();
A.notNull(qry, "qry");
try {
ctx.checkSecurity(SecurityPermission.CACHE_READ);
validate(qry);
convertToBinary(qry);
CacheOperationContext opCtxCall = ctx.operationContextPerCall();
boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
if (qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer)
return (QueryCursor<R>) queryContinuous((AbstractContinuousQuery) qry, qry.isLocal(), keepBinary);
if (qry instanceof SqlQuery)
return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
if (qry instanceof SqlFieldsQuery)
return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, null, keepBinary, true).get(0);
if (qry instanceof ScanQuery)
return query((ScanQuery) qry, null, projection(qry.isLocal()));
return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
} catch (IgniteCheckedException e) {
throw cacheException(e);
} catch (Exception e) {
if (e instanceof CacheException)
throw (CacheException) e;
throw new CacheException(e.getMessage(), e);
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class JavaThinCompatibilityTest method testContinuousQueries.
/**
*/
private void testContinuousQueries() throws Exception {
X.println(">>>> Testing continuous queries");
try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
ClientCache<Object, Object> cache = client.getOrCreateCache("testContinuousQueries");
List<CacheEntryEvent<?, ?>> allEvts = new ArrayList<>();
cache.query(new ContinuousQuery<>().setLocalListener(evts -> evts.forEach(allEvts::add)));
cache.put(0, 0);
cache.put(0, 1);
cache.remove(0);
assertTrue(GridTestUtils.waitForCondition(() -> allEvts.size() == 3, 1_000L));
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class UsingContinuousQueries method localListenerExample.
public static void localListenerExample() {
try (Ignite ignite = Ignition.start()) {
// tag::localListener[]
IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
ContinuousQuery<Integer, String> query = new ContinuousQuery<>();
query.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> events) throws CacheEntryListenerException {
// react to the update events here
}
});
cache.query(query);
// end::localListener[]
}
}
use of org.apache.ignite.cache.query.ContinuousQuery 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");
}
}
Aggregations