Search in sources :

Example 61 with IgnitePredicate

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();
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) CountDownLatch(java.util.concurrent.CountDownLatch) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 62 with IgnitePredicate

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);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) EventAdapter(org.apache.ignite.events.EventAdapter)

Example 63 with IgnitePredicate

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);
        }
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 64 with IgnitePredicate

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);
}
Also used : EVTS_TASK_EXECUTION(org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION) Ignition(org.apache.ignite.Ignition) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) IgniteException(org.apache.ignite.IgniteException) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) TaskEvent(org.apache.ignite.events.TaskEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite)

Example 65 with IgnitePredicate

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());
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Event(org.apache.ignite.events.Event) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)81 Event (org.apache.ignite.events.Event)44 CountDownLatch (java.util.concurrent.CountDownLatch)35 Ignite (org.apache.ignite.Ignite)32 UUID (java.util.UUID)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)18 ArrayList (java.util.ArrayList)15 Ignition (org.apache.ignite.Ignition)10 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)10 Collection (java.util.Collection)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)8 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)7 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7