Hello, Actors
Your first Actor code
In the previous post we began with a brief introduction of what Akka is and that it helps you write highly scalable, fault tolerant, distributed applications.
previous post:
In this post we’ll start begin by looking at what actors are and how they operate.
Actor model
The actor model was first proposed by Carl Hewitt in 1973. Under this model, an actor is the primitive of concurrent computation. Each actor has a state and can communicate with other actors by sending them messages. The messages received are stored in a mailbox and are processed one-by-one by the actor.
As messages are processed, the receiving actor may update it’s state, perform some computation, send a message to some other actor, or even spawn a new actor.
There’s a couple more things to note about the actor model. One, actors never expose their state except via messages. If one actor wishes to know the state of the other actor, it does so by sending a message asking for the state. Second, the message sending is asynchronous.
If all this sounds confusing, think of actors as little lego figurines talking to each other.
Now, let’s get into writing some code.
Hello, world!
The focus of this series is to introduce you to the Akka framework using the Scala API. I’ll be using IntelliJ IDEA as my IDE throughout the series. I’m skipping the steps to setup IntelliJ and Scala plugin for the sake of brevity.
Begin by creating a new Scala SBT project. After that, add the Akka library to your build.sbt. Here’s what my build.sbt looks like:
If you look at line 7, we’re adding Akka actors library as a dependency. Line 9 is just a convenience so we can run multiple applications from within the same project using SBT.
Next, create a package named “hello” under the Scala folder in your IDE.
Next create a Scala class named GreetingsActor under the “hello” package and paste the following code in it:
You create an actor by extending the akka.actor.Actor base trait. The logic goes inside receive which returns a partial function defining the behavior of the actor. The case class represent the messages that will be passed to the actor. The companion object simply contains the properties for this actor. It’s a good practice to provide a factory method on a companion object to specify the props as it keeps the properties as close to the actor as possible. Why we did this will become clear soon.
Next, create an object named HelloAkka and extend the App trait. This will let us run the code within that object.
We begin by creating an actor system. An actor system is the universe in which your actors will exist. From this, we retrieve a reference to our greetings actor by looking for an actor that matches the properties that we specify. Notice that you got back an instance of ActorRef and not an Actor directly. This way that actor’s internal state remains hidden from the outside world. Next we send a message to the actor and wait for 2 seconds for the actor to finish processing the message it has received. The actor is running in its own thread. Then, we kill the actor and gracefully shutdown the system.
If you look at the console, you’d see the output. That’s it, you’ve written your first Akka actors code. More to follow soon.