“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):||3 November 2018|
|PDF File Size:||1.15 Mb|
|ePub File Size:||13.13 Mb|
|Price:||Free* [*Free Regsitration Required]|
Sometimes you just want to listen to a Future being completed, and react to that not by creating a new Futurebut by side-effecting. For example, transformations and operations on collections of futures are supported, along sith numerous asynchronous callbacks.
You can also use the recoverWith method, which has the same relationship to recover as flatMap has to mapand is use like this:. If a Future does contain an Exceptioncalling Await. You can also create already completed Futures using the Future companion Futures classwhich can be either successes:. The blocking operations are located in Await. This wigh the same result as this example: Instead, the flatMap method should be used: Future fallbackTo combines 2 Futures into a new Futureand will hold the successful value of the second Future if the first Future fails.
The return value of the map method is another Future that will contain the new result:. Target Audience Composable Futures with Akka 2.
To be able to use this from Java, Akka provides a java friendly interface in akka. The futures implementation provided by Akka 1. The example for comprehension above is an example of composing Future s.
Future is done using scala-java8-compat library. In order to execute callbacks and operations, Futures need something called an ExecutionContextwhich is very similar to wuth java.
You can also combine two Futures into a new Future that will hold a tuple of the two Futures successful results, using the zip operation. Further information for programming in each language is provided, usually with code snippets.
Async methods As mentioned above, default async methods are always executed on ForkJoinPool. See next section for examples. Found an error akka this documentation?
Some familiarity with git is assumed. First thenApply is actually performed on scala-java8-compat instance and computational stage lambda execution is delegated to default Java thenApplyAsync which is executed on ForkJoinPool. When using non-blocking it is better to use the mapTo method to safely try to cast a Future to wtih expected type: The futures implementation bundled in the free, open source Akka stack is written in Scala.
Composable Futures with Akka | Mike Slinn’s Weblog
Second and eith thenApply methods are executed on Java 8 CompletableFuture instance which executes computational stages on the thread which completed the first stage. About The Author Mike Slinn is a principal at Micronautics Research Corp- orationa contract software development firm that focuses on cutting-edge enterprise software technology. This book explains how to write concurrent programs that can run in multiple threads and on multiple cores.
Composwble 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. The real benefit comes when the Future s are created first, and then combining them together.
It is never executed on a thread of Scala Future because default thenApply breaks the chain and executes on ForkJoinPool. Actors are generally preferred for coordinated or stateful activities.
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. Then we wait for stages to complete complsable second and third thenApply are executed on completed CompletionStageand stages are executed on the current thread – the thread which called second and third thenApply.
Instead, the flatMap method should be used:.
Composable Futures with Akka 2.0
Mike Slinn is a principal at Micronautics Research Corp- orationa contract software development firm that focuses on cutting-edge enterprise software technology. You can also use the SBT console to try code snippets interactively. It is also possible to handle an Exception by returning a different result. If you know Java and are learning Scala, comparing the two programs will be instructive. Existing projects can use this technique to rapidly leverage the extra functionality of Akka futures, and thereby extend the functionality of legacy software.
All async methods without an explicit Executor are performed using the ForkJoinPool. It is very often desirable to be able to combine different Futures with each other, below are some examples on how that can be done in a non-blocking fashion. UserDataActor holds the data in memory, so that it can return the current state of the user data quickly upon a request.
CompletionStage instances returned by Akka it falls back to standard behavior of Java CompletableFuture.
Handling Exception s will be discussed further within this documentation. Failure containing the ArithmeticExceptionour Future would have a result of 0. If the nature of the Future calls invoked by the actor matches or is compatible with the activities of that actor e.
First thenApply is still executed on ForkJoinPool. In this example we are joining two strings together within a Future. Blocking is discouraged though as it will cause performance problems.
Note that the pipeTo pipe method used with the?
Futures • Akka Documentation
The map method is fine if we are modifying a single Futurebut if 2 or more Future s are involved map will not allow you to combine them together:. Futures Dependency This section explains using plain Scala Futures but focuses on their interop with Akka Actors, so to follow those examples you will want to depend on: When wkka original Futuref1completes, it will also apply our function and complete the second Future with its result.
Mike is a futurws architect who enjoys programming and mentoring. For this Future supports onCompleteonSuccess and onFailureof which the last two are specializations of the first.