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);
}
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());
}
});
}
}
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());
}
});
}
}
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);
}
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);
}
}
Aggregations