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

Strong fault tolerance is one of the key features that Akka brings to the table. Actors in Akka form a hierarchy with each child’s parent actor being responsible for determining what to do when the child fails. This can propagate all the way up the hierarchy with each step deciding what to do when its children fail.

When an actor is told a child has thrown an exception, it can select from one of a handful of actions: it can resume the failed actor; it can restart the actor; it can escalate up to its parent; or it can just stop the failed actor entirely. Also, it can choose to apply this action to the one failed actor or to all its children at once.

Let’s look at an example:

There are a few things going on here, but the really important item to pay attention to is where Parent actor overrides the supervisorStrategy. This is a method that is defined by default in the Actor class, but which you are free to modify to suit your needs. In this case, we are telling Akka that in cases where a child actor throws a ChildException, we want to stop that child.

By default, if you do not override this, most exceptions (extending from the Exception class, to be clear) will cause a restart of the child. There are a couple of internal exceptions defined within Akka which can cause the child to be stopped and any error that is not an exception, but is of type Throwable will be escalated up the hierarchy. If the topmost level of the hierarchy receives an error (whether through the default handling or through explicit escalation) it will cause the entire actor system to halt.

Hopefully you can see how this gives you a lot of power. The motto of the Akka team is “let it fail.” The reason they say that is that it is often easier to build systems where discrete failures are expected and handle those failures through restarts, fail-over or other means external to the internal state. Planning for your actors to be restartable or to escalate the failures where appropriate leads to elegant designs that are easier to maintain than the often seen spaghetti of exception handling code with which far too many of us are familiar.

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, Scala,

Comments are closed.