Asynchronous Support
This is a legacy Apache Ignite documentationThe new documentation is hosted here: https://ignite.apache.org/docs/latest/
Overview
A majority of Apache Ignite APIs can be used in both synchronous or asynchronous fashion. The asynchronous methods' names have an Async suffix.
// Synchronous get
V get(K key);
// Asynchronous get
IgniteFuture<V> getAsync(K key);The asynchronous operations return an instance of IgniteFuture or one of its subclasses. You can wait for the result of an asynchronous operation by either calling a blocking IgniteFuture.get() method or registering a closure using the IgniteFuture.listen() or IgniteFuture.chain() method and wait while the closure gets called upon the operation completion.
Supported Interfaces
The interfaces listed below can be used in synchronous or asynchronous modes:
IgniteComputeIgniteCacheTransactionIgniteServicesIgniteMessagingIgniteEvents
Listeners and Chaining Futures
To wait for the result of an asynchronous operation in a non-blocking fashion (IgniteFuture.get()), register a closure using the IgniteFuture.listen() or IgniteFuture.chain() method. Once the operation gets completed, the closure will be called. For example:
IgniteCompute compute = ignite.compute();
// Execute a closure asynchronously.
IgniteFuture<String> fut = compute.callAsync(() -> {
return "Hello World";
});
// Listen for completion and print out the result.
fut.listen(f -> System.out.println("Job result: " + f.get()));
Closures Execution and Thread PoolsIf an asynchronous operation has been completed by the time the closure is passed to either the
IgniteFuture.listen()orIgniteFuture.chain()method, then the closure will be executed synchronously by the calling thread. Otherwise, the closure will be executed asynchronously upon the operation completion.Depending on the type of operation, the closure might be called by a thread from the system pool (asynchronous cache operations) or by a thread from the public pool (asynchronous Ignite Compute operations). Therefore, you should avoid calling synchronous cache and compute operations from the closure implementation. Otherwise, it may lead to a deadlock due to pools starvation.
To achieve nested execution of asynchronous Ignite Compute operations, you can take advantage of custom thread pools.
Updated 10 months ago
