Skip to content

Instantly share code, notes, and snippets.

@huljas
Last active December 19, 2015 00:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save huljas/07eb777f6e6d3c6e7d1f to your computer and use it in GitHub Desktop.
Save huljas/07eb777f6e6d3c6e7d1f to your computer and use it in GitHub Desktop.
How to convert Java Future to Akka Future
/**
* @author Heikki Uljas
*/
public class JavaFutureToAkkaFuture {
public static void main(String[] args) throws Exception {
// Create java future
java.util.concurrent.ExecutorService executorService = java.util.concurrent.Executors.newFixedThreadPool(10);
final java.util.concurrent.Future<String> javaFuture = executorService.submit(new java.util.concurrent.Callable<String>() {
public String call() {
return "java";
}
});
// Convert java future to akka future
akka.dispatch.ExecutionContext executionContext = akka.dispatch.ExecutionContexts.fromExecutor(executorService);
akka.dispatch.Future<String> akkaFuture = akka.dispatch.Futures.future(new java.util.concurrent.Callable<String>() {
public String call() throws Exception {
return "scala->" + javaFuture.get();
}
}, executionContext);
System.out.println(akka.dispatch.Await.result(akkaFuture, akka.util.Duration.Inf()));
}
}
/**
* @author Heikki Uljas
*/
object JavaFutureToAkkaFuture {
def main(args:Array[String]) {
// Create java future
val executorService: java.util.concurrent.ExecutorService = java.util.concurrent.Executors.newFixedThreadPool(10)
val javaFuture: java.util.concurrent.Future[String] = executorService.submit(new java.util.concurrent.Callable[String] {
def call: String = {
return "java"
}
})
// Convert java future to akka future
val executionContext: akka.dispatch.ExecutionContext = akka.dispatch.ExecutionContexts.fromExecutor(executorService)
val akkaFuture: akka.dispatch.Future[String] = akka.dispatch.Futures.future(new java.util.concurrent.Callable[String] {
def call: String = {
return "scala->" + javaFuture.get
}
}, executionContext)
print(akka.dispatch.Await.result(akkaFuture, akka.util.Duration.Inf))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment