use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteDynamicCacheStartSelfTest method testServerNodesLeftEvent.
/**
* @throws Exception If failed.
*/
public void testServerNodesLeftEvent() throws Exception {
testAttribute = false;
startGrid(nodeCount());
CacheConfiguration cfg = new CacheConfiguration(DYNAMIC_CACHE_NAME);
cfg.setNodeFilter(F.not(NODE_FILTER));
IgniteCache<Object, Object> cache = ignite(0).createCache(cfg);
final CountDownLatch[] latches = new CountDownLatch[nodeCount()];
IgnitePredicate[] lsnrs = new IgnitePredicate[nodeCount()];
for (int i = 0; i < nodeCount(); i++) {
final int idx = i;
latches[i] = new CountDownLatch(1);
lsnrs[i] = new IgnitePredicate<CacheEvent>() {
@Override
public boolean apply(CacheEvent e) {
switch(e.type()) {
case EventType.EVT_CACHE_NODES_LEFT:
latches[idx].countDown();
break;
default:
assert false;
}
assertEquals(DYNAMIC_CACHE_NAME, e.cacheName());
return true;
}
};
ignite(i).events().localListen(lsnrs[i], EventType.EVTS_CACHE_LIFECYCLE);
}
stopGrid(nodeCount());
for (CountDownLatch latch : latches) latch.await();
for (int i = 0; i < nodeCount(); i++) ignite(i).events().stopLocalListen(lsnrs[i]);
cache.destroy();
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class PlatformEvents method startWaitForLocal.
/**
* Starts the waitForLocal.
*
* @param reader Reader
* @param events Events.
* @return Result.
*/
private EventAdapter startWaitForLocal(BinaryRawReaderEx reader, IgniteEvents events) {
Long filterHnd = reader.readObject();
IgnitePredicate filter = filterHnd != null ? localFilter(filterHnd) : null;
int[] eventTypes = readEventTypes(reader);
return (EventAdapter) events.waitForLocal(filter, eventTypes);
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class CacheEventsExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws IgniteException If example execution failed.
*/
public static void main(String[] args) throws IgniteException, InterruptedException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Cache events example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
// This optional local callback is called for each event notification
// that passed remote predicate listener.
IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {
@Override
public boolean apply(UUID uuid, CacheEvent evt) {
System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() + ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue());
// Continue listening.
return true;
}
};
// Remote listener which only accepts events for keys that are
// greater or equal than 10 and if event node is primary for this key.
IgnitePredicate<CacheEvent> rmtLsnr = new IgnitePredicate<CacheEvent>() {
@Override
public boolean apply(CacheEvent evt) {
System.out.println("Cache event [name=" + evt.name() + ", key=" + evt.key() + ']');
int key = evt.key();
return key >= 10 && ignite.affinity(CACHE_NAME).isPrimary(ignite.cluster().localNode(), key);
}
};
// Subscribe to specified cache events on all nodes that have cache running.
// Cache events are explicitly enabled in examples/config/example-ignite.xml file.
ignite.events(ignite.cluster().forCacheNodes(CACHE_NAME)).remoteListen(locLsnr, rmtLsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
// Generate cache events.
for (int i = 0; i < 20; 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);
}
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class EventsExample method localListen.
/**
* Listen to events that happen only on local node.
*
* @throws IgniteException If failed.
*/
private static void localListen() throws IgniteException {
System.out.println();
System.out.println(">>> Local event listener example.");
Ignite ignite = Ignition.ignite();
IgnitePredicate<TaskEvent> lsnr = evt -> {
System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');
// Return true to continue listening.
return true;
};
// Register event listener for all local task execution events.
ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION);
// Generate task events.
ignite.compute().withName("example-event-task").run(() -> System.out.println("Executing sample job."));
// Unsubscribe local task event listener.
ignite.events().stopLocalListen(lsnr);
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridCachePartitionedUnloadEventsSelfTest method checkPartitionUnloadEvents.
/**
* @param evts Events.
* @param g Grid.
* @param parts Parts.
*/
private void checkPartitionUnloadEvents(Collection<Event> evts, Ignite g, Collection<GridDhtLocalPartition> parts) {
assertEquals(parts.size(), evts.size());
for (Event evt : evts) {
CacheRebalancingEvent unloadEvt = (CacheRebalancingEvent) evt;
final int part = unloadEvt.partition();
assertNotNull("Unexpected partition: " + part, F.find(parts, null, new IgnitePredicate<GridDhtLocalPartition>() {
@Override
public boolean apply(GridDhtLocalPartition e) {
return e.id() == part;
}
}));
assertEquals(g.cache(DEFAULT_CACHE_NAME).getName(), unloadEvt.cacheName());
assertEquals(g.cluster().localNode().id(), unloadEvt.node().id());
}
}
Aggregations