Check whether a ListenableFuture completed successfully?

1,426 views
Skip to first unread message

Michael Bolin

unread,
May 23, 2013, 5:10:27 PM5/23/13
to guava-...@googlegroups.com
I would find it helpful if ListenableFuture had observer methods that could tell whether it completed successfully. I find myself writing something like:

public static void isSuccess(ListenableFuture future) {
  if (future.isDone()) {
    try {
      future.get();
      return true;
    } catch (CancellationException|ExecutionException|InterruptedException e) {
      return false;
    }
  } else {
    return false;
  }
}


This seems tedious when this is easily something that ListenableFuture could keep track of. Is there a better way?

I want to do something like this when I have reached a point in my code where I expect that the future has been resolved:

  Preconditions.checkState(future.isDone());
  if (isSuccess(future)) {
    T value = future.get();
    // Use value.
  } else {
    System.err.println("Well that's too bad...");
  }

It would be handy to be able to do things this way where I don't need so many try/catch blocks.

Chris Povirk

unread,
May 23, 2013, 5:42:08 PM5/23/13
to Michael Bolin, guava-discuss
For comparison, is this try-catch version the equivalent to your
isSuccess version?

Preconditions.checkState(future.isDone());
try {
T value = future.get();
// Use value.
} catch (CancellationException|ExecutionException|InterruptedException e) {
System.err.println("Well that's too bad...");
}

That is indeed a pretty ugly catch() block. One way to shrink it is to
use Futures.get:

Preconditions.checkState(future.isDone());
try {
T value = Futures.get(future, MyException.class);
// Use value.
} catch (MyException e) {
System.err.println("Well that's too bad...");
}

Another feature we've considered adding is Futures.getNow, which would
mean you'd no longer have to deal with InterruptedException. Perhaps
it would also be OK for CancellationException to be propagated? That
would leave you with a single kind of exception to catch:

Preconditions.checkState(future.isDone());
try {
T value = Futures.getNow(future.get);
// Use value.
} catch (ExecutionException e) {
System.err.println("Well that's too bad...");
}

There's only so much we can do with that pesky ExecutionException around :\

Chris Povirk

unread,
May 23, 2013, 5:54:59 PM5/23/13
to Michael Bolin, guava-discuss
I've been informed that I failed to paste in the link to the getNow
feature request. Sorry about that.

https://code.google.com/p/guava-libraries/issues/detail?id=1426

Martin Buchholz

unread,
May 23, 2013, 8:29:32 PM5/23/13
to Chris Povirk, Michael Bolin, guava-discuss
Comparison with jsr166:


"""
The execution status of tasks may be queried at several levels of detail: isDone() is true if a task completed in any way (including the case where a task was cancelled without executing); isCompletedNormally() is true if a task completed without cancellation or encountering an exception; isCancelled() is true if the task was cancelled (in which case getException() returns a CancellationException); and isCompletedAbnormally() is true if a task was either cancelled or encountered an exception, in which case getException() will return either the encountered exception or CancellationException.
"""




--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss

This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")

---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Reply all
Reply to author
Forward
0 new messages