Open source Java projects: Akka

Building distributed systems for concurrent and scalable Java applications

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

The HelloActor class has a main() method that sets up the ActorSystem. It creates an ActorSystem named "MySystem" and then creates an ActorRef that points to the HelloActor class named "actorRef". The main() method sends the actorRef a new HelloMessage instance with the message "Hello, Akka!" by invoking its tell() method. The tell() method is the mechanism for sending messages to actors asynchronously. The main() method then sleeps for a second and finally shuts down Akka. It shuts down by first telling the ActorSystem to stop the HelloActor actor reference, and then telling the ActorSystem it shut itself down.

HelloMessage doesn't demonstrate how to create a concurrent application using Akka, but it does demonstrate how to create various actor model components (ActorSystem, Actor, and ActorRef), and send messages to an Actor through the ActorRef. The prime number computing program in the next section will demonstrate Akka's concurrency model. You'll also get to see a better approach to shutting down the actor system, using a listener actor.

Computing prime numbers with Akka

The example application in this section is a complete Akka application that computes prime numbers for a given range. For example, if we were to list all of the prime numbers between 1 and 10, we would expect the following output: 1, 2, 3, 5, 7. This program won't be particularly challenging to code, but it will demonstrate how to do following:

  • Create a master actor
  • Create a round-robin router to distribute work across multiple worker actors
  • Communicate between worker actors and the master actor
  • Communicate between the master actor and a listener

Not bad for a simple number cruncher!

Figure 2 shows all the moving parts involved in the PrimeCalculator program.

A flow diagram of PrimeCalculator.
Figure 2. An actor-based system for computing prime numbers with Akka (click to enlarge)

As shown in Figure 2, the PrimeCalculator provides a calculate() method that sends a number-range message to the PrimeMaster class. The PrimeMaster divides this number range into 10 units and distributes them across 10 PrimeWorkers. The PrimeWorkers complete their work and then publish a Result message back to the PrimeMaster. The PrimeMaster aggregates all 10 Result responses, then publishes the aggregate Result object to the PrimeListener. The PrimeListener writes the results to the screen and then shuts down the ActorSystem.

In the next sections I'll discuss each of the main components of the PrimeCalculator in detail.

NumberRangeMessage and Result

We'll start with the NumberRangeMessage and Result message classes, which are are simple serializable POJOs that contain data to be passed between actors.

Listing 4. NumberRangeMessage.java

package com.geekcap.akka.prime.message;

import java.io.Serializable;

public class NumberRangeMessage implements Serializable
{
    private long startNumber;
    private long endNumber;

    public NumberRangeMessage(long startNumber, long endNumber) {
        this.startNumber = startNumber;
        this.endNumber = endNumber;
    }

    public long getStartNumber() {
        return startNumber;
    }

    public void setStartNumber(long startNumber) {
        this.startNumber = startNumber;
    }

    public long getEndNumber() {
        return endNumber;
    }

    public void setEndNumber(long endNumber) {
        this.endNumber = endNumber;
    }
}

Listing 5. Result.java

package com.geekcap.akka.prime.message;

import java.util.ArrayList;
import java.util.List;

public class Result
{
    private List<Long> results = new ArrayList<Long>();

    public Result()
    {
    }

    public List<Long> getResults()
    {
        return results;
    }
}

Both of these classes are pretty self-explanatory.

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