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

转载:http://blog.chinaunix.net/uid-25885064-id-3401962.html

             http://akka.io/

你可能感兴趣的:(log)