use of org.apache.ignite.IgniteException in project ignite by apache.
the class ClientHttpTask method split.
/** {@inheritDoc} */
@Override
protected Collection<? extends ComputeJob> split(int gridSize, String arg) {
try {
JsonNode json = JSON_MAPPER.readTree(arg);
List<String> list = null;
if (json.isArray()) {
list = new ArrayList<>();
for (JsonNode child : json) list.add(child.asText());
}
return delegate.split(gridSize, list);
} catch (IOException e) {
throw new IgniteException(e);
}
}
use of org.apache.ignite.IgniteException 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.IgniteException 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.IgniteException in project ignite by apache.
the class MessagingPingPongExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
// Game is played over the default ignite.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2))
return;
System.out.println();
System.out.println(">>> Messaging ping-pong example started.");
// Pick random remote node as a partner.
ClusterGroup nodeB = ignite.cluster().forRemotes().forRandom();
// Note that both nodeA and nodeB will always point to
// same nodes regardless of whether they were implicitly
// serialized and deserialized on another node as part of
// anonymous closure's state during its remote execution.
// Set up remote player.
ignite.message(nodeB).remoteListen(null, (nodeId, rcvMsg) -> {
System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']');
if ("PING".equals(rcvMsg)) {
ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PONG");
// Continue listening.
return true;
}
// Unsubscribe.
return false;
});
int MAX_PLAYS = 10;
final CountDownLatch cnt = new CountDownLatch(MAX_PLAYS);
// Set up local player.
ignite.message().localListen(null, (nodeId, rcvMsg) -> {
System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']');
if (cnt.getCount() == 1) {
ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "STOP");
cnt.countDown();
// Stop listening.
return false;
} else if ("PONG".equals(rcvMsg))
ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PING");
else
throw new IgniteException("Received unexpected message: " + rcvMsg);
cnt.countDown();
// Continue listening.
return true;
});
// Serve!
ignite.message(nodeB).send(null, "PING");
// Wait til the game is over.
try {
cnt.await();
} catch (InterruptedException e) {
System.err.println("Hm... let us finish the game!\n" + e);
}
}
}
use of org.apache.ignite.IgniteException in project ignite by apache.
the class MessagingExample method startListening.
/**
* Start listening to messages on remote cluster nodes.
*
* @param msg Ignite messaging.
*/
private static void startListening(IgniteMessaging msg) {
// Add ordered message listener.
msg.remoteListen(EXAMPLE_TOPIC.ORDERED, new IgniteBiPredicate<UUID, String>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public boolean apply(UUID nodeId, String msg) {
System.out.println("Received ordered message [msg=" + msg + ", fromNodeId=" + nodeId + ']');
try {
ignite.message(ignite.cluster().forNodeId(nodeId)).send(EXAMPLE_TOPIC.ORDERED, msg);
} catch (IgniteException e) {
e.printStackTrace();
}
// Return true to continue listening.
return true;
}
});
// Add unordered message listener.
msg.remoteListen(EXAMPLE_TOPIC.UNORDERED, new IgniteBiPredicate<UUID, String>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public boolean apply(UUID nodeId, String msg) {
System.out.println("Received unordered message [msg=" + msg + ", fromNodeId=" + nodeId + ']');
try {
ignite.message(ignite.cluster().forNodeId(nodeId)).send(EXAMPLE_TOPIC.UNORDERED, msg);
} catch (IgniteException e) {
e.printStackTrace();
}
// Return true to continue listening.
return true;
}
});
}
Aggregations