“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.

Author: Tauktilar Kazrazragore
Country: Greece
Language: English (Spanish)
Genre: Literature
Published (Last): 20 March 2009
Pages: 139
PDF File Size: 9.61 Mb
ePub File Size: 8.43 Mb
ISBN: 326-2-93239-467-7
Downloads: 80363
Price: Free* [*Free Regsitration Required]
Uploader: Moogukazahn

This is done with the recover method. Future is done using scala-java8-compat library. Target Audience Composable Futures with Akka 2.

Other implementations of futures are limited. Composable Futures with Akka 2. The Akka and Twitter implementations are much more advanced.

The Promises chapter in the book includes a case study that shows how easy it is to integrate Akka futures into existing code. 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.

All async methods without an explicit Executor are performed using the ForkJoinPool. Found an error in this documentation? 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. Mike has consulted on many legal cases and performed project failure analysis, and has researched and testified in cases involving intellectual property issues such as patent infringement and trade secret misappropriation.

What are composable futures? In order to execute callbacks and operations, Futures need something called an ExecutionContextwhich is very similar to a java. Now compare that to this example: Java 8 CompletableFuture creates a new instance of CompletableFuture for any new stage, which means scala-java8-compat implementation is not used after the first mapping method. Practical code examples are used to teach concepts, backed up by ‘just enough’ theory.


Composable Futures with Akka

Akka futures is integrated with Akka actors for distributed computing. When using non-blocking it is better to use the mapTo method to safely try to cast a Future to an expected type:. 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.

For these examples PrintResult is defined as follows: The completion is delayed: Clmposable that the pipeTo pipe method used with the? Something to note when using these methods: Both blocking and non-blocking operations can be performed on Akka futures. 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 using non-blocking it is better to use the mapTo method to safely try to cast a Future to an expected type: See also Java 8 Compatibility for Java compatibility.

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.

Async methods As mentioned above, default async methods are always executed on ForkJoinPool. Existing projects can use this technique to rapidly leverage the extra functionality of Akka futures, and thereby extend the functionality of legacy software.

Future counterpart in Java; conversion from scala. Composable futures mean that operations on a future cutures a collection of futures can be chained together without blocking; for example, transformations such as map can be applied to a composable future.

CompletionStage also has async methods which take Executor as a second parameter, just like Future:. Akka Futures can also be preset to a value or exception when created. Non-async methods When non-async methods are applied on a not yet completed CompletionStagethey are completed by the thread which completes initial CompletionStage: A common use case for this is combining the replies of several Actor s into a single calculation without resorting to calling Await.


Here we wait for the results from the first 2 Actor s before sending that result to the third Actor. When in doubt, async methods with explicit executor should be used. Instead of waiting gutures this f1 to complete, we apply our function that calculates the length of the string using the map method.

Composable Futures with Akka 2.0

It is also possible to handle an Exception by returning a different result. To be able to use this from Java, Akka provides a java friendly interface in akka. 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: 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.

Each actor is configured to be run on a MessageDispatcherand that dispatcher doubles as an ExecutionContext. The return value of the map method is another Future that will contain the new result:. To wait for and retrieve the actual result the simplest method is:.

The scala-java8-compat library returns its own implementation of CompletionStage which delegates all non-async methods to their async counterparts. You can also create already completed Futures using the Future companion Futures classwhich can be either successes:. 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:.

If the sequence passed to fold is empty, it will return the start-value, in the case above, that will be 0.