Akka is used by many large companies to solve real problems. Akka is built to scare in many directions and to provide extreme flexibility. One of Akka’s goals is to ‘manage system overload.’
Akka uses Actors: ‘Akka’s unit of code organization.’ According to Klang, ‘Actors help you create concurrent, scalable, and fault-tolerant applications.’ Actors ‘keep many ‘policy decisions’ separate from the business logic.’ Actors originated in 1970s (‘like all cool stuff in computer science’) and Erlang has been using actors with ‘great success’ for several years.
Klang warned to avoid ‘thinking in terms of shared state, a leaky abstraction.’ He added that threads and locks are ‘a means of execution’ rather than structural. He said this mixes execution with business logic. Concurrent collections are good optimizations for local uses.
Actors are ‘distributable by design’ and Klang had a slide listing several bullets explaining this statement. He stated that an actor can be an alternative to ‘a thread,’ ‘an object instance,’ ‘a callback or listener,’ ‘a singleton or service,’ ‘a router, load-balancer, or pool,’ ‘Java EE session bean or Message-Driven Bean,’ ‘out of process service,’ and ‘finite state machines.’
Klang referenced a video of Carl Hewitt on actors. An actor is a ‘fundamental unit of computation that embodies several key characteristics.’
Klang showed code examples in my preferred format in a presentation: embedded in his slides with color syntax highlighting. He showed step 0 (‘DEFINE’) in which his code defined the Actor’s class and the Actor’s behavior.
Once defined, the first operation (I – ‘CREATE’) ‘creates a new instance of an Actor.’ The created Actor is extremely lightweight and its ‘state and behavior are indistinguishable from each other.’ He drove this last point home: ‘The only way to observe state is by sending an actor a message and seeing how the actor reacts.’ The Actor is ‘very strong encapsulation’ of state, behavior, and message queue.
Akka provides an
ActorSystem for creating Akka Actor instances. An instance of
Props is provided to the Actor because actors need props.
Step 2 (‘SEND’) involves ‘sending a message to an Actor’ and ‘everything happens reactively’ and ‘everything is asynchronous and lockless.’ Akka supports a ‘fire and forget’ mode with the actor’s
tell method. However, Akka provides guaranteed order of delivery. The reply is implemented in Akka with
Step 3 (‘BECOME’) ‘redefines the Actor’s behavior’ and is ‘triggered reactively by receipt of message.’ The reasons one might want to change the behavior of an actor at runtime include supporting highly contended actor transforming to an actor pool or to implement graceful degradation.
Actors can supervise other Actors, leading to Step 4 (‘SUPERVISE’). A ‘supevisor detects and responds to the failures of the Actor(s) it supervises’ and Klang stated that this translates to ‘a clean separation and processing and error handling.’
Klang talked about ‘failure management in Java, C, and C#’ where you are ‘given a single thread of control.’ He put it this way in a bullet: ‘If this thread blows up, you are screwed.’ The implication of this is that all ‘explicit error handling’ is done ‘within the single thread’ and ‘tangled up’ with the business code.
Klang said the way to deal with error handling is to push the error handling out away from the business logic. He then referenced the onion-layer error kernel. Klang talked about callbacks (
postRestart) provided for Actors to handle failures.
A Router is a special case of Actor. Klang showed a slide with code using a
RoundRobinRouter. He also showed being able to define the deployment scenario outside of the code in configuration file and referencing that from the code with a path. He took this example even further to show code for ‘remote deployment’ specifying a URL with the ‘akka’ protocol, a host name, and a port.
Everything that Klang presented to this point is available today as Akka 2.0. Klang said that there will be Akka Cluster in the to-be-released-soon Akka 2.1. He asked for feedback to ensure that the correct APIs and correct functionality are available for clustering in Akka 2.2. More information on Akka clustering is available in the specification, the user guide, and the code itself.
Reference: JavaOne 2012: Up, Up, and Out: Scaling Software with Akka from our JCG partner Dustin Marx at the Inspired by Actual Events blog.
Get ready to program in a whole new way!
Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.