Fixing the Akka Java tutorial

The first Akka Java tutorial is a nice introduction to Akka actors and messages.  In terms of programming, this method has been around for a long time and the Akka libraries provide a good way for the Java community to use messages and actors.

However, the tutorial in its current state (July 2013) has some problems that will trip up developers and leave you searching for solutions - here are some answers.  As usual, if you know what the problem is, then it's easy to deal with - in fact, the code is fine, but only when mixed with the right Akka.

First, the links to the tutorial in github are wrong - look on this tree (2.0.2). Alternatively, you can cut and paste the program code from the tutorial here

Which version of Akka do you want to run? The tutorial code as is requires an older version of Akka - the tutorial was written to Akka 2.0.2, but also works with Akka 2.0.5 which can be downloaded here or via the download page.  It won't work as is with Akka 2.2.0.

Akka 2.2.0+ - change one line
The most annoying issue that I came across was the error below.  It's a bit easier to fix when using Eclipse (or similar IDE), but I was using a text editor:
tutorial\ error: method tell in class ActorRef cannot be applied to given types;
     master.tell(new Calculate());
  required: Object,ActorRef
  found: Calculate
  reason: actual and formal argument lists differ in length 
(compiled with:
 C:\Tools\akka-2.2.0>"c:\Program Files\Java\jdk1.7.0_09\bin\javac.exe" 
          -cp lib\scala-library.jar;lib\akka\akka-actor_2.10-2.2.0.jar tutorial\ 

The method signature for master.tell() should accept messages only or messages and actor references - except that they were deprecated in Akka 2.1 and removed in Akka 2.2.  Make sure you can find the right documentation.

To fix, add master.noSender() or null to the method call if using an Akka version greater than 2.1. Then recompile and run including the same jars as above.

Second solution - use older version of Akka

For the sake of simplicity, comment out the package statement at the top of the program:

Now, it's just a matter of compiling and running the code - as easy as it was supposed to be.  Put the tutorial directory in the akka-2.0.5 directory so that it's at the same level as Akka's lib directory.

javac.exe -cp lib/scala-library.jar:lib/akka/akka-actor-2.0.5.jar tutorial/ 
(for *nix/linux)
javac.exe -cp lib\scala-library.jar;lib\akka\akka-actor-2.0.5.jar tutorial\ 
(for windows)
or more generically: 
javac.exe -cp path_to_akka/lib/scala-library.jar:path_to_akk/libakka/akka-actor-2.0.5.jar \
(for linux - similarly for windows)
(compiled with java 7)

Running the example requires just a little more:
java.exe -cp lib\scala-library.jar;lib\akka\akka-actor-2.0.5.jar;tutorial;lib\akka\config-0.3.1.jar Pi 
(for windows, run from the common directory if you've done that)
java.exe -cp lib/scala-library.jar;lib/akka/akka-actor-2.0.5.jar;tutorial;lib/akka/config-0.3.1.jar Pi 
(for linux, *nix, run from the common directory if you've done that)
p_a=path_to_akka #set p_a equal to the path to akka, for example /opt/akka/lib
java.exe -cp \
$p_a/lib/scala-library.jar:$p_a/lib/akka/akka-actor-2.0.5.jar:path_to_tutorial/tutorial:$p_a/lib/akka/config-0.3.1.jar Pi 
(for linux, *nix, specifying exact paths)

When run, the output should look like:
        Pi approximation:               3.1415826535897926
        Calculation time:       1187 milliseconds


Popular Posts