Skip to content

Instantly share code, notes, and snippets.

@beritou
Created August 29, 2017 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beritou/f903fcb673479858cb69d5f2f902ca8e to your computer and use it in GitHub Desktop.
Save beritou/f903fcb673479858cb69d5f2f902ca8e to your computer and use it in GitHub Desktop.
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