Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrating usage of Future with Actors in Akka Java
package com.lightbend.akka.sample;
import akka.actor.AbstractActor;
import akka.actor.ActorSystem;
import akka.dispatch.Futures;
import akka.dispatch.OnSuccess;
import scala.concurrent.Future;
import static akka.dispatch.Futures.future; //this import might be missing
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
//don't import this
//import java.util.concurrent.Future;
public class ActorWithFuture extends AbstractActor {
final ActorSystem system = ActorSystem.create("helloakka");
ActorWithFuture(){
Futures.future(() -> "hello", getContext().dispatcher());
}
@Override
public Receive createReceive() {
Future<List<String>> f = future(new Callable<List<String>>() {
public List<String> call() {
return Arrays.asList("Hello", "World!");
}
}, system.dispatcher());
f.onSuccess(new PrintResult<List<String>>(), system.dispatcher());
return AbstractActor.emptyBehavior();
}
public final static class PrintResult<T> extends OnSuccess<T> {
@Override public final void onSuccess(T t) {
System.out.println(t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.