“Composable Futures with Akka ” features Java and Scala code examples. The book is available in PDF format. Akka is hot, and Akka. Writing concurrent programs that can run in multiple threads and on multiple cores is crucial but daunting. Composable futures provide a convenient abstraction. Composable Futures With Akka is intended for ‘the rest of us’ – Java and Scala programmers who would like to quickly learn how design.
|Published (Last):||8 February 2015|
|PDF File Size:||7.86 Mb|
|ePub File Size:||8.46 Mb|
|Price:||Free* [*Free Regsitration Required]|
Akka futures are being subsumed into Scala 2. Then we wait for stages to complete so second and third thenApply are executed on completed CompletionStageand stages are executed on the current thread – the thread which called second futhres third thenApply. Actions supplied for dependent completions of non-async methods may be performed by the thread that completes the current CompletableFutureor by any other caller of a completion method.
Since callbacks are executed in any order and potentially in parallel, it can be tricky at the times when you need sequential ordering of operations. Async methods As mentioned above, default async methods are always executed on ForkJoinPool. He has been recognized as a software expert in US federal court.
Even if the Future has already been completed, when one of these methods is called. Other implementations of futures are limited. Future counterpart in Java; conversion from scala. When we finally get the result, it will contain the number There are generally two ways of getting a reply from an Actor AbstractActor: UserDataActor holds the data in memory, so that it can return the current state of the user data quickly upon a request.
The completion is delayed: The source code for this page can be found here.
Composable Futures with Akka 2.0
CompletionStage instances returned by Akka it falls back to standard behavior of Java CompletableFuture. Here we wait for the results from the first 2 Actor s before sending that result to the third Actor. Akka futures integrate easily with existing software projects. Future which has async methods only, CompletionStage has async and non-async methods.
This example is behaving like Future: Please feel free to edit and contribute a pull request. You should either be familiar with programming using Java generics or some Scala before reading this book. Instead of waiting for this f1 to complete, we apply our function that calculates the length of the string using the map method. Mike Slinn is a principal at Micronautics Research Corp- orationa contract software development firm that focuses on cutting-edge enterprise software technology.
Second and third thenApply methods are executed on Java 8 CompletableFuture instance which executes computational stages on the thread which completed the first stage. Future is done using scala-java8-compat library.
Composable Futures with Akka
The scala-java8-compat library returns its own implementation of CompletionStage which delegates all non-async methods to their async counterparts. The code is provided as a git repository, which you can update any time.
It is very often desirable to be able to combine different Futures with each other, below are composab,e examples on how that can be done in a non-blocking fashion.
Since it needs to talk to the separate repositoryit takes time to retrieve the list of UserActivityhence the return type of queryHistoricalActivities is Future CompletableFuture. Java 8 CompletableFuture creates a new instance of CompletableFuture wkth any new stage, which means scala-java8-compat implementation is not used after the first mapping method.
In this example, when you need information about a user, you send a request message to UserProxyActorthen it gets the corresponding result from the appropriate backend actor based on the request message type. The map method is fine if we are modifying a single Future composalbe, but if 2 or more Future s are involved map will not allow you to combine them together: This is fine when dealing with a known amount of Actors, but can akks unwieldy if we have more than a handful.
You can also use the recoverWith method, which has the same relationship to recover as flatMap has to mapand is use like this:. Special considerations for programming with futures are discussed. Composable futures mean that operations on a future or a collection of futures can be chained together ftuures blocking; for example, transformations such as map can be applied to a composable future.
The sequence and traverse helper methods can make it easier to handle more complex use cases. He is often engaged to turn around projects and can provide interim technical leadership for companies in transition. In this example we futues joining two strings together within a Future. When to use futures and when to use actors? Mike is a hands-on architect who enjoys programming and mentoring. See next section for examples.