spotify/futures-extra

Name: futures-extra

Owner: Spotify

Description: Java library for working with Guava futures

Created: 2015-02-21 08:20:50.0

Updated: 2018-05-23 14:26:42.0

Pushed: 2018-03-09 10:58:25.0

Homepage: null

Size: 150

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Futures-extra

Futures-extra is a set of small utility functions to simplify working with Guava's ListenableFuture class

Build status

Travis Coverage Status

Maven central

Maven Central

Build dependencies
Runtime dependencies
Usage

Futures-extra is meant to be used as a library embedded in other software. To import it with maven, use this:

<dependency>
  <groupId>com.spotify</groupId>
  <artifactId>futures-extra</artifactId>
  <version>3.1.1</version>
</dependency>
Examples
Cleaner transforms for Java 8 with Guava < 20

Java 8 introduced lambdas which can greatly reduce verbosity in code, which is great when using futures and transforms. One drawback with lambdas though is that when a lambda is supplied as an argument to a method with overloaded parameters, the compiler may fail to figure out which variant of a method call that is intended to be used.

Ideally, applying java 8 lambdas to Guava's Futures.transform() would look something like this:

ic static <A, B> ListenableFuture<B> example(ListenableFuture<A> future) {
turn Futures.transform(future, a -> toB(a));

Unfortunately this doesn't actually work because Futures.transform has two variants: one that takes a Function as its second parameter and one that takes an AsyncFunction. The compiler can't determine which variant to use without additional type information.

You could work around that by casting it like this:

ic static <A, B> ListenableFuture<B> example(ListenableFuture<A> future) {
turn Futures.transform(future, (Function<A, B>) a -> toB(a));

With futures-extra you can do this instead:

ic static <A, B> ListenableFuture<B> example(ListenableFuture<A> future) {
turn FuturesExtra.syncTransform(future, a -> toB(a));

This is just a simple delegating method that explicitly calls Futures.transform(future, Function). There is also a corresponding FuturesExtra.asyncTransform that calls Futures.transform(future, AsyncFunction).

When using Guava 20 or higher there is no method overloading and corresponding methods in Futures class can be used directly.

Joining multiple futures

A common use case is waiting for two or more futures and then transforming the result to something else. You can do this in a couple of different ways, here are two of them:

The examples are for Java 8, but they also work for Java 6 and 7 (though it becomes more verbose).

l ListenableFuture<A> futureA = getFutureA();
l ListenableFuture<B> futureB = getFutureB();

enableFuture<C> ret = Futures.transform(Futures.allAsList(futureA, futureB),
(Function<List<?>, C>)list -> combine((A) list.get(0), (B) list.get(1));

where combine is a method with parameters of type A and B returning C.

This one has the problem that you have to manually make sure that the casts and ordering are correct, otherwise you will get ClassCastException.

You could also access the futures directly to avoid casts:

l ListenableFuture<A> futureA = getFutureA();
l ListenableFuture<B> futureB = getFutureB();

enableFuture<C> ret = Futures.transform(Futures.allAsList(futureA, futureB),
(Function<List<?>, C>)list -> combine(Futures.getUnchecked(futureA), Futures.getUnchecked(futureB));

Now you instead need to make sure that the futures in the transform input are the same as the ones you getUnchecked. If you fail to do this, things may work anyway (which is a good way of hiding bugs), but block the thread, actually removing the asynchronous advantage. Even worse - the future may never finish, blocking the thread forever.

To simplify these use cases we have a couple of helper functions:

l ListenableFuture<A> futureA = getFutureA();
l ListenableFuture<B> futureB = getFutureB();

enableFuture<C> ret = FuturesExtra.syncTransform2(futureA, futureB,
(a, b) -> combine(a, b));

This is much clearer! We don't need any type information because the lambda can infer it, and we avoid the potential bugs that can occur as a result of the first to examples.

The tuple transform can be used up to 6 arguments named syncTransform2() through syncTransform6(). If you need more than that you could probably benefit from some refactoring, but you can also use FuturesExtra.join():

l ListenableFuture<A> futureA = getFutureA();
l ListenableFuture<B> futureB = getFutureB();

l ListenableFuture<JoinedResults> futureJoined = FuturesExtra.join(futureA, futureB);
rn Futures.transform(futureJoined,
joined -> combine(joined.get(futureA), joined.get(futureB)));

This supports an arbitrary number of futures, but is slightly more complex. However, it is much safer than the first two examples, because joined.get(…) will fail if you try to get the value of a future that was not part of the input.

Timeouts

Sometimes you want to stop waiting for a future after a specific timeout and to do this you generally need to have some sort of scheduling involved. To simplify that, you can use this:

l ListenableFuture<A> future = getFuture();
l ListenableFuture<A> futureWithTimeout = FuturesExtra.makeTimeoutFuture(scheduledExecutor, future, 100, TimeUnit.MILLISECONDS);
Select

If you have some futures and want to succeed as soon as the first one succeeds, you can use select:

l List<ListenableFuture<A>> futures = getFutures();
l ListenableFuture<A> firstSuccessful = FuturesExtra.select(futures);
Success/Failure callbacks

You can attach callbacks that are run depending on the results of a future:

l ListenableFuture<A> future = getFuture();
resExtra.addCallback(future, System.out::println, Throwable::printStackTrace);

Alternatively, if you are only interested in either successful or failed results of a future, you can use:

l ListenableFuture<A> future = getFuture();
resExtra.addSuccessCallback(future, System.out::println);
ava
l ListenableFuture<B> future = getFuture();
resExtra.addFailureCallback(future, System.out::println);
Concurrency limiting

If you want to fire of a large number of asynchronous requests or jobs, it can be useful to limit how many will run concurrently. To help with this, there is a new class called ConcurrencyLimiter. You use it like this:

maxConcurrency = 10;
maxQueueSize = 100;
urrencyLimiter<T> limiter = ConcurrencyLimiter.create(maxConcurrency, maxQueueSize);
(int i = 0; i < 1000; i++) {
stenableFuture<T> future = limiter.add(() -> createFuture());

The concurrency limiter will ensure that at most 10 futures are created and incomplete at the same time. All the jobs that are passed into ConcurrencyLimiter.add() will wait in a queue until the concurrency is below the limit.

The jobs you pass in should not be blocking or be overly CPU intensive. If that is something you need you should let your ConcurrencyLimiter jobs push the work on a thread pool.

The internal queue is bounded and if its limit is reached it, the call to add will return a failed future of ConcurrencyLimiter.CapacityReachedException.

Completed futures

In some cases you want to extract the value (or exception) from the future and you know that the future is completed so it won't be a blocking operation.

You could use these methods for that, but they will also block if the future is not complete which may lead to hard to find bugs.

lue = future.get();
lue = Futures.getUnchecked(future);

Instead you can use these methods which will never block but instead immediately throw an exception if the future is not completed. This is typically useful in unit tests (where futures should be immediate) and in general future callbacks/transforms where you know that a specific future must be completed for this codepath to be triggered.

lue = FuturesExtra.getCompleted(future);
wable exc = FuturesExtra.getException(future);
JDK 8 CompletableFuture <-> ListenableFuture Conversion
enableFuture<V> listenable = getFuture();
letableFuture<V> completable = CompletableFuturesExtra.toCompletableFuture(listenable);
letableFuture<V> completable = getFuture();
enableFuture<V> listenable = CompletableFuturesExtra.toListenableFuture(completable);

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.