Java源码示例:org.apache.ignite.IgniteCompute

示例1
public static void main(String[] args) throws IOException {

        try (Ignite ignite = Ignition.start(CLIENT_CONFIG)) {
            IgniteCompute compute = ignite.compute();
            CacheConfiguration cacheConfiguration = new CacheConfiguration("checkpoints");
            // explicitly uses of checkpoint
            CacheCheckpointSpi cacheCheckpointSpi = new CacheCheckpointSpi();
            cacheCheckpointSpi.setCacheName("checkpointCache");
            //cacheConfiguration.setC
            ignite.configuration().setCheckpointSpi(cacheCheckpointSpi)
                                  .setFailoverSpi(new AlwaysFailoverSpi());
            // create or get cache
            ignite.getOrCreateCache(cacheConfiguration);

            ValidateMessage[] validateMessages = TestDataGenerator.getValidateMessages();

            Boolean result = compute.execute(new ForkJoinWithCheckpointComputation(), validateMessages);
            System.out.println("final result=" + result);
        }
    }
 
示例2
public static void main(String[] args) throws IOException {
    try (Ignite ignite = Ignition.start(CLIENT_CONFIG)) {
        IgniteCompute compute = ignite.compute();

        // Execute closure on all cluster nodes.
        Collection<Integer> res = compute.apply(
                new IgniteClosure<String, Integer>() {
                    @Override
                    public Integer apply(String word) {
                        // Return number of letters in the word.
                        return word.length();
                    }
                },
                Arrays.asList("Count characters using closure".split(" "))
        );

        int sum = 0;

        // Add up individual word lengths received from remote nodes
        for (int len : res)
            sum += len;
        System.out.println("Length of the sentence: "+ sum);
    }
}
 
示例3
public static void main(String[] args) throws IOException {
    String sample1 = TestDataGenerator.getSample1();
    byte[] vaidateSchema = TestDataGenerator.getValidateSchema();
    String validateScript = TestDataGenerator.getValidateScript();

    try (Ignite ignite = Ignition.start(CLIENT_CONFIG)) {
        IgniteCompute compute = ignite.compute();

        Boolean result = compute.call(() -> {
            boolean validateXsdResult = XsdValidator.validate(sample1, vaidateSchema);
            boolean validateByJs = JSEvaluate.evaluateJs(sample1, validateScript);

            System.out.println("validateXsdResult=" + validateXsdResult);
            System.out.println("validateByJs=" + validateByJs);

            return validateXsdResult && validateByJs;
        });

        System.out.println("result=" + result);
    }
}
 
示例4
/**
 * @throws Exception If failed.
 */
@Test
public void testL2Cache() throws Exception {
    Ignite srv = ignite(0);

    {
        IgniteCompute client1Compute =
            srv.compute(srv.cluster().forNodeId(ignite(1).cluster().localNode().id()));

        client1Compute.run(new HibernateInsertRunnable());
    }

    {
        IgniteCompute client2Compute =
            srv.compute(srv.cluster().forNodeId(ignite(2).cluster().localNode().id()));

        client2Compute.run(new HibernateLoadRunnable());
    }

    {
        IgniteCompute srvCompute = srv.compute(srv.cluster().forLocal());

        srvCompute.run(new HibernateLoadRunnable());
    }
}
 
示例5
/**
 * @throws Exception If failed.
 */
@Test
public void testL2Cache() throws Exception {
    Ignite srv = ignite(0);

    {
        IgniteCompute client1Compute =
            srv.compute(srv.cluster().forNodeId(ignite(1).cluster().localNode().id()));

        client1Compute.run(new HibernateInsertRunnable());
    }

    {
        IgniteCompute client2Compute =
            srv.compute(srv.cluster().forNodeId(ignite(2).cluster().localNode().id()));

        client2Compute.run(new HibernateLoadRunnable());
    }

    {
        IgniteCompute srvCompute = srv.compute(srv.cluster().forLocal());

        srvCompute.run(new HibernateLoadRunnable());
    }
}
 
示例6
/**
 * @throws Exception If failed.
 */
