-
-
Save huljas/07eb777f6e6d3c6e7d1f to your computer and use it in GitHub Desktop.
How to convert Java Future to Akka Future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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