|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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.
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:
Not bad for a simple number cruncher!
Figure 2 shows all the moving parts involved in the PrimeCalculator program.
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.
We'll start with the NumberRangeMessage and Result message classes, which are are simple serializable POJOs that contain data to be passed between actors.
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;
}
}
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.