| 1 | ## Asynchronous programming |
| 2 | |
| 3 | The driver exposes an asynchronous API that allows you to write programs |
| 4 | in a fully-non blocking manner. Asynchronous methods return instances of |
| 5 | Guava's [ListenableFuture], that can be conveniently chained and |
| 6 | composed. |
| 7 | |
| 8 | Here is a short example that opens a session and runs a query |
| 9 | asynchronously: |
| 10 | |
| 11 | ```java |
| 12 | import com.google.common.util.concurrent.*; |
| 13 | |
| 14 | ListenableFuture<Session> session = cluster.connectAsync(); |
| 15 | |
| 16 | // Use transform with an AsyncFunction to chain an async operation after another: |
| 17 | ListenableFuture<ResultSet> resultSet = Futures.transform(session, |
| 18 | new AsyncFunction<Session, ResultSet>() { |
| 19 | public ListenableFuture<ResultSet> apply(Session session) throws Exception { |
| 20 | return session.executeAsync("select release_version from system.local"); |
| 21 | } |
| 22 | }); |
| 23 | |
| 24 | // Use transform with a simple Function to apply a synchronous computation on the result: |
| 25 | ListenableFuture<String> version = Futures.transform(resultSet, |
| 26 | new Function<ResultSet, String>() { |
| 27 | public String apply(ResultSet rs) { |
| 28 | return rs.one().getString("release_version"); |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | // Use a callback to perform an action once the future is complete: |
| 33 | Futures.addCallback(version, new FutureCallback<String>() { |
| 34 | public void onSuccess(String version) { |
| 35 | System.out.printf("Cassandra version: %s%n", version); |
| 36 | } |
| 37 | |
| 38 | public void onFailure(Throwable t) { |
| 39 | System.out.printf("Failed to retrieve the version: %s%n", |
| 40 | t.getMessage()); |
| 41 | } |
| 42 | }); |
| 43 | ``` |
| 44 | |
| 45 | ### Good practices |
| 46 | |
| 47 | If your callback is slow, consider providing a separate executor. |
| 48 | Otherwise the callback might run on one of the driver's I/O threads, |
| 49 | blocking I/O operations for other requests while it is running: |
| 50 | |
| 51 | ```java |
| 52 | ListenableFuture<String> result = Futures.transform(resultSet, |
| 53 | new Function<ResultSet, String>() { |
| 54 | public String apply(ResultSet rs) { |
| 55 | return someVeryLongComputation(rs); |
| 56 | } |
| 57 | }, myCustomExecutor); |
| 58 | ``` |
| 59 | |
| 60 | Avoid blocking operations in callbacks, especially if you don't provide |
| 61 | a separate executor. This could easily lead to deadlock if the thread |
| 62 | that's supposed to complete the blocking call is also the thread that's |
| 63 | waiting on it: |
| 64 | |
| 65 | ```java |
| 66 | ListenableFuture<ResultSet> resultSet = Futures.transform(session, |
| 67 | new Function<Session, ResultSet>() { |
| 68 | public ResultSet apply(Session session) { |
| 69 | // Synchronous operation in a callback. |
| 70 | // DON'T DO THIS! It might deadlock. |
| 71 | return session.execute("select release_version from system.local"); |
| 72 | } |
| 73 | }); |
| 74 | ``` |
| 75 | |
| 76 | ### Known limitations |
| 77 | |
| 78 | There are still a few places where the driver will block internally |
| 79 | (mainly for historical reasons): |
| 80 | |
| 81 | * [Cluster#init][init] performs blocking I/O operations. To avoid |
| 82 | issues, you should create your `Cluster` instances while bootstrapping |
| 83 | your application, and call `init` immediately. If you need to create new |
| 84 | instances at runtime, make sure this does not happen on an I/O thread. |
| 85 | * if a connection pool is busy, the driver will block until a connection |
| 86 | becomes available. To avoid this, set |
| 87 | [PoolingOptions.poolTimeoutMillis][setPoolTimeoutMillis] to 0; the |
| 88 | driver will not block, just move to the next host immediately. |
| 89 | * if the session is set to a specific keyspace (either at startup or by |
| 90 | issuing a `USE` statement on a running session), the keyspace needs to |
| 91 | be propagated to any newly created connection. This is done when the |
| 92 | connection is first borrowed from the connection pool, and currently |
| 93 | blocks. To avoid any issue, only use a session with no keyspace set |
| 94 | (i.e. created by `Cluster#connect()`). This will be addressed in |
| 95 | [JAVA-893](https://datastax-oss.atlassian.net/browse/JAVA-893). |
| 96 | * trying to read fields from a [query trace] will block if the trace |
| 97 | hasn't been fetched already. |
| 98 | |
| 99 | [ListenableFuture]: https://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained |
| 100 | [init]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Cluster.html#init() |
| 101 | [setPoolTimeoutMillis]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/PoolingOptions.html#setPoolTimeoutMillis(int) |
| 102 | [query trace]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/QueryTrace.html |