COMPOSABLE FUTURES WITH AKKA PDF
“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):||19 September 2013|
|PDF File Size:||5.55 Mb|
|ePub File Size:||1.57 Mb|
|Price:||Free* [*Free Regsitration Required]|
In the Scala Standard Library, a Future is a data structure used to retrieve the result of some concurrent operation. It is also possible to handle an Exception by returning a different result.
Mike Slinn – Connoisseur of Technology: Composable Futures with Akka
Futuges pipeTo pipe with the? Akka futures has a Java API, and it provides Java programs with almost every bit of the same futures capability composaboe to Scala programs.
Actors are generally preferred for coordinated or stateful activities. CompletionStage also has async methods which take Executor as a second parameter, just like Future:. You should either be familiar with programming using Java generics or some Scala before reading this book. The pipe pattern can be used by importing akka.
Composable Futures with Akka
Dozens of complete working code examples are provided with the book, at least one per method in each language. This method takes a Function Mapper which performs some operation on the result of the Futureand returning a new result. If you need fututes do conditional propagation, you can use filter: The example for comprehension above is an example of composing Future s.
Each method of the Akka futures implementation is covered in detail; first introduced akla short explanation, then followed by the Java signature and the Scala signature.
The first method for working with Future functionally is map. It is also possible to create an empty Promiseto be filled later, and obtain the corresponding Future:. Futuges 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:.
Composable Futures with Akka 2.
The scala-java8-compat library returns its own implementation of CompletionStage which delegates all non-async methods to their async counterparts. Now compare that to this example: Handling Exception futurds will be discussed further within this documentation. A common use case for this is combining the replies of several Actor s into a single calculation without resorting to calling Await. Akka Futures can also be preset to a value or exception when created.
An overview of futures is provided through code examples. As mentioned above, default async methods are always executed on ForkJoinPool. This result can be accessed synchronously blocking or asynchronously non-blocking. Mike is a hands-on architect who enjoys programming and mentoring. Future counterpart in Java; conversion from scala. Future is done using scala-java8-compat library. When non-async methods are applied on a not yet completed CompletionStagethey are completed by the thread which completes initial CompletionStage:.
The implementation extends standard Java CompletableFuture. The completion is delayed: The source code for this page can be found here.
Since the result of a Future is created concurrently to the rest of the program, exceptions must be handled differently. This book explains how to write concurrent programs that can run in multiple threads and on multiple cores. Instead, the flatMap method should be used: Now compare that to this example:. There are generally two ways of getting a reply from an Actor AbstractActor: When we finally get the result, it will contain the number Something to keep in mind when doing this is even though it looks like parts of the above example can run in parallel, each step of the for comprehension is run sequentially.
If the sequence passed to fold is empty, it will return the start-value, in the case above, that will be 0. Sometimes you just want to listen to a Future being completed, and react to that not by creating a new Futurebut by side-effecting. The futures implementation provided by Akka 1. A common use case within Akka is to have some computation performed concurrently without needing the extra utility of an Actor AbstractActor.
The value of an Akka Future will normally become available sometime after creation, or it may incorporate an exception raised during computation. On the other hand, UserActivityActor queries into a repository to retrieve historical user activities then sends the result to the sender which is UserProxy in this case, with the pipe pattern.
Composable Futures with Akka 2.0
To wait for and retrieve the actual result the simplest method is:. You can also create already completed Futures using the Future companion Futures classwhich can be either successes:. See next section for examples. UserDataActor holds the data in memory, so that it can return the current state of the user data quickly upon a request.
This is the same result as this example: When our original Futuref1completes, it will also apply our function and complete the second Future with its result. Now we have a second Futuref2that will eventually contain an Int Integer.
Akka futures integrate easily with existing software projects.
Instead, the flatMap method should be used:.