1. 程式人生 > >Akka入門安裝以及示例(Java)

Akka入門安裝以及示例(Java)

一、安裝開發環境

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,寫入以下內容:

  1. import akka.actor.ActorRef;
  2. import akka.actor.ActorSystem;
  3. import akka.actor.Props;
  4. import akka.actor.UntypedActor;

  5. import akka.actor.UntypedActorFactory;
  6. import akka.routing.RoundRobinRouter;
  7. import akka.util.Duration;
  8. import java.util.concurrent.TimeUnit;
  9. public class Pi {
  10.     public static void main(String[] args) {
  11.         Pi pi = new Pi();
  12.         pi.calculate(4, 10000, 10000);
  13.     }
  14.     static class Calculate {
  15.     }

  16.     static class Work {
  17.         private final int start;
  18.         private final int nrOfElements;
  19.         public Work(int start, int nrOfElements) {
  20.             this.start = start;
  21.             this.nrOfElements = nrOfElements;
  22.         }
  23.         public int getStart() {
  24.             return start;
  25.         }
  26.         public int getNrOfElements() {
  27.             return nrOfElements;
  28.         }
  29.     }
  30.     static class Result {
  31.         private final double value;
  32.         public Result(double value) {
  33.             this.value = value;
  34.         }
  35.         public double getValue() {
  36.             return value;
  37.         }
  38.     }
  39.     static class PiApproximation {
  40.         private final double pi;
  41.         private final Duration duration;
  42.         public PiApproximation(double pi, Duration duration) {
  43.             this.pi = pi;
  44.             this.duration = duration;
  45.         }
  46.         public double getPi() {
  47.             return pi;
  48.         }
  49.         public Duration getDuration() {
  50.             return duration;
  51.         }
  52.     }
  53.     public static class Worker extends UntypedActor {
  54.         private double calculatePiFor(int start, int nrOfElements) {
  55.             double acc = 0.0;
  56.             for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
  57.                 acc += 4.* (- (% 2) * 2) / (* i + 1);
  58.             }
  59.             return acc;
  60.         }
  61.         public void onReceive(Object message) {
  62.             if (message instanceof Work) {
  63.                 Work work = (Work) message;
  64.                 double result = calculatePiFor(work.getStart(), work.getNrOfElements());
  65.                 getSender().tell(new Result(result), getSelf());
  66.             } else {
  67.                 unhandled(message);
  68.             }
  69.         }
  70.     }
  71.     public static class Master extends UntypedActor {
  72.         private final int nrOfMessages;
  73.         private final int nrOfElements;
  74.         private double pi;
  75.         private int nrOfResults;
  76.         private final long start = System.currentTimeMillis();
  77.         private final ActorRef listener;
  78.         private final ActorRef workerRouter;
  79.         public Master(final int nrOfWorkers, int nrOfMessages, int nrOfElements, ActorRef listener) {
  80.             this.nrOfMessages = nrOfMessages;
  81.             this.nrOfElements = nrOfElements;
  82.             this.listener = listener;
  83.             workerRouter = this.getContext().actorOf(new Props(Worker.class).withRouter(new RoundRobinRouter(nrOfWorkers)),
  84.                            "workerRouter");
  85.         }
  86.         public void onReceive(Object message) {
  87.             if (message instanceof Calculate) {
  88.                 for (int start = 0; start < nrOfMessages; start++) {
  89.                     workerRouter.tell(new Work(start, nrOfElements), getSelf());
  90.                 }
  91.             } else if (message instanceof Result) {
  92.                 Result result = (Result) message;
  93.                 pi += result.getValue();
  94.                 nrOfResults += 1;
  95.                 if (nrOfResults == nrOfMessages) {
  96.                     // Send the result to the listener
  97.                     Duration duration = Duration.create(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS);
  98.                     listener.tell(new PiApproximation(pi, duration), getSelf());
  99.                     // Stops this actor and all its supervised children
  100.                     getContext().stop(getSelf());
  101.                 }
  102.             } else {
  103.                 unhandled(message);
  104.             }
  105.         }
  106.     }
  107.     public static class Listener extends UntypedActor {
  108.         public void onReceive(Object message) {
  109.             if (message instanceof PiApproximation) {
  110.                 PiApproximation approximation = (PiApproximation) message;
  111.                 System.out.println(String.format("\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s",
  112.                                                  approximation.getPi(), approximation.getDuration()));
  113.                 getContext().system().shutdown();
  114.             } else {
  115.                 unhandled(message);
  116.             }
  117.         }
  118.     }
  119.     public void calculate(final int nrOfWorkers, final int nrOfElements, final int nrOfMessages) {
  120.         // Create an Akka system
  121.         ActorSystem system = ActorSystem.create("PiSystem");
  122.         // create the result listener, which will print the result and shutdown the system
  123.         final ActorRef listener = system.actorOf(new Props(Listener.class), "listener");
  124.         // create the master
  125.         ActorRef master = system.actorOf(new Props(new UntypedActorFactory() {
  126.             public UntypedActor create() {
  127.                 return new Master(nrOfWorkers, nrOfMessages, nrOfElements, listener);
  128.             }
  129.         }), "master");
  130.         // start the calculation
  131.         master.tell(new Calculate());
  132.     }
  133. }

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