Tuesday, 15 November 2011

Akka 1.2 Made it Simple to Get Started

Dear Junior

The release of Akka 1.2 is about a month old, but it contains a change that is makes it much easer to try out Akka. It is not some revolutionary new feature, quite the contrary. They have simplified the package structure of Akka so much that the core Akka actor package now has no external dependencies. Correct: no, none, zip external dependencies - stands for itself.

Earlier, if you wanted to try out Akka, you had the problem that Akka relied on a ton of other open source libraries. So, if you just wanted to try out to write a simple actor, you still had to download a long list of other jars. Well, that can be handled by maven, or by the wonderful little tool "sbt" (Simple Build Tool), but still that is a threshold to get over before getting started. And that threshold has noting to do with "understanding actors".

In Akka 1.2 they have done a wonderful job of splitting out dependencies and managed to refine to "core actor" parts to be self-contained, i e it does not require any other libraries in place to work.

To get started writing your first Akka actor, simply download Akka 1.2 and put "akka-actor-1.2.jar" on your classpath. Ready to start hakking.

An Example

In InteillJ, I create a new project "akka12" in a library with the same name:

dajob05:akka12 danbj$ pwd
dajob05:akka12 danbj$ ls -l
total 8
-rw-r--r-- 1 danbj staff 796 15 Nov 15:55 akka12.iml
drwxr-xr-x 2 danbj staff 68 15 Nov 15:55 src

Well, I happen to use IntelliJ, but any other modern IDE would work as well.

Now, I create a "lib" directory for third party libraries (i e Akka), download akka and put the jar in "lib"

mkdir lib
curl http://akka.io/downloads/akka-actors-1.2.zip >akka-1.2.zip
unzip akka-1.2.zip akka-actors-1.2/lib/akka/akka-actor-1.2.jar
mv akka-actors-1.2/lib/akka/akka-actor-1.2.jar lib/

Now the self-contained Akka jar is in the lib directory.

dajob05:akka12 danbj$ ls lib

I add the jar as a project library in my IntelliJ project, and now I can create an actor class together with a small script that sends a message to that actor.

import akka.actor.Actor

object helloworld extends App {
  val worlder = Actor.actorOf[Worlder]
  worlder ! "hello"

class Worlder extends Actor {
  def receive = {
    case msg => println(msg + " world")

Running it (from inside the IDE) gives the expected

hello world

I just love what the Typesafe crowd have done to make it so easy to get started.

Happy hakking