Search in sources :

Example 1 with TaskEvent

use of org.apache.ignite.events.TaskEvent 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;
    };
    // 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 2 with TaskEvent

use of org.apache.ignite.events.TaskEvent in project ignite by apache.

the class EventsExample method remoteListen.

/**
     * Listen to events coming from all cluster nodes.
     *
     * @throws IgniteException If failed.
     */
private static void remoteListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Remote event listener example.");
    // This optional local callback is called for each event notification
    // that passed remote predicate listener.
    IgniteBiPredicate<UUID, TaskEvent> locLsnr = (nodeId, evt) -> {
        // Remote filter only accepts tasks whose name being with "good-task" prefix.
        assert evt.taskName().startsWith("good-task");
        System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
        // Return true to continue listening.
        return true;
    };
    // Remote filter which only accepts tasks whose name begins with "good-task" prefix.
    IgnitePredicate<TaskEvent> rmtLsnr = evt -> evt.taskName().startsWith("good-task");
    Ignite ignite = Ignition.ignite();
    // Register event listeners on all nodes to listen for task events.
    ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    for (int i = 0; i < 10; i++) {
        ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {

            // Auto-inject task session.
            @TaskSessionResource
            private ComputeTaskSession ses;

            @Override
            public void run() {
                System.out.println("Executing sample job for task: " + ses.getTaskName());
            }
        });
    }
}
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) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) UUID(java.util.UUID) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 3 with TaskEvent

use of org.apache.ignite.events.TaskEvent in project ignite by apache.

the class TaskEventSubjectIdSelfTest method testFailedTask.

/**
 * @throws Exception If failed.
 */
public void testFailedTask() throws Exception {
    latch = new CountDownLatch(2);
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            grid().compute().execute(new FailedTask(), null);
            return null;
        }
    }, IgniteCheckedException.class, null);
    assert latch.await(1000, MILLISECONDS);
    assertEquals(2, evts.size());
    Iterator<TaskEvent> it = evts.iterator();
    assert it.hasNext();
    TaskEvent evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_STARTED, evt.type());
    assertEquals(nodeId, evt.subjectId());
    assert it.hasNext();
    evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_FAILED, evt.type());
    assertEquals(nodeId, evt.subjectId());
    assert !it.hasNext();
}
Also used : TaskEvent(org.apache.ignite.events.TaskEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ComputeTaskTimeoutException(org.apache.ignite.compute.ComputeTaskTimeoutException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Example 4 with TaskEvent

use of org.apache.ignite.events.TaskEvent in project ignite by apache.

the class TaskEventSubjectIdSelfTest method testClient.

/**
 * @throws Exception If failed.
 */
public void testClient() throws Exception {
    latch = new CountDownLatch(3);
    client.compute().execute(SimpleTask.class.getName(), null);
    assert latch.await(1000, MILLISECONDS);
    assertEquals(3, evts.size());
    Iterator<TaskEvent> it = evts.iterator();
    assert it.hasNext();
    TaskEvent evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_STARTED, evt.type());
    assertEquals(client.id(), evt.subjectId());
    assert it.hasNext();
    evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_REDUCED, evt.type());
    assertEquals(client.id(), evt.subjectId());
    assert it.hasNext();
    evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_FINISHED, evt.type());
    assertEquals(client.id(), evt.subjectId());
    assert !it.hasNext();
}
Also used : TaskEvent(org.apache.ignite.events.TaskEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with TaskEvent

use of org.apache.ignite.events.TaskEvent in project ignite by apache.

the class TaskEventSubjectIdSelfTest method testTimedOutTask.

/**
 * @throws Exception If failed.
 */
public void testTimedOutTask() throws Exception {
    latch = new CountDownLatch(2);
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            grid().compute().withTimeout(100).execute(new TimedOutTask(), null);
            return null;
        }
    }, ComputeTaskTimeoutException.class, null);
    assert latch.await(1000, MILLISECONDS);
    assertEquals(3, evts.size());
    Iterator<TaskEvent> it = evts.iterator();
    assert it.hasNext();
    TaskEvent evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_STARTED, evt.type());
    assertEquals(nodeId, evt.subjectId());
    assert it.hasNext();
    evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_TIMEDOUT, evt.type());
    assertEquals(nodeId, evt.subjectId());
    assert it.hasNext();
    evt = it.next();
    assert evt != null;
    assertEquals(EVT_TASK_FAILED, evt.type());
    assertEquals(nodeId, evt.subjectId());
    assert !it.hasNext();
}
Also used : TaskEvent(org.apache.ignite.events.TaskEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ComputeTaskTimeoutException(org.apache.ignite.compute.ComputeTaskTimeoutException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Aggregations

TaskEvent (org.apache.ignite.events.TaskEvent)23 Event (org.apache.ignite.events.Event)9 UUID (java.util.UUID)8 IgniteException (org.apache.ignite.IgniteException)8 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Ignite (org.apache.ignite.Ignite)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)6 JobEvent (org.apache.ignite.events.JobEvent)6 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)6 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 Ignition (org.apache.ignite.Ignition)4 EVTS_TASK_EXECUTION (org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION)3 ExampleNodeStartup (org.apache.ignite.examples.ExampleNodeStartup)3 VisorTaskArgument (org.apache.ignite.internal.visor.VisorTaskArgument)3 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 TaskSessionResource (org.apache.ignite.resources.TaskSessionResource)3 ArrayList (java.util.ArrayList)2