Skip to content

Instantly share code, notes, and snippets.

@mslinn
Created February 19, 2012 18:35
Show Gist options
  • Save mslinn/1865034 to your computer and use it in GitHub Desktop.
Save mslinn/1865034 to your computer and use it in GitHub Desktop.
Future.foreach() invoked from Java
import akka.actor.ActorSystem;
import akka.dispatch.*;
import com.typesafe.config.ConfigFactory;
import java.util.concurrent.Callable;
/** This example uses Future.future() to create a future that computes 2+3. */
class ForeachJava {
private final String configString = "akka { logConfigOnStart=off }";
ActorSystem system = ActorSystem.apply("actorSystem", ConfigFactory.parseString(configString));
MessageDispatcher dispatcher = system.dispatcher();
@SuppressWarnings("unchecked")
private final Foreach<Integer> foreachFunction = new Foreach<Integer>() {
public void each(final Integer result) {
System.out.println("Java foreach result: " + result);
}
};
@SuppressWarnings("unchecked")
private final OnComplete<Integer> shutdownFunction = new OnComplete<Integer>() {
@Override public void onComplete(final Throwable exception, final Integer result) {
system.shutdown(); // releases threadpool resources so System.exit() can run
}
};
private final Callable<Integer> callable = new Callable<Integer>() {
@Override public Integer call() { return 2 + 3; }
};
public void doit() {
final Future<Integer> f = Futures.future(callable, dispatcher);
f.foreach(foreachFunction)/*.andThen(shutdownFunction)*/;
}
public static void main(final String[] args) {
new ForeachJava().doit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment