In summary, welcome aboard! You’ve just received a whirlwind tour of the high points of Akka and should have some clue as to why it will be the awesome toolkit that you’ve heard about. When it comes to building highly concurrent and fault-tolerant applications on the JVM, Akka is a solid choice.
As we progress, you’ll learn how to apply the tools we’ve already dis- cussed to your application design and development. You’ll also start seeing a lot more code than we’ve seen thus far that will help establish a set of patterns for coding in Akka. Later on, we’ll establish a set of anti-patterns, because there certainly are a fair number of those. Like any decent power tool, if you point it straight at your eye and then run forward, bad things will happen. There are great ways to use Akka and there are also the power-tool- to-the-eye ways, and we’re going to favour the former.
You’ve learned a ton so far, and you should feel pretty awesome about that, but before you run out into the street naked declaring your superiority over the mere machine that you sit in front of, let’s cover some more of the nuts and bolts.
Flip the page and let’s go. . .
Actors
It’s time to start concurrency programming with actors. We’ll begin by ex- ploring the most basic mechanics of actor construction and operation, so you can get a feel for things and how most of the work gets done. Using that example, we’ll explore more about what actors are and how they work, building up use cases and understanding so that you’re armed with heaps of awesomeness that you can employ when solving your coding and design problems.
What follows is a single actor definition of a terrible Shakespearean actor and a poor sod who has to talk to him. The Shakespearean actor is a true Akka Actorand the poor sod ismain. In these early stages, I’ll show you some fairly atypical elements of actor programming, but they’ll help keep things familiar.
package zzz.akka.investigation
// All that's needed for now are three components from Akka import akka.actor.{Actor, Props, ActorSystem}
// Our Actor
class BadShakespeareanActor extends Actor { // The 'Business Logic'
def receive = {
case "Good Morning" =>
println("Him: Forsooth 'tis the 'morn, but mourneth " +
"for thou doest I do!") case "You're terrible" =>
println("Him: Yup")
} }
object BadShakespeareanMain {
val system = ActorSystem("BadShakespearean")
val actor = system.actorOf(Props[BadShakespeareanActor],
"Shake")
// We'll use this utility method to talk with our Actor def send(msg: String) {
println(s"Me: $msg") actor ! msg
Thread.sleep(100) }
// And our driver
def main(args: Array[String]) { send("Good Morning")
send("You're terrible") system.shutdown() }
}
We’ve already set up all of the dependencies and build infrastructure using sbt, so you should be able to place this code and give it a go. Put the content into
src/main/scala/zzz/akka/investigation/BadShakespeareanActor.scala, typesbt run, and you should see this:
Me: Good Morning
Him: Forsooth 'tis the 'morn but, mourneth for thou doest I do!
Me: You're terrible Him: Yup
Just a couple of guys having a painful chat. I said earlier that we’d see things that were atypical of actor programming, and this example certainly has some of them, but they tend to be items that we use all the time, so they should be easy to spot. There are three main things:
• We generally don’t use rawStrings to communicate with actors. The type checker can’t help us tell the difference between “Good Morning”
and “good morning”.
• Our actor doesn’t do anything and that’s pretty boring. A println doesn’t count as something.
• Thread.sleepis the big one. If your code has asleepin it, then you’re tying up an execution thread for some amount of time and we don’t do that in Akka. In other words, we’re doing it wrong.
In this chapter, you’ll learn how to write code that’s agile, decoupled, fast, non-blocking, type safe, and highly communicative. What we’ve seen thus far falls somewhat short of those goals, but it is also quite illustrative of some basic properties of actors. Let’s explore the mechanics behind what we’ve seen and start working on things that actuallyaretypical of actor pro- gramming in Akka.