Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Inital version of a Giter8 Akka 2.0 Pi Calculation Tutorial for Java.

  • Loading branch information...
commit d22414e24a014d8869c50b865335f28fcf3a1b1d 1 parent f1949f5
Björn Antonsson bantonsson authored
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +target/
  2 +project/project/
55 README.md
Source Rendered
... ... @@ -1,4 +1,57 @@
1   -A [giter8] [Akka] 2.0 tutorial project using [Java] and [Maven].
  1 +A [giter8] [Akka] 2.0 tutorial project using [Java] and [Maven].
  2 +
  3 +Prerequisites to use this tutorial is to have [Java], [Maven] and [giter8] installed.
  4 +
  5 +The next step is to open a terminal window and:
  6 +
  7 + $ cd /Users/theuser/code (or where ever you would like your code to end up)
  8 + $ g8 typesafehub/akka-first-tutorial-java
  9 +
  10 + Akka 2.0 Pi Calculation Tutorial with Java and Maven
  11 +
  12 + name [Akka Pi Calculation Tutorial]:
  13 + group_id [org.example]:
  14 + artifact_id [pi-calculation]:
  15 + package [org.example]:
  16 + version [0.1-SNAPSHOT]:
  17 + akka_version [2.0]:
  18 +
  19 + Applied typesafehub/akka-first-tutorial-java.g8 in akka-pi-calculation-tutorial
  20 +
  21 +Okay, so now you have created the tutorial as a local project on your computer.
  22 +To run and test it use [Maven]:
  23 +
  24 + $ cd akka-pi-calculation-tutorial
  25 + $ mvn compile exec:java
  26 + [INFO] Scanning for projects...
  27 + [INFO]
  28 + [INFO] ------------------------------------------------------------------------
  29 + [INFO] Building Akka Pi Calculation Tutorial 0.1-SNAPSHOT
  30 + [INFO] ------------------------------------------------------------------------
  31 + [INFO]
  32 + [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ pi-calculation ---
  33 + [debug] execute contextualize
  34 + [INFO] skip non existing resourceDirectory /Users/theuser/code/akka-pi-calculation-tutorial/src/main/resources
  35 + [INFO]
  36 + [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ pi-calculation ---
  37 + [WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
  38 + [INFO] Compiling 1 source file to /Users/theuser/code/akka-pi-calculation-tutorial/target/classes
  39 + [INFO]
  40 + [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ pi-calculation >>>
  41 + [INFO]
  42 + [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ pi-calculation <<<
  43 + [INFO]
  44 + [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ pi-calculation ---
  45 + Pi approximation: 3.1415926435897883
  46 + Calculation time: 565 milliseconds
  47 + [INFO] ------------------------------------------------------------------------
  48 + [INFO] BUILD SUCCESS
  49 + [INFO] ------------------------------------------------------------------------
  50 + [INFO] Total time: 3.027s
  51 + [INFO] Finished at: Thu Mar 08 21:03:48 CET 2012
  52 + [INFO] Final Memory: 9M/81M
  53 + [INFO] ------------------------------------------------------------------------
  54 +
2 55
3 56 [giter8]: https://github.com/n8han/giter8
4 57 [Akka]: http://akka.io
1  build.sbt
... ... @@ -0,0 +1 @@
  1 +seq(giter8Settings :_*)
1  project/build.properties
... ... @@ -0,0 +1 @@
  1 +sbt.version=0.11.2
1  project/plugins.sbt
... ... @@ -0,0 +1 @@
  1 +addSbtPlugin("net.databinder" % "giter8-plugin" % "0.3.2")
8 src/main/g8/default.properties
... ... @@ -0,0 +1,8 @@
  1 +description=Akka 2.0 Pi Calculation Tutorial with Java and Maven
  2 +
  3 +name=Akka Pi Calculation Tutorial
  4 +group_id=org.example
  5 +artifact_id=pi-calculation
  6 +package=org.example
  7 +version=0.1-SNAPSHOT
  8 +akka_version=2.0
43 src/main/g8/pom.xml
... ... @@ -0,0 +1,43 @@
  1 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  2 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4 + http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <modelVersion>4.0.0</modelVersion>
  6 +
  7 + <groupId>$group_id$</groupId>
  8 + <artifactId>$artifact_id$</artifactId>
  9 + <version>$version$</version>
  10 + <name>$name$</name>
  11 +
  12 + <properties>
  13 + <akka.version>$akka_version$</akka.version>
  14 + </properties>
  15 +
  16 + <repositories>
  17 + <repository>
  18 + <id>typesafe-releases</id>
  19 + <url>http://repo.typesafe.com/typesafe/releases</url>
  20 + </repository>
  21 + </repositories>
  22 +
  23 + <dependencies>
  24 + <dependency>
  25 + <groupId>com.typesafe.akka</groupId>
  26 + <artifactId>akka-actor</artifactId>
  27 + <version>\${akka.version}</version>
  28 + </dependency>
  29 + </dependencies>
  30 +
  31 + <build>
  32 + <plugins>
  33 + <plugin>
  34 + <groupId>org.codehaus.mojo</groupId>
  35 + <artifactId>exec-maven-plugin</artifactId>
  36 + <version>1.2.1</version>
  37 + <configuration>
  38 + <mainClass>$package$.Pi</mainClass>
  39 + </configuration>
  40 + </plugin>
  41 + </plugins>
  42 + </build>
  43 +</project>
179 src/main/g8/src/main/java/$package$/Pi.java
... ... @@ -0,0 +1,179 @@
  1 +/**
  2 + * Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
  3 + */
  4 +
  5 +package $package$;
  6 +
  7 +import akka.actor.ActorRef;
  8 +import akka.actor.ActorSystem;
  9 +import akka.actor.Props;
  10 +import akka.actor.UntypedActor;
  11 +import akka.actor.UntypedActorFactory;
  12 +import akka.routing.RoundRobinRouter;
  13 +import akka.util.Duration;
  14 +import java.util.concurrent.TimeUnit;
  15 +
  16 +public class Pi {
  17 +
  18 + public static void main(String[] args) {
  19 + Pi pi = new Pi();
  20 + pi.calculate(4, 10000, 10000);
  21 + }
  22 +
  23 + static class Calculate {
  24 + }
  25 +
  26 + static class Work {
  27 + private final int start;
  28 + private final int nrOfElements;
  29 +
  30 + public Work(int start, int nrOfElements) {
  31 + this.start = start;
  32 + this.nrOfElements = nrOfElements;
  33 + }
  34 +
  35 + public int getStart() {
  36 + return start;
  37 + }
  38 +
  39 + public int getNrOfElements() {
  40 + return nrOfElements;
  41 + }
  42 + }
  43 +
  44 + static class Result {
  45 + private final double value;
  46 +
  47 + public Result(double value) {
  48 + this.value = value;
  49 + }
  50 +
  51 + public double getValue() {
  52 + return value;
  53 + }
  54 + }
  55 +
  56 + static class PiApproximation {
  57 + private final double pi;
  58 + private final Duration duration;
  59 +
  60 + public PiApproximation(double pi, Duration duration) {
  61 + this.pi = pi;
  62 + this.duration = duration;
  63 + }
  64 +
  65 + public double getPi() {
  66 + return pi;
  67 + }
  68 +
  69 + public Duration getDuration() {
  70 + return duration;
  71 + }
  72 + }
  73 +
  74 + public static class Worker extends UntypedActor {
  75 +
  76 + private double calculatePiFor(int start, int nrOfElements) {
  77 + double acc = 0.0;
  78 + for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
  79 + acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
  80 + }
  81 + return acc;
  82 + }
  83 +
  84 + public void onReceive(Object message) {
  85 + if (message instanceof Work) {
  86 + Work work = (Work) message;
  87 + double result = calculatePiFor(work.getStart(), work.getNrOfElements());
  88 + getSender().tell(new Result(result), getSelf());
  89 + } else {
  90 + unhandled(message);
  91 + }
  92 + }
  93 + }
  94 +
  95 + public static class Master extends UntypedActor {
  96 + private final int nrOfMessages;
  97 + private final int nrOfElements;
  98 +
  99 + private double pi;
  100 + private int nrOfResults;
  101 + private final long start = System.currentTimeMillis();
  102 +
  103 + private final ActorRef listener;
  104 + private final ActorRef workerRouter;
  105 +
  106 + public Master(
  107 + final int nrOfWorkers,
  108 + int nrOfMessages,
  109 + int nrOfElements,
  110 + ActorRef listener) {
  111 +
  112 + this.nrOfMessages = nrOfMessages;
  113 + this.nrOfElements = nrOfElements;
  114 + this.listener = listener;
  115 +
  116 + workerRouter = this.getContext().actorOf(new Props(Worker.class).withRouter(
  117 + new RoundRobinRouter(nrOfWorkers)), "workerRouter");
  118 + }
  119 +
  120 + public void onReceive(Object message) {
  121 + if (message instanceof Calculate) {
  122 + for (int start = 0; start < nrOfMessages; start++) {
  123 + workerRouter.tell(new Work(start, nrOfElements), getSelf());
  124 + }
  125 + } else if (message instanceof Result) {
  126 + Result result = (Result) message;
  127 + pi += result.getValue();
  128 + nrOfResults += 1;
  129 + if (nrOfResults == nrOfMessages) {
  130 + // Send the result to the listener
  131 + Duration duration = Duration.create(System.currentTimeMillis() - start,
  132 + TimeUnit.MILLISECONDS);
  133 + listener.tell(new PiApproximation(pi, duration), getSelf());
  134 + // Stops this actor and all its supervised children
  135 + getContext().stop(getSelf());
  136 + }
  137 + } else {
  138 + unhandled(message);
  139 + }
  140 + }
  141 + }
  142 +
  143 + public static class Listener extends UntypedActor {
  144 + public void onReceive(Object message) {
  145 + if (message instanceof PiApproximation) {
  146 + PiApproximation approximation = (PiApproximation) message;
  147 + System.out.println(String.format("\n\tPi approximation: " +
  148 + "\t\t%s\n\tCalculation time: \t%s",
  149 + approximation.getPi(), approximation.getDuration()));
  150 + getContext().system().shutdown();
  151 + } else {
  152 + unhandled(message);
  153 + }
  154 + }
  155 + }
  156 +
  157 + public void calculate(
  158 + final int nrOfWorkers,
  159 + final int nrOfElements,
  160 + final int nrOfMessages) {
  161 +
  162 + // Create an Akka system
  163 + ActorSystem system = ActorSystem.create("PiSystem");
  164 +
  165 + // create the result listener, which will print the result and shutdown the system
  166 + final ActorRef listener = system.actorOf(new Props(Listener.class), "listener");
  167 +
  168 + // create the master
  169 + ActorRef master = system.actorOf(new Props(new UntypedActorFactory() {
  170 + public UntypedActor create() {
  171 + return new Master(nrOfWorkers, nrOfMessages, nrOfElements, listener);
  172 + }
  173 + }), "master");
  174 +
  175 + // start the calculation
  176 + master.tell(new Calculate());
  177 +
  178 + }
  179 +}

0 comments on commit d22414e

Please sign in to comment.
Something went wrong with that request. Please try again.