@Test
public void testL2Cache() throws Exception {
    Ignite srv = ignite(0);

    {
        IgniteCompute client1Compute =
            srv.compute(srv.cluster().forNodeId(ignite(1).cluster().localNode().id()));

        client1Compute.run(new HibernateInsertRunnable());
    }

    {
        IgniteCompute client2Compute =
            srv.compute(srv.cluster().forNodeId(ignite(2).cluster().localNode().id()));

        client2Compute.run(new HibernateLoadRunnable());
    }

    {
        IgniteCompute srvCompute = srv.compute(srv.cluster().forLocal());

        srvCompute.run(new HibernateLoadRunnable());
    }
}
 
示例7
/**
 * Execute method on grid.
 *
 * @param compute {@link org.apache.ignite.IgniteCompute} instance.
 * @param cls Joint point signature class.
 * @param arg GridifyArgument with all method signature parameters.
 * @param nodeFilter Node filter.
 * @param threshold Parameter that defines the minimal value below which the
 *      execution will NOT be grid-enabled.
 * @param splitSize Size of elements to send in job argument.
 * @param timeout Execution timeout.
 * @return Result.
 * @throws IgniteCheckedException If execution failed.
 */
protected Object execute(IgniteCompute compute, Class<?> cls, GridifyRangeArgument arg,
    GridifyNodeFilter nodeFilter, int threshold, int splitSize, long timeout) throws IgniteCheckedException {
    long now = U.currentTimeMillis();

    long end = timeout == 0 ? Long.MAX_VALUE : timeout + now;

    // Prevent overflow.
    if (end < 0)
        end = Long.MAX_VALUE;

    if (now > end)
        throw new ComputeTaskTimeoutCheckedException("Timeout occurred while waiting for completion.");

    Collection<?> res = compute.withTimeout(timeout == 0 ? 0L : (end - now)).execute(
        new GridifyDefaultRangeTask(cls, nodeFilter, threshold, splitSize, false),
        arg);

    return result(arg.getMethodReturnType(), res);
}
 
示例8
/**
 * Execute task taking arguments from the given reader.
 *
 * @param reader Reader.
 * @param async Execute asynchronously flag.
 * @return Task result.
 * @throws IgniteCheckedException On error.
 */
protected Object executeJavaTask(BinaryRawReaderEx reader, boolean async) throws IgniteCheckedException {
    String taskName = reader.readString();
    boolean keepBinary = reader.readBoolean();
    Object arg = reader.readObjectDetached();

    Collection<UUID> nodeIds = readNodeIds(reader);

    IgniteCompute compute0 = computeForTask(nodeIds);

    if (!keepBinary && arg instanceof BinaryObjectImpl)
        arg = ((BinaryObject)arg).deserialize();

    if (async)
        return readAndListenFuture(reader, new ComputeConvertingFuture(compute0.executeAsync(taskName, arg)));
    else
        return toBinary(compute0.execute(taskName, arg));
}
 
示例9
/** {@inheritDoc} */
@Override protected Void run(VisorComputeCancelSessionsTaskArg arg) {
    Set<IgniteUuid> sesIds = arg.getSessionIds();

    if (sesIds != null && !sesIds.isEmpty()) {
        IgniteCompute compute = ignite.compute(ignite.cluster().forLocal());

        Map<IgniteUuid, ComputeTaskFuture<Object>> futs = compute.activeTaskFutures();

        for (IgniteUuid sesId : sesIds) {
            ComputeTaskFuture<Object> fut = futs.get(sesId);

            if (fut != null)
                fut.cancel();
        }
    }

    return null;
}
 
示例10
/** {@inheritDoc} */
@Nullable @Override public Object call() throws Exception {
    IgniteCompute rmts = g.compute(g.cluster().forRemotes());

    while (!finish) {
        try {
            rmts.execute(GridJobExecutionLoadTestTask.class, null);

            txCnt.increment();
        }
        catch (IgniteException e) {
            e.printStackTrace();
        }
    }

    return null;
}
 
示例11
/**
 * Start compute jobs in the separate thread.
 *
 * @param compute Ignite compute instance.
 */
