Search in sources :

Example 6 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 7 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 8 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 = new IgniteBiPredicate<UUID, TaskEvent>() {

        @Override
        public boolean apply(UUID nodeId, TaskEvent 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 = new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent evt) {
            return 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 : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) 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 9 with TaskEvent

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

the class PlatformContextImpl method writeEvent.

/** {@inheritDoc} */
@Override
public void writeEvent(BinaryRawWriterEx writer, Event evt) {
    assert writer != null;
    if (evt == null) {
        writer.writeInt(-1);
        return;
    }
    EventAdapter evt0 = (EventAdapter) evt;
    if (evt0 instanceof CacheEvent) {
        writer.writeInt(2);
        writeCommonEventData(writer, evt0);
        CacheEvent event0 = (CacheEvent) evt0;
        writer.writeString(event0.cacheName());
        writer.writeInt(event0.partition());
        writer.writeBoolean(event0.isNear());
        writeNode(writer, event0.eventNode());
        writer.writeObject(event0.key());
        writer.writeObject(event0.xid());
        writer.writeObject(event0.newValue());
        writer.writeObject(event0.oldValue());
        writer.writeBoolean(event0.hasOldValue());
        writer.writeBoolean(event0.hasNewValue());
        writer.writeUuid(event0.subjectId());
        writer.writeString(event0.closureClassName());
        writer.writeString(event0.taskName());
    } else if (evt0 instanceof CacheQueryExecutedEvent) {
        writer.writeInt(3);
        writeCommonEventData(writer, evt0);
        CacheQueryExecutedEvent event0 = (CacheQueryExecutedEvent) evt0;
        writer.writeString(event0.queryType());
        writer.writeString(event0.cacheName());
        writer.writeString(event0.className());
        writer.writeString(event0.clause());
        writer.writeUuid(event0.subjectId());
        writer.writeString(event0.taskName());
    } else if (evt0 instanceof CacheQueryReadEvent) {
        writer.writeInt(4);
        writeCommonEventData(writer, evt0);
        CacheQueryReadEvent event0 = (CacheQueryReadEvent) evt0;
        writer.writeString(event0.queryType());
        writer.writeString(event0.cacheName());
        writer.writeString(event0.className());
        writer.writeString(event0.clause());
        writer.writeUuid(event0.subjectId());
        writer.writeString(event0.taskName());
        writer.writeObject(event0.key());
        writer.writeObject(event0.value());
        writer.writeObject(event0.oldValue());
        writer.writeObject(event0.row());
    } else if (evt0 instanceof CacheRebalancingEvent) {
        writer.writeInt(5);
        writeCommonEventData(writer, evt0);
        CacheRebalancingEvent event0 = (CacheRebalancingEvent) evt0;
        writer.writeString(event0.cacheName());
        writer.writeInt(event0.partition());
        writeNode(writer, event0.discoveryNode());
        writer.writeInt(event0.discoveryEventType());
        writer.writeString(event0.discoveryEventName());
        writer.writeLong(event0.discoveryTimestamp());
    } else if (evt0 instanceof CheckpointEvent) {
        writer.writeInt(6);
        writeCommonEventData(writer, evt0);
        CheckpointEvent event0 = (CheckpointEvent) evt0;
        writer.writeString(event0.key());
    } else if (evt0 instanceof DiscoveryEvent) {
        writer.writeInt(7);
        writeCommonEventData(writer, evt0);
        DiscoveryEvent event0 = (DiscoveryEvent) evt0;
        writeNode(writer, event0.eventNode());
        writer.writeLong(event0.topologyVersion());
        writeNodes(writer, event0.topologyNodes());
    } else if (evt0 instanceof JobEvent) {
        writer.writeInt(8);
        writeCommonEventData(writer, evt0);
        JobEvent event0 = (JobEvent) evt0;
        writer.writeString(event0.taskName());
        writer.writeString(event0.taskClassName());
        writer.writeObject(event0.taskSessionId());
        writer.writeObject(event0.jobId());
        writeNode(writer, event0.taskNode());
        writer.writeUuid(event0.taskSubjectId());
    } else if (evt0 instanceof TaskEvent) {
        writer.writeInt(10);
        writeCommonEventData(writer, evt0);
        TaskEvent event0 = (TaskEvent) evt0;
        writer.writeString(event0.taskName());
        writer.writeString(event0.taskClassName());
        writer.writeObject(event0.taskSessionId());
        writer.writeBoolean(event0.internal());
        writer.writeUuid(event0.subjectId());
    } else
        throw new IgniteException("Unsupported event: " + evt);
}
Also used : CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) JobEvent(org.apache.ignite.events.JobEvent) IgniteException(org.apache.ignite.IgniteException) CacheEvent(org.apache.ignite.events.CacheEvent) TaskEvent(org.apache.ignite.events.TaskEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CheckpointEvent(org.apache.ignite.events.CheckpointEvent) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent) EventAdapter(org.apache.ignite.events.EventAdapter) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent)

Example 10 with TaskEvent

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

the class GridTaskWorker method recordTaskEvent.

/**
     * @param evtType Event type.
     * @param msg Event message.
     */
private void recordTaskEvent(int evtType, String msg) {
    if (!internal && ctx.event().isRecordable(evtType)) {
        Event evt = new TaskEvent(ctx.discovery().localNode(), msg, evtType, ses.getId(), ses.getTaskName(), ses.getTaskClassName(), internal, subjId);
        ctx.event().record(evt);
    }
}
Also used : TaskEvent(org.apache.ignite.events.TaskEvent) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event)

Aggregations

TaskEvent (org.apache.ignite.events.TaskEvent)19 Event (org.apache.ignite.events.Event)7 UUID (java.util.UUID)6 Ignite (org.apache.ignite.Ignite)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 IgniteException (org.apache.ignite.IgniteException)5 ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)5 JobEvent (org.apache.ignite.events.JobEvent)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)4 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)4 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 TaskSessionResource (org.apache.ignite.resources.TaskSessionResource)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 GridTestTaskSession (org.apache.ignite.GridTestTaskSession)2 Ignition (org.apache.ignite.Ignition)2 ComputeTaskTimeoutException (org.apache.ignite.compute.ComputeTaskTimeoutException)2