Posted on by & filed under Content - Highlights and Reviews, Information Technology, Web Development.

If you have taken a look at Akka or spent much time with Scala, you’ve probably run across the concept of futures at some point. If you haven’t seen them or taken the time to understand them, futures might seem a bit hard to grasp. But they really are a simple concept. A future is just a structure that lets you access the result of some computation that may or may not take place at some future point in time.

Let’s take a more careful look at this. Here’s a simple example using Akka’s support for futures. A warning is necessary that there is a bit of boilerplate necessary here, in some cases (more on this in a bit).

This example is admittedly simplistic. All it does is use Akka’s future factory object to create a future with an immediate value for a result and then wait on the result, which returns immediately, of course, though we specified a one second timeout.

The body of the Future block in this example could have performed some complex computation. Akka would execute that block on another thread (this is configurable, but that’s outside the scope of this post). Then, when you ask for the result, it would return it or timeout.

Let’s look at a slight more complex example, next.

Here we assume you want to execute some complex task that may take a while to respond. We don’t really care how long this takes, but when it finishes, we want to handle the result, whether it was successful or whether it failed. The callbacks, onSuccess and onFailure, take Scala partial functions as parameters (the type is actually called PartialFunction), which is a complex way of saying they take a block of case statements each of which will be tested in turn by the Scala runtime until it finds one that matches the value passed to it. This is a very useful approach that allows for clean handling of asynchronous method calls, but it can also get messy if you have a lot of futures to deal with.

Thankfully, Akka provides you a nice syntax for handling this situation. It does this by exploiting Scala’s for-comprehensions. Again, the details here are outside the scope of this post, but we can show what this looks like very easily. Let’s say you have a series of futures being returned by calls to various methods defined elsewhere and that each subsequent method depends on the result of the previous call.

Normally, this wouldn’t even work if those methods weren’t explicitly defined to take a Future as a parameter. But you can use a for-comprehension to define a structure for handling the result of these calls that passes the result of each call in sequence to the next item. You can even pass the final result to another method to process the result of the last method in the sequence.

The downside of this approach is that it will block as each step waits on the previous call to return, but it allows you to write very succinct code for handling this kind of sequencing.

As a final example, we can look at how this is useful within the context of Akka’s actors. Akka provides you the ability to send a message to an actor and wait for a response to be sent back. The response to this call is, naturally, a future. The ask method is what you use to call an actor when you expect a result. With this future you can define callbacks or use the for-comprehension approach we just looked at.

While Akka is perhaps best known as an actor framework, it is really futures that give you the tools to compose complex interactions between systems of actors. Understanding them is key to using Akka effectively.

Safari Books Online has the content you need

Take advantage of these Scala resources in Safari Books Online:

Scala is an object-oriented programming language for the Java Virtual Machine. In addition to being object-oriented, Scala is also a functional language, and combines the best approaches to OO and functional programming. Artima is very pleased to publish a new edition of the best-selling book, Programming in Scala, written by the designer of the language, Martin Odersky. Co-authored by Lex Spoon and Bill Venners, this book takes a step-by-step tutorial approach to teaching you Scala. Starting with the fundamental elements of the language, Programming in Scala introduces functional programming from the practitioner’s perspective, and describes advanced language features that can make you a better, more productive developer.
Beginning Scala introduces you to the Scala programming language and then guides you through Scala constructs and libraries that allow small and large teams to assemble small components into high-performance, scalable systems. You will learn why Scala is becoming the language of choice for Web 2.0 companies such as Twitter as well as enterprises such as Seimens and SAP.
Learn how to be more productive with Scala, a new multi-paradigm language for the Java Virtual Machine (JVM) that integrates features of both object-oriented and functional programming. With Programming Scala, you’ll discover why Scala is ideal for highly scalable, component-based applications that support concurrency and distribution. Programming Scala clearly explains the advantages of Scala as a JVM language. You’ll learn how to leverage the wealth of Java class libraries to meet the practical needs of enterprise and Internet projects more easily. Packed with code examples, this book provides useful information on Scala’s command-line tools, third-party tools, libraries, and available language-aware plugins for editors and IDEs.
Lift in Action: The Simply Functional Web Framework for Scala is a step-by-step exploration of the Lift framework. It moves through the subject quickly using carefully crafted, well-explained examples that make you comfortable from the start. This book is written for developers who are new to both Scala and Lift.

About this author

Thomas Lockney has been writing software professionally for over 15 years and has been playing with computers for even longer. He discovered Scala nearly 4 years ago and has been known to run screaming from Java in the years since. You can find him on Twitter as @tlockney when he’s not too busy going on adventures with his lovely wife and their canine companions.

Tags: Akka, futures, Scala,

Comments are closed.