private void runJobAsync(final IgniteCompute compute) {
    new Thread(new Runnable() {
        @Override public void run() {
            try {
                compute.call(new IgniteCallable<Integer>() {
                    @Override public Integer call() throws Exception {
                        COMPUTE_JOB_STARTED.countDown();

                        // Simulate long-running job.
                        new CountDownLatch(1).await();

                        return null;
                    }
                });
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
 
示例12
/**
 * @throws Exception If failed.
 */
@Test
public void testReconnectComputeApi() throws Exception {
    final Ignite client = grid(serverCount());

    final IgniteCompute comp = client.compute();

    reconnectFailover(new Callable<Void>() {
        @Override public Void call() throws Exception {
            comp.call(new DummyClosure());

            comp.broadcast(new DummyClosure());

            return null;
        }
    });
}
 
示例13
/**
 * @throws Exception If failed.
 */
@Test
public void testApplyAsync() throws Exception {
    runTest(closureFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            final IgniteCompute comp = ignite.compute();

            Collection<IgniteFuture<Object>> futures = new ArrayList<>(MAX_JOB_COUNT);

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                // value(i - 1): use negative argument of the value method to generate nullong value.
                futures.add(comp.applyAsync((IgniteClosure<Object, Object>)factory.create(), value(i - 1)));
            }

            // Wait for results.
            Collection<Object> results = new ArrayList<>(MAX_JOB_COUNT);

            for (IgniteFuture<Object> future : futures)
                results.add(future.get());

            checkResultsClassCount(MAX_JOB_COUNT - 1, results, value(0).getClass());
            assertCollectionsEquals("Results value mismatch", createGoldenResults(), results);
        }
    });
}
 
示例14
/**
 * @throws Exception If failed.
 */
@Test
public void testCallAsync() throws Exception {
    runTest(callableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            final IgniteCompute comp = ignite.compute();

            Collection<IgniteFuture<Object>> futures = new ArrayList<>(MAX_JOB_COUNT);

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                EchoCallable job = (EchoCallable)factory.create();
                job.setArg(value(i - 1));

                futures.add(comp.callAsync(job));
            }

            // Wait for results.
            Collection<Object> results = new ArrayList<>(MAX_JOB_COUNT);
            for (IgniteFuture<Object> future : futures)
                results.add(future.get());

            checkResultsClassCount(MAX_JOB_COUNT - 1, results, value(0).getClass());
            assertCollectionsEquals("Results value mismatch", createGoldenResults(), results);
        }
    });
}
 
示例15
/**
 * @throws Exception If failed.
 */
@Test
public void testAffinityCall() throws Exception {
    runTest(callableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache(CACHE_NAME);

            final IgniteCompute comp = ignite.compute();

            Collection<Object> results = new ArrayList<>(MAX_JOB_COUNT);

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                EchoCallable job = (EchoCallable)factory.create();

                job.setArg(value(i - 1));

                results.add(comp.affinityCall("test", key(0), job));
            }

            checkResultsClassCount(MAX_JOB_COUNT - 1, results, value(0).getClass());
            assertCollectionsEquals("Results value mismatch", createGoldenResults(), results);
        }
    });
}
 
示例16
/**
 * @throws Exception If failed.
 */
@Test
public void testAffinityCallAsync() throws Exception {
    runTest(callableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache(CACHE_NAME);

            final IgniteCompute comp = ignite.compute();

            Collection<Object> results = new ArrayList<>(MAX_JOB_COUNT);

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                EchoCallable job = (EchoCallable)factory.create();

                job.setArg(value(i - 1));

                IgniteFuture<Object> fut = comp.affinityCallAsync("test", key(0), job);

                results.add(fut.get());
            }

            checkResultsClassCount(MAX_JOB_COUNT - 1, results, value(0).getClass());
            assertCollectionsEquals("Results value mismatch", createGoldenResults(), results);
        }
    });
}
 
示例17
/**
 * @throws Exception If failed.
 */
@Test
public void testMultiCacheAffinityCall() throws Exception {
    runTest(callableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache("test0");
            ignite.getOrCreateCache("test1");

            final IgniteCompute comp = ignite.compute();

            Collection<Object> results = new ArrayList<>(MAX_JOB_COUNT);

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                EchoCallable job = (EchoCallable)factory.create();

                job.setArg(value(i - 1));

                results.add(comp.affinityCall(Arrays.asList("test0", "test1"), key(0), job));
            }

            checkResultsClassCount(MAX_JOB_COUNT - 1, results, value(0).getClass());
            assertCollectionsEquals("Results value mismatch", createGoldenResults(), results);
        }
    });
}
 
