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

If you find yourself having to develop with concurrency, you should do yourself a favor and take a look at Akka. For those not already familiar with it, Akka is a framework that provides an actor-based approach to concurrency. Having said that, it would probably help to explain what actors (and the actor model) are.

The actor model was first described by Carl Hewitt in 1973 as a way to model concurrent computation. The most basic concept you need to understand the model is actors. Actors are simple: they are entities which receive messages. Upon receipt of those messages, they can create additional actors, send messages to other actors and change their behavior for the next message they receive. Actors can also hold internal state, but they are not required to and in many cases will not. They also refer to each other by addresses, rather than by direct reference.

You’re probably asking yourself why this matters? The simple reason is it lets you work around a lot of cases where you would normally find yourself using other well-known tools of the trade in the world of concurrency: locks, semaphones, mutexs, and such. This alone is a pretty compelling argument, but it also makes it nearly trivial to scale out, once you understand how to break your problem up into appropriate sized pieces to take advantage of the actor-based approach.

I think it’s good to make an important point clear before we move on: actors are not threads. Actors will use a thread while they are handling their current message, though. The way that actors are scheduled on available threads is up to you (in Akka, this is available via configuration of the dispatcher – more on this shortly). Since an actor will use a thread after it receives a message and needs to handle it, the actor can block. As we all know by now, blocking is where bad things happen to otherwise good concurrency citizens. So be sure to be mindful of how you write your actor code.

Now, let’s get to some actual Akka code. We’ll start with a very basic example:

I’m not going to spend time explaining the Scala syntax, assuming that it’s not too different from what you are familiar with from other languages. There are many good Scala tutorials online if you are interested in learning more. The import parts are the creation of an ActorSystem and then the creation of an actor within that system. In this case, I’m creating an anonymous actor that just accepts any kind of message you send it – msg is a universal match for any value, since it’s not qualified in any way. It then prints it to the screen.

You should take a moment to understand what purpose the actor system serves in Akka. Your actors will form some form of hierarchy. That hierarchy defines a system of actors. There are a few features that Akka gives you as part of that hierarchy and to help you in structuring it, but it all begins with the actor system. This is where you will create your top-level actors and it lets you get access to other actors in the system among other things (most of which we can ignore for now).

Now for a slightly more substantial example.

Of course, this is still a fairly trivial actor system, but it shows a bit more of the capabilities of actors and how they interact. First, you’ll notice we set up our message types: Start, Ping and Pong. We then define two actors to handle the messages, create our actor system and send an initial Start message to pinger, the PingActor instance.

One important item, which is really core Scala syntax, is the way the messages are being handled in the receive methods. You’ll notice that this time the case statements are matching the exact message types that were defined earlier.

This is just a taste of what you can do with Akka. In upcoming posts, we will cover a few other important features to give you an idea of why Akka is a powerful toolkit for building highly scalable concurrent systems. Hopefully it will give you enough of a sense to decide whether it could play a role in your future applications.

Safari Books Online has the content you need

Take advantage of these Scala mobile 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: actors, Akka, concurrency, Scala,

2 Responses to “Simplifying Concurrency with Scala and Akka”

  1. Sergey Weiss

    The last example doesn’t seem to do the work for two reasons.
    First is that PongActor’s receive is syntactically incorrect. I’ve fixed like that:
    case Ping(count) if (count > 0) => { /* your case code goes here */ }
    Second is that ponger will never receive a message from pinger since PingActor only sends to sender. And ponger is not a sender in the example.

  2. tlockney


    thanks for point out that issue. It turns out I accidentally included an earlier draft that had the incorrect code. The post has now been updated to include the correct code. Please let me know if you have any more issues.