Akka入門安裝以及示例(Java)
阿新 • • 發佈:2019-02-11
一、安裝開發環境
1.確保安裝的java環境是1.6或以上;
2.設定JAVA_HOME環境變數(java SDK所安裝的目錄)
# export JAVA_HOME=..root of Java distribution..
# export PATH=$PATH:$JAVA_HOME/bin
3.可以使用以下指令檢視java的版本
# java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~10.04.2)
OpenJDK Server VM (build 20.0-b12, mixed mode)
二、下載開發包及設定開發環境
1.從http://akka.io/downloads/處下載最新的Akka開發包,並將其加壓到相應的目錄,例如/srv目錄下;
2.設定開發包的環境變數AKKA_HOME
# export AKKA_HOME=/srv/akka-2.0.3
三、編輯、編譯原始碼及執行
1.在/srv/akka-2.0.3/tutorial目錄下,建立Pi.java,寫入以下內容:
2.編譯
# javac -cp ../lib/scala-library.jar:../lib/akka/akka-actor-2.0.3.jar:../lib/akka/config-0.3.1.jar:. Pi.java
3.執行
# java -cp ../lib/scala-library.jar:../lib/akka/akka-actor-2.0.3.jar:../lib/akka/config-0.3.1.jar:. Pi
Pi approximation: 3.1415926435897883
1.確保安裝的java環境是1.6或以上;
2.設定JAVA_HOME環境變數(java SDK所安裝的目錄)
# export JAVA_HOME=..root of Java distribution..
# export PATH=$PATH:$JAVA_HOME/bin
3.可以使用以下指令檢視java的版本
# java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~10.04.2)
OpenJDK Server VM (build 20.0-b12, mixed mode)
二、下載開發包及設定開發環境
1.從http://akka.io/downloads/處下載最新的Akka開發包,並將其加壓到相應的目錄,例如/srv目錄下;
2.設定開發包的環境變數AKKA_HOME
# export AKKA_HOME=/srv/akka-2.0.3
三、編輯、編譯原始碼及執行
1.在/srv/akka-2.0.3/tutorial目錄下,建立Pi.java,寫入以下內容:
-
import akka.actor.ActorRef;
-
import akka.actor.ActorSystem;
-
import akka.actor.Props;
-
import akka.actor.UntypedActor;
-
import akka.actor.UntypedActorFactory;
-
import akka.routing.RoundRobinRouter;
-
import akka.util.Duration;
-
import java.util.concurrent.TimeUnit;
-
public class Pi {
-
public static void main(String[] args) {
-
Pi pi = new Pi();
-
pi.calculate(4, 10000, 10000);
-
}
-
static class Calculate {
-
}
-
static class Work {
-
private final int start;
-
private final int nrOfElements;
-
public Work(int start, int nrOfElements) {
-
this.start = start;
-
this.nrOfElements = nrOfElements;
-
}
-
public int getStart() {
-
return start;
-
}
-
public int getNrOfElements() {
-
return nrOfElements;
-
}
-
}
-
static class Result {
-
private final double value;
-
public Result(double value) {
-
this.value = value;
-
}
-
public double getValue() {
-
return value;
-
}
-
}
-
static class PiApproximation {
-
private final double pi;
-
private final Duration duration;
-
public PiApproximation(double pi, Duration duration) {
-
this.pi = pi;
-
this.duration = duration;
-
}
-
public double getPi() {
-
return pi;
-
}
-
public Duration getDuration() {
-
return duration;
-
}
-
}
-
public static class Worker extends UntypedActor {
-
private double calculatePiFor(int start, int nrOfElements) {
-
double acc = 0.0;
-
for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
-
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
-
}
-
return acc;
-
}
-
public void onReceive(Object message) {
-
if (message instanceof Work) {
-
Work work = (Work) message;
-
double result = calculatePiFor(work.getStart(), work.getNrOfElements());
-
getSender().tell(new Result(result), getSelf());
-
} else {
-
unhandled(message);
-
}
-
}
-
}
-
public static class Master extends UntypedActor {
-
private final int nrOfMessages;
-
private final int nrOfElements;
-
private double pi;
-
private int nrOfResults;
-
private final long start = System.currentTimeMillis();
-
private final ActorRef listener;
-
private final ActorRef workerRouter;
-
public Master(final int nrOfWorkers, int nrOfMessages, int nrOfElements, ActorRef
listener) {
-
this.nrOfMessages = nrOfMessages;
-
this.nrOfElements = nrOfElements;
-
this.listener = listener;
-
workerRouter = this.getContext().actorOf(new Props(Worker.class).withRouter(new RoundRobinRouter(nrOfWorkers)),
-
"workerRouter");
-
}
-
public void onReceive(Object message) {
-
if (message instanceof Calculate) {
-
for (int start = 0; start < nrOfMessages; start++) {
-
workerRouter.tell(new Work(start, nrOfElements), getSelf());
-
}
-
} else if (message instanceof Result) {
-
Result result = (Result) message;
-
pi += result.getValue();
-
nrOfResults += 1;
-
if (nrOfResults == nrOfMessages) {
-
// Send the result to the listener
-
Duration duration = Duration.create(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS);
-
listener.tell(new PiApproximation(pi, duration), getSelf());
-
// Stops this actor and all its supervised children
-
getContext().stop(getSelf());
-
}
-
} else {
-
unhandled(message);
-
}
-
}
-
}
-
public static class Listener extends UntypedActor {
-
public void onReceive(Object message) {
-
if (message instanceof PiApproximation) {
-
PiApproximation approximation = (PiApproximation) message;
-
System.out.println(String.format("\n\tPi
approximation: \t\t%s\n\tCalculation time: \t%s",
-
approximation.getPi(), approximation.getDuration()));
-
getContext().system().shutdown();
-
} else {
-
unhandled(message);
-
}
-
}
-
}
-
public void calculate(final int nrOfWorkers, final int nrOfElements, final int nrOfMessages) {
-
// Create an Akka system
-
ActorSystem system = ActorSystem.create("PiSystem");
-
// create the result listener, which will print the result and shutdown the system
-
final ActorRef listener = system.actorOf(new Props(Listener.class), "listener");
-
// create the master
-
ActorRef master = system.actorOf(new Props(new UntypedActorFactory() {
-
public UntypedActor create() {
-
return new Master(nrOfWorkers, nrOfMessages, nrOfElements, listener);
-
}
-
}), "master");
-
// start the calculation
-
master.tell(new Calculate());
-
}
- }
2.編譯
# javac -cp ../lib/scala-library.jar:../lib/akka/akka-actor-2.0.3.jar:../lib/akka/config-0.3.1.jar:. Pi.java
3.執行
# java -cp ../lib/scala-library.jar:../lib/akka/akka-actor-2.0.3.jar:../lib/akka/config-0.3.1.jar:. Pi
Pi approximation: 3.1415926435897883
Calculation time: 744 milliseconds