Search in sources :

Example 1 with IgniteException

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);
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 2 with IgniteException

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);
}
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 3 with IgniteException

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());
            }
        });
    }
}
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 4 with IgniteException

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);
        }
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with IgniteException

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;
        }
    });
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Aggregations

IgniteException (org.apache.ignite.IgniteException)414 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)114 Ignite (org.apache.ignite.Ignite)82 ClusterNode (org.apache.ignite.cluster.ClusterNode)47 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)42 ArrayList (java.util.ArrayList)40 UUID (java.util.UUID)40 CountDownLatch (java.util.concurrent.CountDownLatch)40 IOException (java.io.IOException)32 HashMap (java.util.HashMap)32 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)31 CacheException (javax.cache.CacheException)31 Transaction (org.apache.ignite.transactions.Transaction)28 CyclicBarrier (java.util.concurrent.CyclicBarrier)21 Map (java.util.Map)20 IgniteCache (org.apache.ignite.IgniteCache)19 ClusterStartNodeResult (org.apache.ignite.cluster.ClusterStartNodeResult)18 Nullable (org.jetbrains.annotations.Nullable)18 List (java.util.List)17 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)16