For those not familiar, Akka is a Scala library for implementing the Actor Model. Technically you can use Java as well, but things are a little more concise and idiomatic in Scala.

Let’s get started shall we. The first thing you should have installed is sbt. Then you’ll want to create a new folder for your project hello-akka. Within that folder, let’s create a build.sbt file with the following contents:

 1 name := "Hello Akka"
 2  
 3 version := "0.0.1"
 4  
 5 scalaVersion := "2.10.2"
 6  
 7 resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
 8  
 9 libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.2.0"
10  
11 libraryDependencies += "com.typesafe.akka" %% "akka-kernel" % "2.2.0"

You can see that we just have some basic setup with names and versions. You’ll also notice that we’re delcaring akka-actor and akka-kernel as our dependencies. The main library is akka-actor which we will (obviously) define our actor in. The second is a little less obvious, akka-kernel, and will be used to define our akka-instance as a standalone setup (not as a library within another project).

The next thing we want to do is create a couple of actors to perform an asynchronous hello-world:

 1 import akka.actor.{Actor, Props}
 2  
 3 object HelloWorldActor {
 4   case object Tick
 5 }
 6  
 7 class HelloWorldActor extends Actor {
 8   val greeter = context.actorOf(Props[Greeter], "greeter")
 9  
10   def receive: Actor.Receive = {
11     case HelloWorldActor.Tick => greeter ! Greeter.Greet
12   }
13 }
 1 import akka.actor.Actor
 2  
 3 object Greeter {
 4   case object Greet
 5 }
 6 class Greeter extends Actor {
 7   def receive = {
 8     case Greeter.Greet => {
 9       println("Hello, World")
10     }
11   }
12 }

Take note that these files were created within the directory src/main/scala within the hello-akka directory. From the above, you’ll notice that the HelloWorldActor receives a Tick event and then sends a message to the Greeter actor which then prints "Hello, World". You may be wondering what is sending the Tick event to the HelloWorldActor to start with. For this we have to define our kernel:

 1 import akka.actor.{ActorSystem, Props}
 2 import akka.kernel.Bootable
 3 import scala.concurrent.duration._
 4  
 5 class HelloWorldKernel extends Bootable {
 6   val system = ActorSystem("helloworldkernel")
 7   import system.dispatcher
 8  
 9   def startup() {
10     val helloWorldActor = system.actorOf(Props[HelloWorldActor])
11     system.scheduler.schedule(0 milliseconds,
12       500 milliseconds,
13       helloWorldActor,
14       HelloWorldActor.Tick)
15   }
16  
17   def shutdown() {
18     system.shutdown()
19   }
20 }

This is the entry-point for our application. You can see that we are creating an instance of the HelloWorldActor and scheduling a message be sent (Tick) every 500 milliseconds.

Done!

That’s all there is to creating a standalone Akka, pretty simple. There are various ways you can run your application, but I feel the easiest is with scripts that ship with Akka. To add that to your project, run the following script from within the hello-akka directory:

1 wget http://downloads.typesafe.com/akka/akka-2.2.1.tgz
2 tar xzf akka-2.2.1.tgz
3 mv akka-2.2.1 akka
4 rm akka-2.2.1.tgz
5 dos2unix akka/bin/akka

I typically create a Makefile for running my project just because I find that easier. You could do something like the following:

 1 all: build deploy
 2 run: build deploy start
 3  
 4 build:
 5   sbt package:clean
 6   sbt package
 7  
 8 start:
 9   ./akka/bin/akka HelloWorldKernel
10  
11 deploy:
12   mv target/scala-2.10/hello-akka_2.10-0.0.1.jar akka/deploy/

And then just run make run to start your project. You should see something like the following:

 1 Starting Akka...
 2 Running Akka 2.2.0
 3 Deploying file:/Users/jmurray/IdeaProjects/HelloAkka/akka/deploy/hello-akka_2.10-0.0.1.jar
 4 Starting up HelloWorldKernel
 5 Successfully started Akka
 6 Hello, World
 7 Hello, World
 8 Hello, World
 9 Hello, World
10 ...