Search in sources :

Example 1 with IgniteFuture

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

the class CacheAsyncApiExample method main.

/**
     * 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(">>> Cache asynchronous API example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            Collection<IgniteFuture<?>> futs = new ArrayList<>();
            // Execute several puts asynchronously.
            for (int i = 0; i < 10; i++) futs.add(cache.putAsync(i, String.valueOf(i)));
            // Wait for completion of all futures.
            for (IgniteFuture<?> fut : futs) fut.get();
            // Execute get operation asynchronously and wait for result.
            cache.getAsync(1).listen(new IgniteInClosure<IgniteFuture<String>>() {

                @Override
                public void apply(IgniteFuture<String> fut) {
                    System.out.println("Get operation completed [value=" + fut.get() + ']');
                }
            });
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite)

Example 2 with IgniteFuture

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

the class CacheAsyncApiExample method main.

/**
     * 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(">>> Cache asynchronous API example started.");
        CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>();
        cfg.setCacheMode(CacheMode.PARTITIONED);
        cfg.setName(CACHE_NAME);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cfg)) {
            // Enable asynchronous mode.
            IgniteCache<Integer, String> asyncCache = cache.withAsync();
            Collection<IgniteFuture<?>> futs = new ArrayList<>();
            // Execute several puts asynchronously.
            for (int i = 0; i < 10; i++) {
                asyncCache.put(i, String.valueOf(i));
                futs.add(asyncCache.future());
            }
            // Wait for completion of all futures.
            futs.forEach(IgniteFuture::get);
            // Execute get operation asynchronously.
            asyncCache.get(1);
            // Asynchronously wait for result.
            asyncCache.<String>future().listen(fut -> System.out.println("Get operation completed [value=" + fut.get() + ']'));
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 3 with IgniteFuture

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

the class PlatformTargetProxyImpl method inStreamAsync.

/** {@inheritDoc} */
@Override
public void inStreamAsync(int type, long memPtr) throws Exception {
    try (PlatformMemory mem = platformCtx.memory().get(memPtr)) {
        BinaryRawReaderEx reader = platformCtx.reader(mem);
        long futId = reader.readLong();
        int futTyp = reader.readInt();
        final PlatformAsyncResult res = target.processInStreamAsync(type, reader);
        if (res == null)
            throw new IgniteException("PlatformTarget.processInStreamAsync should not return null.");
        IgniteFuture fut = res.future();
        if (fut == null)
            throw new IgniteException("PlatformAsyncResult.future() should not return null.");
        PlatformFutureUtils.listen(platformCtx, fut, futId, futTyp, new PlatformFutureUtils.Writer() {

            /** {@inheritDoc} */
            @Override
            public void write(BinaryRawWriterEx writer, Object obj, Throwable err) {
                res.write(writer, obj);
            }

            /** {@inheritDoc} */
            @Override
            public boolean canWrite(Object obj, Throwable err) {
                return err == null;
            }
        }, target);
    } catch (Exception e) {
        throw target.convertException(e);
    }
}
Also used : PlatformFutureUtils(org.apache.ignite.internal.processors.platform.utils.PlatformFutureUtils) IgniteFuture(org.apache.ignite.lang.IgniteFuture) BinaryRawReaderEx(org.apache.ignite.internal.binary.BinaryRawReaderEx) IgniteException(org.apache.ignite.IgniteException) IgniteException(org.apache.ignite.IgniteException) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory)

Example 4 with IgniteFuture

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

the class GridServiceProcessorStopSelfTest method testStopDuringDeployment.

/**
     * @throws Exception If failed.
     */
public void testStopDuringDeployment() throws Exception {
    final CountDownLatch depLatch = new CountDownLatch(1);
    final CountDownLatch finishLatch = new CountDownLatch(1);
    final Ignite ignite = startGrid(0);
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            IgniteServices svcs = ignite.services();
            IgniteFuture f = svcs.deployClusterSingletonAsync("myClusterSingletonService", new TestServiceImpl());
            depLatch.countDown();
            try {
                f.get();
            } catch (IgniteException ignored) {
                finishLatch.countDown();
            } finally {
                finishLatch.countDown();
            }
            return null;
        }
    }, "deploy-thread");
    depLatch.await();
    Ignition.stopAll(true);
    boolean wait = finishLatch.await(15, TimeUnit.SECONDS);
    if (!wait)
        U.dumpThreads(log);
    assertTrue("Deploy future isn't completed", wait);
    fut.get();
}
Also used : IgniteServices(org.apache.ignite.IgniteServices) IgniteException(org.apache.ignite.IgniteException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException)

Example 5 with IgniteFuture

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

the class LargeEntryUpdateTest method testEntryUpdate.

/**
     * @throws Exception If failed.
     */
public void testEntryUpdate() throws Exception {
    try (Ignite ignite = startGrid()) {
        for (int i = 0; i < CACHE_COUNT; ++i) {
            IgniteCache<Long, byte[]> cache = ignite.cache(CACHE_PREFIX + i);
            cache.put(0L, new byte[PAGE_SIZE * 2]);
        }
        IgniteCompute compute = ignite.compute().withAsync();
        long endTime = System.currentTimeMillis() + WAIT_TIMEOUT;
        int iter = 0;
        while (System.currentTimeMillis() < endTime) {
            log.info("Iteration: " + iter++);
            cacheUpdate.set(true);
            try {
                List<IgniteFuture> futs = new ArrayList<>();
                for (int i = 0; i < THREAD_COUNT; ++i) {
                    compute.run(new CacheUpdater());
                    futs.add(compute.future());
                }
                Thread.sleep(30_000);
                cacheUpdate.set(false);
                for (IgniteFuture fut : futs) fut.get();
            } finally {
                cacheUpdate.set(false);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute)

Aggregations

IgniteFuture (org.apache.ignite.lang.IgniteFuture)65 Ignite (org.apache.ignite.Ignite)31 ArrayList (java.util.ArrayList)23 IgniteException (org.apache.ignite.IgniteException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CacheException (javax.cache.CacheException)10 IgniteCompute (org.apache.ignite.IgniteCompute)8 CI1 (org.apache.ignite.internal.util.typedef.CI1)7 UUID (java.util.UUID)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 List (java.util.List)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 Collection (java.util.Collection)4 LinkedList (java.util.LinkedList)4 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)4 HashMap (java.util.HashMap)3