Getting started with Akka
10 Feb 2016Akka is a library designed for building applications using the actor model. From their site:
Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.
In today’s post, I’m going to start with some of the primitives to using this framework.
Messages
Actors process messages that you’ll define in your modules. For today’s example, I’m going to implement a very basic logging application. Messages sent into this system are expected to be logged out to the console. To start off, we define the messages for this system:
Using scala’s case classes we can clean up the definition of these log messages. We have a message that will do general logging LogMessage
, one that will log a string in LogString
and one that will dissect and log out an exception object LogException
.
Actor Logic
We now focus on the logic required to log information out from our actor. This is really quite simple; we’re just going to push everything out to the console:
The receive
method is just a big pattern matching statement. Each of the message types are handled in here. Note how LogString
and LogException
send messages to self
. self
is a built-in, given to us representing this actor. All we’re doing is just on-forwarding the message in the string and exception cases.
Creating a system
We have actors; we have messages to pass between the actors; we now need a system that the actors will participate in.
Using the tell
and ask
methods, we can send and send/receive messages to/from this actor. We also can create a logic-less actor that just acts as a message sender/receiver:
Mailboxes are an important abstraction; they hold messages for actors. Each actor has its own mailbox, but we’ve created one above attached to a system that we can pipe messages into:
Lots of Actors
A slightly more complex topic is to create a pool of actors. In this next snippet, we’ll create a RoundRobinPool.
Now that we’ve created a pool, it’s time to smash!
Scheduled
Finally, we can schedule these messages to be sent . . as if they were sent from no where using the actor system that we’d created earlier:
This will send a LogString
message to the actor system actors
after zero seconds and then another message every second there after.