use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.
the class CacheContinuousQueryFilterDeploymentFailedTest method testContinuousQueryFilterDeploymentFailed.
/**
* Tests continuous query behavior in case of filter deployment obtaining failure.
*
* @throws Exception If failed.
*/
@Test
@SuppressWarnings({ "ThrowableNotThrown" })
public void testContinuousQueryFilterDeploymentFailed() throws Exception {
startGrids(NODES_CNT - 1);
IgniteEx cli = startClientGrid(NODES_CNT - 1);
IgniteCache<Integer, Integer> cache = cli.createCache(DEFAULT_CACHE_NAME);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
Class<Factory<CacheEntryEventFilter<Integer, Integer>>> rmtFilterFactoryCls = (Class<Factory<CacheEntryEventFilter<Integer, Integer>>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
qry.setRemoteFilterFactory(rmtFilterFactoryCls.newInstance());
spi(grid(1)).blockMessages((node, msg) -> msg instanceof GridDeploymentRequest);
assertThrowsWithCause(() -> cache.query(qry), CacheException.class);
assertTrue(stopRoutineLatch.await(getTestTimeout(), MILLISECONDS));
assertTrue(allGrids().stream().allMatch(g -> ((IgniteEx) g).context().systemView().view(CQ_SYS_VIEW).size() == 0));
}
use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.
the class CacheContinuousQueryDeploymentToClientTest method testDeploymentToNewClient.
/**
* Test starts 1 server node and 1 client node. The client node deploys
* CQ for the cache {@link #CACHE_NAME}. After that another client node is started.
* Expected that CQ won't be deployed to the new client, since the client doesn't
* store any data.
*
* @throws Exception If failed.
*/
@Test
public void testDeploymentToNewClient() throws Exception {
startGrid(0);
IgniteEx client1 = startClientGrid(1);
IgniteCache<Integer, String> cache = client1.cache(CACHE_NAME);
AbstractContinuousQuery<Integer, String> qry = new ContinuousQuery<Integer, String>().setLocalListener(evts -> {
// No-op.
}).setRemoteFilterFactory((Factory<CacheEntryEventFilter<Integer, String>>) () -> evt -> true);
cache.query(qry);
IgniteEx client2 = startClientGrid(2);
GridContinuousProcessor proc = client2.context().continuous();
assertEquals(0, ((Map<?, ?>) U.field(proc, "locInfos")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "rmtInfos")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "startFuts")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "stopFuts")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "bufCheckThreads")).size());
}
use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.
the class CacheContinuousQueryDeploymentToClientTest method testDeploymentToExistingClient.
/**
* Test starts 1 server node and 2 client nodes. The first client node deploys
* CQ for the cache {@link #CACHE_NAME}.
* Expected that CQ won't be deployed to the second client, since the client doesn't
* store any data.
*
* @throws Exception If failed.
*/
@Test
public void testDeploymentToExistingClient() throws Exception {
startGrid(0);
IgniteEx client1 = startClientGrid(1);
IgniteCache<Integer, String> cache = client1.cache(CACHE_NAME);
IgniteEx client2 = startClientGrid(2);
AbstractContinuousQuery<Integer, String> qry = new ContinuousQuery<Integer, String>().setLocalListener(evts -> {
// No-op.
}).setRemoteFilterFactory((Factory<CacheEntryEventFilter<Integer, String>>) () -> evt -> true);
cache.query(qry);
GridContinuousProcessor proc = client2.context().continuous();
assertEquals(0, ((Map<?, ?>) U.field(proc, "locInfos")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "rmtInfos")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "startFuts")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "stopFuts")).size());
assertEquals(0, ((Map<?, ?>) U.field(proc, "bufCheckThreads")).size());
}
use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.
the class CacheContinuousAsyncQueryExample 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 CacheEntryFilter();
}
});
// 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 = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
// Wait for a while while callback is notified about remaining puts.
Thread.sleep(2000);
}
// Iterate through entries which was updated from filter.
for (int i = 0; i < 10; i++) System.out.println("Entry updated from filter [key=" + i + ", val=" + cache.get(i) + ']');
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(CACHE_NAME);
}
}
}
use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.
the class ClientCacheEntryListenerHandler method startListen.
/**
* Send request to the server and start
*/
public synchronized void startListen(CacheEntryUpdatedListener<K, V> locLsnr, ClientDisconnectListener disconnectLsnr, Factory<? extends CacheEntryEventFilter<? super K, ? super V>> rmtFilterFactory, int pageSize, long timeInterval, boolean includeExpired) {
assert locLsnr != null;
if (clientCh != null)
throw new IllegalStateException("Listener was already started");
this.locLsnr = locLsnr;
this.disconnectLsnr = disconnectLsnr;
Consumer<PayloadOutputChannel> qryWriter = payloadCh -> {
BinaryOutputStream out = payloadCh.out();
out.writeInt(ClientUtils.cacheId(jCacheAdapter.getName()));
out.writeByte(keepBinary ? KEEP_BINARY_FLAG_MASK : 0);
out.writeInt(pageSize);
out.writeLong(timeInterval);
out.writeBoolean(includeExpired);
if (rmtFilterFactory == null)
out.writeByte(GridBinaryMarshaller.NULL);
else {
utils.writeObject(out, rmtFilterFactory);
out.writeByte(JAVA_PLATFORM);
}
};
Function<PayloadInputChannel, T2<ClientChannel, Long>> qryReader = payloadCh -> {
ClientChannel ch = payloadCh.clientChannel();
Long rsrcId = payloadCh.in().readLong();
ch.addNotificationListener(CONTINUOUS_QUERY_EVENT, rsrcId, this);
return new T2<>(ch, rsrcId);
};
try {
T2<ClientChannel, Long> params = ch.service(ClientOperation.QUERY_CONTINUOUS, qryWriter, qryReader);
clientCh = params.get1();
rsrcId = params.get2();
} catch (ClientError e) {
throw new ClientException(e);
}
}
Aggregations