Java docs for Futures Needs Love
The examples on http://akka.io/docs/akka/2.0-M2/java/futures.html are missing a few infobits on how to use Futures without Actors.
- How to define the system variable.
- How to obtain the default dispatcher instance without creating an Actor.
- How to call onComplete() - simple but complete example needed. The docs say that onComplete() is supported for Java, but the example uses Await instead and I do not see onComplete() in the Futures Scaladoc.
- How to call Futures.filter() . BTW, FutureTestDocBase does not have a test for Futures.filter()
- How to define the system variable.
- How to obtain the default dispatcher instance without creating an Actor.
- How to call onComplete() - simple but complete example needed. The docs say that onComplete() is supported for Java, but the example uses Await instead and I do not see onComplete() in the Futures Scaladoc.
- How to call Futures.filter() . BTW, FutureTestDocBase does not have a test for Futures.filter()
Leave a comment
on 2012-01-13 16:19 *
By Mike Slinn
Here is some sample code that fills in some of the missing information:
I'm trying to figure out how to rewrite the above using onComplete(). I found ticket #1464, but it did not show the necessary imports and there were other errors in the code. Also https://github.com/jboner/akka/blob/releasing-2.0-M2/akka-docs/java/code/akka/docs/future/FutureDocTestBase.java does not have an example of onComplete().
Here is what I have so far, but there is one error:
The error is:
import static akka.dispatch.Futures.future;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.Callable;
import akka.actor.ActorSystem;
import akka.dispatch.Await;
import akka.dispatch.Future;
import akka.util.Duration;
/** Non-blocking function call, executed on another thread. */
class Apply {
public static void main(String[] args) {
ActorSystem system = ActorSystem.create();
Future<Integer> resultFuture = future(new Callable<Integer>() {
public Integer call() {
return 2 + 3;
}
}, system.dispatcher());
// Await.result() blocks until the Future completes
Integer result = (Integer) Await.result(resultFuture, Duration.create(1, SECONDS));
System.out.println("Result: " + result);
}
}
I'm trying to figure out how to rewrite the above using onComplete(). I found ticket #1464, but it did not show the necessary imports and there were other errors in the code. Also https://github.com/jboner/akka/blob/releasing-2.0-M2/akka-docs/java/code/akka/docs/future/FutureDocTestBase.java does not have an example of onComplete().
Here is what I have so far, but there is one error:
import static akka.dispatch.Futures.future;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.Callable;
import scala.Either;
import scala.Option;
import akka.actor.ActorSystem;
import akka.dispatch.Await;
import akka.dispatch.Future;
import akka.japi.Procedure;
import akka.util.Duration;
private static void nonBlockingExample() {
ActorSystem system = ActorSystem.create();
Future<Integer> resultFuture = future(new Callable<Integer>() {
public Integer call() {
return 2 + 3;
}
}, system.dispatcher());
resultFuture.onComplete(new Procedure<Future<Integer>>() {
public void apply(Future<Integer> future) {
Option<Either<Throwable,Integer>> result = future.value();
System.out.println("Result: " + result);
}
});
}
The error is:
The method onComplete(Function1<Either<Throwable,Integer>,BoxedUnit>) in the type Future<Integer> is not applicable
for the arguments (new Procedure<Future<Integer>>(){})
on 2012-01-13 16:50 *
By Mike Slinn
Description changed from The examples on http://akka... to The examples on http://akka...
on 2012-01-13 17:51 *
By Mike Slinn
Description changed from The examples on http://akka... to The examples on http://akka...
on 2012-01-13 17:52 *
By Mike Slinn
Description changed from The examples on http://akka... to The examples on http://akka...
on 2012-01-13 19:58 *
By Mike Slinn
Futures.map() is not defined. Is this deliberate or an oversight? The test cases use the Scala Future.map() method instead of the Java Futures class. When should the Java Futures methods be used, and when should Java code use the Scala Future methods?
Futures is for operations that are not logically associated with a single future.
As for onComplete:
/**
private[japi] final def onComplete[A >: T](proc: Procedure2[Throwable, A]): this.type = self.onComplete(_.fold(t ⇒ proc(t, null.asInstanceOf[T]), r ⇒ proc(null, r)))
Btw, this ticket is a duplicate of 1537, add your comments to that ticket.
As for onComplete:
/**
- Asynchronously called when this future is completed with either a failed or a successful result
- In case of a success, the first parameter (Throwable) will be null
- In case of a failure, the second parameter (T) will be null
- For no reason will both be null or neither be null
private[japi] final def onComplete[A >: T](proc: Procedure2[Throwable, A]): this.type = self.onComplete(_.fold(t ⇒ proc(t, null.asInstanceOf[T]), r ⇒ proc(null, r)))
Btw, this ticket is a duplicate of 1537, add your comments to that ticket.
on 2012-01-16 11:51 *
By Mike Slinn
Suggestion on how to correctly write the onComplete() in Java?