Mike Slinn
Mike Slinn

Composable Futures With Akka 2.0
Featuring Java, Scala and Akka Code Examples

Time to read: about 3 minutes.

ISBN 978-0-9842789-2-3
178 pages
Originally published in 2011, republished in March 2012, April 2012 and October 2012.
No longer available.

About This Book

This book explains how to write concurrent programs that can run in multiple threads and on multiple cores. A chapter is dedicated to java.util.concurrent, which underlies the Akka futures implementation. An overview of futures is provided through code examples. Special considerations for programming with futures are discussed.

The book provides complete reference information about the classes and methods for working with Akka futures. Each method of the Akka futures implementation is covered in detail; first introduced with short explanation, then followed by the Java signature and the Scala signature. Further information for programming in each language is provided, usually with code snippets.

Akka stack

Dozens of complete working code examples are provided with the book, at least one per method in each language. Similar programs were written in Java and Scala. If you know Java and are learning Scala, comparing the two programs will be instructive. Programs are provided with an SBT script, and instructions for creating Eclipse and IntelliJ IDEA projects from the SBT project. You can also use the SBT console to try code snippets interactively.

The code is provided as a git repository, which you can update any time.

Target Audience

Composable Futures with Akka 2.0 is intended for ‘the rest of us’ – Java and Scala programmers who would like to quickly learn how design and implement applications using composable futures. Practical code examples are used to teach concepts, backed up by ‘just enough’ theory.

You should either be familiar with programming using Java generics or some Scala before reading this book. Java programmers do not require prior experience with Scala; Scala programmers do not need to know Java. Some familiarity with git is assumed.

Did You Know?

Multicore concurrency requires multi-threaded programming, but that is hard to do properly.

Futures provide a fast, effective means of developing multi-threaded programs.

When to use futures and when to use actors?

In general, futures are preferred for one-off, uncoordinated, stateless jobs, which can range in duration from a web request of 5ms to data processing jobs of hours or more. Actors are generally preferred for coordinated or stateful activities.

Other implementations of futures are limited.

java.util.concurrent provides an elementary futures implementation; Google’s Guava and provides a slightly better implementation. The Akka and Twitter implementations are much more advanced. For example, transformations and operations on collections of futures are supported, along with numerous asynchronous callbacks. Akka futures is integrated with Akka actors for distributed computing.

Java and Scala can both use Akka Futures

The futures implementation bundled in the free, open source Akka stack is written in Scala. Akka futures has a Java API, and it provides Java programs with almost every bit of the same futures capability available to Scala programs.

Akka futures integrate easily with existing software projects.

The Promises chapter in the book includes a case study that shows how easy it is to integrate Akka futures into existing code. Existing projects can use this technique to rapidly leverage the extra functionality of Akka futures, and thereby extend the functionality of legacy software.

What are composable futures?

Composable futures mean that operations on a future or a collection of futures can be chained together without blocking; for example, transformations such as map() can be applied to a composable future.

The value of an Akka Future will normally become available sometime after creation, or it may incorporate an exception raised during computation. Akka Futures can also be preset to a value or exception when created. Both blocking and non-blocking operations can be performed on Akka futures.

Akka futures are being subsumed into Scala 2.10

The futures implementation provided by Akka 1.x was improved for Akka 2.0, and will be migrated to Scala 2.10 when it is released in early 2012.