Open source Java projects: Akka

Building distributed systems for concurrent and scalable Java applications

1 2 3 4 5 6 Page 3
Page 3 of 6

Hello, Message!

In this section I'll demonstrate the simplest way to build an Akka application. My generic "Hello World" app will use the default configuration for most options and will only define a single actor. This way you'll learn the bare minimum that is required to build and execute an actor before we try writing a more complex program. You can download the project source code for this article at any time.

As I mentioned earlier, there are several ways to build an Akka application. This example uses Maven to manage Akka as a dependency and runs Akka embedded inside of the application itself. Listing 1 shows the contents of the Maven pom.xml file.

Listing 1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.geekcap.akka</groupId>
    <artifactId>AkkaSample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AkkaSample</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.geekcap.akka.HelloActor</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.10</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Note that this POM file includes Akka version 2.1.2 as a dependency. In the build section, it sets the Java build version to 1.6, tells Maven to package the dependent JAR files into the target lib directory, and defines a main class to execute.

Akka operates by passing messages to actors, so Listing 2 shows the source code for HelloMessage.java.

Listing 2. HelloMessage.java

package com.geekcap.akka;

import java.io.Serializable;

public class HelloMessage implements Serializable
{
    private String message;

    public HelloMessage(String message)
    {
        this.message = message;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }
}

The HelloMessage class is a simple POJO that implements the Serializable interface. This is important because if you do move your Actors to another machine your messages must be serializable. Making all messages serializable ensures that you will not have issues later on.

Listing 3 shows the meat of the Akka code in the HelloActor class.

Listing 3. HelloActor.java

package com.geekcap.akka;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;

public class HelloActor extends UntypedActor
{
    public void onReceive(Object message)
    {
        if( message instanceof HelloMessage)
        {
            System.out.println( "My message is: " + ( (HelloMessage)message ).getMessage() );
        }
    }

    public static void main( String[] args )
    {
        ActorSystem actorSystem = ActorSystem.create( "MySystem" );
        ActorRef actorRef = actorSystem.actorOf( new Props( HelloActor.class ), "myActor" );
        actorRef.tell( new HelloMessage( "Hello, Akka!" ) );

        try
        {
            Thread.sleep( 1000 );
        }
        catch( Exception e ) {}

        actorSystem.stop( actorRef );
        actorSystem.shutdown();


    }
}

Listing 3 defines the HelloActor class, which extends the UntypedActor base class. This class has a single method that needs to be implemented: onReceive(). The onReceive() method is invoked by the HelloActor's Mailbox when the MessageDispatcher gives the Mailbox a thread time-slice, if there is a message waiting in the Mailbox's MessageQueue. The message is passed as an object so we check to see if the message received is a HelloMessage. If the message is a HelloMessage, then the HelloActor will invoke the HelloMessage's getMessage() method to print out the message.

1 2 3 4 5 6 Page 3
Page 3 of 6