Search in sources :

Example 1 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class ComputeBroadcastExample method hello.

/**
     * Print 'Hello' message on all nodes.
     *
     * @param ignite Ignite instance.
     * @throws IgniteException If failed.
     */
private static void hello(Ignite ignite) throws IgniteException {
    // Print out hello message on all nodes.
    ignite.compute().broadcast(new IgniteRunnable() {

        @Override
        public void run() {
            System.out.println();
            System.out.println(">>> Hello Node! :)");
        }
    });
    System.out.println();
    System.out.println(">>> Check all nodes for hello message output.");
}
Also used : IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 2 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable 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 IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project camel by apache.

the class IgniteComputeProducer method doAffinityRun.

private void doAffinityRun(final Exchange exchange, final AsyncCallback callback, IgniteCompute compute) throws Exception {
    IgniteRunnable job = exchange.getIn().getBody(IgniteRunnable.class);
    String affinityCache = exchange.getIn().getHeader(IgniteConstants.IGNITE_COMPUTE_AFFINITY_CACHE_NAME, String.class);
    Object affinityKey = exchange.getIn().getHeader(IgniteConstants.IGNITE_COMPUTE_AFFINITY_KEY, Object.class);
    if (job == null || affinityCache == null || affinityKey == null) {
        throw new RuntimeCamelException(String.format("Ignite Compute endpoint with AFFINITY_RUN executionType is only " + "supported for IgniteRunnable payloads, along with an affinity cache and key. The payload type was: %s.", exchange.getIn().getBody().getClass().getName()));
    }
    compute.affinityRun(affinityCache, affinityKey, job);
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 4 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class IgniteCacheLockPartitionOnAffinityRunTest method testCheckReservePartitionException.

/**
 * @throws Exception If failed.
 */
public void testCheckReservePartitionException() throws Exception {
    int orgId = primaryKey(grid(1).cache(Organization.class.getSimpleName()));
    try {
        grid(0).compute().affinityRun(Arrays.asList(Organization.class.getSimpleName(), OTHER_CACHE_NAME), new Integer(orgId), new IgniteRunnable() {

            @Override
            public void run() {
            // No-op.
            }
        });
        fail("Exception is expected");
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Failed partition reservation. Partition is not primary on the node."));
    }
    try {
        grid(0).compute().affinityCall(Arrays.asList(Organization.class.getSimpleName(), OTHER_CACHE_NAME), new Integer(orgId), new IgniteCallable<Object>() {

            @Override
            public Object call() throws Exception {
                return null;
            }
        });
        fail("Exception is expected");
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Failed partition reservation. Partition is not primary on the node."));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 5 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method onDynamicCacheChangeFail.

/**
 * Cache change failure message callback, processed from the discovery thread.
 *
 * @param node Message sender node.
 * @param msg Failure message.
 */
public void onDynamicCacheChangeFail(final ClusterNode node, final DynamicCacheChangeFailureMessage msg) {
    assert exchId.equals(msg.exchangeId()) : msg;
    assert firstDiscoEvt.type() == EVT_DISCOVERY_CUSTOM_EVT && dynamicCacheStartExchange();
    final ExchangeActions actions = exchangeActions();
    onDiscoveryEvent(new IgniteRunnable() {

        @Override
        public void run() {
            // The rollbackExchange() method has to wait for checkpoint.
            // That operation is time consumed, and therefore it should be executed outside the discovery thread.
            cctx.kernalContext().pools().getSystemExecutorService().submit(new Runnable() {

                @Override
                public void run() {
                    if (isDone() || !enterBusy())
                        return;
                    try {
                        assert msg.error() != null : msg;
                        // Try to revert all the changes that were done during initialization phase
                        cctx.affinity().forceCloseCaches(GridDhtPartitionsExchangeFuture.this, crd.isLocal(), msg.exchangeActions());
                        synchronized (mux) {
                            finishState = new FinishState(crd.id(), initialVersion(), null);
                            state = ExchangeLocalState.DONE;
                        }
                        if (actions != null)
                            actions.completeRequestFutures(cctx, msg.error());
                        onDone(exchId.topologyVersion());
                    } catch (Throwable e) {
                        onDone(e);
                    } finally {
                        leaveBusy();
                    }
                }
            });
        }
    });
}
Also used : ExchangeActions(org.apache.ignite.internal.processors.cache.ExchangeActions) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)57 Test (org.junit.Test)30 Ignite (org.apache.ignite.Ignite)25 IgniteException (org.apache.ignite.IgniteException)19 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)19 IgniteEx (org.apache.ignite.internal.IgniteEx)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)11 UUID (java.util.UUID)10 ArrayList (java.util.ArrayList)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 IgniteFuture (org.apache.ignite.lang.IgniteFuture)6 IOException (java.io.IOException)5 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)5 List (java.util.List)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 Channel (java.nio.channels.Channel)3 Collection (java.util.Collection)3