示例18
/**
 * @throws Exception If failed.
 */
@Test
public void testMultiCacheByPartIdAffinityCall() throws Exception {
    runTest(callableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache("test0");
            ignite.getOrCreateCache("test1");

            final IgniteCompute comp = ignite.compute();

            Collection<Object> results = new ArrayList<>(MAX_JOB_COUNT);

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                EchoCallable job = (EchoCallable)factory.create();

                job.setArg(value(i - 1));

                results.add(comp.affinityCall(Arrays.asList("test0", "test1"), 0, job));
            }

            checkResultsClassCount(MAX_JOB_COUNT - 1, results, value(0).getClass());
            assertCollectionsEquals("Results value mismatch", createGoldenResults(), results);
        }
    });
}
 
示例19
/**
 * @throws Exception If failed.
 */
@Test
public void testAffinityRun() throws Exception {
    runTest(runnableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache(CACHE_NAME);

            final IgniteCompute comp = ignite.compute();

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                IgniteRunnable job = (IgniteRunnable)factory.create();

                comp.affinityRun("test", key(0), job);
            }
        }
    });
}
 
示例20
/**
 * @throws Exception If failed.
 */
@Test
public void testAffinityRunAsync() throws Exception {
    runTest(runnableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache(CACHE_NAME);

            final IgniteCompute comp = ignite.compute();

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                IgniteRunnable job = (IgniteRunnable)factory.create();

                IgniteFuture<Void> fut = comp.affinityRunAsync("test", key(0), job);

                fut.get();
            }
        }
    });
}
 
示例21
/**
 * @throws Exception If failed.
 */
@Test
public void testMultiCacheAffinityRun() throws Exception {
    runTest(runnableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache("test0");
            ignite.getOrCreateCache("test1");

            final IgniteCompute comp = ignite.compute();

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                IgniteRunnable job = (IgniteRunnable)factory.create();

                comp.affinityRun(Arrays.asList("test0", "test1"), key(0), job);
            }
        }
    });
}
 
示例22
/**
 * @throws Exception If failed.
 */
@Test
public void testMultiCacheAffinityRunAsync() throws Exception {
    runTest(runnableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache("test0");
            ignite.getOrCreateCache("test1");

            final IgniteCompute comp = ignite.compute();

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                IgniteRunnable job = (IgniteRunnable)factory.create();

                IgniteFuture<Void> fut = comp.affinityRunAsync(Arrays.asList("test0", "test1"), key(0), job);

                fut.get();
            }
        }
    });
}
 
示例23
/**
 * @throws Exception If failed.
 */
@Test
public void testMultiCacheByPartIdAffinityRun() throws Exception {
    runTest(runnableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache("test0");
            ignite.getOrCreateCache("test1");

            final IgniteCompute comp = ignite.compute();

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                IgniteRunnable job = (IgniteRunnable)factory.create();

                comp.affinityRun(Arrays.asList("test0", "test1"), 0, job);
            }
        }
    });
}
 
示例24
/**
 * @throws Exception If failed.
 */
@Test
public void testMultiCacheByPartIdAffinityRunAsync() throws Exception {
    runTest(runnableFactories, new ComputeTest() {
        @Override public void test(Factory factory, Ignite ignite) throws Exception {
            ignite.getOrCreateCache("test0");
            ignite.getOrCreateCache("test1");

            final IgniteCompute comp = ignite.compute();

            for (int i = 0; i < MAX_JOB_COUNT; ++i) {
                IgniteRunnable job = (IgniteRunnable)factory.create();

                IgniteFuture<Void> fut = comp.affinityRunAsync(Arrays.asList("test0", "test1"), 0, job);

                fut.get();
            }
        }
    });
}
 
示例25
/**
 * @throws Exception If failed.
 */
