use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.
the class CacheContinuousQueryHandlerV3 method register.
/**
* {@inheritDoc}
*/
@Override
public RegisterStatus register(UUID nodeId, UUID routineId, GridKernalContext ctx) throws IgniteCheckedException {
final IgniteClosure trans = getTransformer();
if (trans != null)
ctx.resource().injectGeneric(trans);
if (locTransLsnr != null) {
ctx.resource().injectGeneric(locTransLsnr);
asyncCb = U.hasAnnotation(locTransLsnr, IgniteAsyncCallback.class);
}
return super.register(nodeId, routineId, ctx);
}
use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.
the class CacheContinuousQueryWithTransformerExample 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 with transformer example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, Organization> cache = ignite.getOrCreateCache(CACHE_NAME)) {
// Create new continuous query with transformer.
ContinuousQueryWithTransformer<Integer, Organization, String> qry = new ContinuousQueryWithTransformer<>();
// Factory to create transformers.
Factory<IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>> factory = FactoryBuilder.factoryOf(// Only this field will be sent over to a local node over the network.
(IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>) event -> event.getValue().name());
qry.setRemoteTransformerFactory(factory);
// Listener that will receive transformed data.
qry.setLocalListener(names -> {
for (String name : names) System.out.println("New organization name: " + name);
});
// Execute query.
try (QueryCursor<Cache.Entry<Integer, Organization>> cur = cache.query(qry)) {
populateCache(cache);
// 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);
}
}
}
use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocalInjection.
/**
* @throws Exception If failed.
*/
public void testLocalInjection() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Boolean>> lists = grid().compute().broadcast(new IgniteCallable<List<Boolean>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Boolean> call() throws Exception {
IgniteClosure<Cache.Entry<Integer, Value>, Boolean> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Boolean>() {
@IgniteInstanceResource
Ignite ignite;
@Override
public Boolean apply(Cache.Entry<Integer, Value> e) {
return ignite != null;
}
};
return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
}
});
List<Boolean> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(50, res.size());
for (int i = 0; i < 50; i++) assertEquals(Boolean.TRUE, res.get(i));
} finally {
cache.destroy();
}
}
use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocalKeepBinary.
/**
* @throws Exception If failed.
*/
public void testLocalKeepBinary() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Integer> call() throws Exception {
IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {
@Override
public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
return e.getValue().field("idx");
}
};
return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<Integer, BinaryObject>().setLocal(true), transformer).getAll();
}
});
List<Integer> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(50, res.size());
Collections.sort(res);
for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
} finally {
cache.destroy();
}
}
use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.
the class GridCacheQueryTransformerSelfTest method testLocal.
/**
* @throws Exception If failed.
*/
public void testLocal() throws Exception {
IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
try {
for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public List<Integer> call() throws Exception {
IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {
@Override
public Integer apply(Cache.Entry<Integer, Value> e) {
return e.getValue().idx;
}
};
return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
}
});
List<Integer> res = new ArrayList<>(F.flatCollections(lists));
assertEquals(50, res.size());
Collections.sort(res);
for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
} finally {
cache.destroy();
}
}
Aggregations