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);
}
}
}
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);
}
}
}
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);
}
}
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();
}
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);
}
}
}
}
Aggregations