@Test
public void testCorrectException() throws Exception {
    Ignite ignite = ignite(0);

    IgniteCompute comp = ignite.compute(ignite.cluster().forRemotes()).withNoFailover();

    stopGrid(1);

    try {
        comp.call(new IgniteCallable<Object>() {
            @Override public Object call() throws Exception {
                fail("Should not be called.");

                return null;
            }
        });

        fail();
    }
    catch (ClusterTopologyException e) {
        log.info("Expected exception: " + e);
    }
}
 
示例26
/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println("Compute runnable example started.");

        IgniteCompute compute = ignite.compute();

        // Iterate through all words in the sentence and create runnable jobs.
        for (final String word : "Print words using runnable".split(" ")) {
            // Execute runnable on some node.
            compute.run(() -> {
                System.out.println();
                System.out.println(">>> Printing '" + word + "' on this node from ignite job.");
            });
        }

        System.out.println();
        System.out.println(">>> Finished printing words using runnable execution.");
        System.out.println(">>> Check all nodes for output (this node is also part of the cluster).");
    }
}
 
示例27
public static void main(String[] args) throws IOException {
    try (Ignite ignite = Ignition.start(CLIENT_CONFIG)) {
        IgniteCompute compute = ignite.compute();

        ValidateMessage[] validateMessages = TestDataGenerator.getValidateMessages();

        Boolean result = compute.execute(new ForkJointWithSessionComputation(), validateMessages);
        System.out.println("result=" + result);
    }
}
 
示例28
public static void main(String[] args) throws IOException {

        try (Ignite ignite = Ignition.start(CLIENT_CONFIG)) {
            IgniteCompute compute = ignite.compute();

            ValidateMessage[] validateMessages = TestDataGenerator.getValidateMessages();
            Boolean result = compute.execute(new ForkJoinComputationExt(), validateMessages);
            System.out.println("result=" + result);
        }
    }
 
示例29
public static void main(String[] args) {

        try (Ignite ignite = Ignition.start(CommonConstants.CLIENT_CONFIG)) {
            IgniteCompute compute = ignite.compute();

            IgniteCache<AccountCacheKey, AccountCacheData> cache = BankDataGenerator.createBankCache(ignite);
            IgniteCache<String, CashBackDictionaryData> savingsCache = BankDataGenerator.initSavigsCache(ignite);

            SqlFieldsQuery sql = new SqlFieldsQuery("select * from TransactionData where account = ?");

            BigDecimal result = compute.affinityCall(BankDataGenerator.ACCOUNT_CACHE, new AccountKey(BankDataGenerator.TEST_ACCOUNT), () -> {
                //download all transactions for this account
                List<List<?>> data = cache.query(sql.setArgs(BankDataGenerator.TEST_ACCOUNT)).getAll();
                BigDecimal cashBack = new BigDecimal(0);
                for (List row : data) {
                    TransactionKey key = (TransactionKey) row.get(0);
                    TransactionData tr = (TransactionData) row.get(1);

                    CashBackDictionaryData cashBackDictionaryData = savingsCache.get(tr.getTransactionType());
                    cashBack = cashBack.add(tr.getSum().multiply(cashBackDictionaryData.getCashBackPercent()));
                }
                //System.out.println("savings="+cashBack);
                return cashBack;
            });

            System.out.println("CashBack="+result);

        }
    }
 
示例30
public static void main(String[] args) throws IOException {
    String sample1 = TestDataGenerator.getSample1();
    byte[] vaidateSchema = TestDataGenerator.getValidateSchema();
    String validateScript = TestDataGenerator.getValidateScript();

    try (Ignite ignite = Ignition.start(CommonConstants.CLIENT_CONFIG)) {
        IgniteCompute compute = ignite.compute().withAsync();

        compute.call(() -> {
            boolean validateXsdResult = XsdValidator.validate(sample1, vaidateSchema);
            boolean validateByJs = JSEvaluate.evaluateJs(sample1, validateScript);

            System.out.println("validateXsdResult=" + validateXsdResult);
            System.out.println("validateByJs=" + validateByJs);

            return validateXsdResult && validateByJs;
        });

        compute.future().listen((result) -> {
            boolean res = (boolean) result.get();
            System.out.println("result=" + res);
        });


        //System.out.println("Presse ENTER to exit!");
        //System.in.read();
    }
}