Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit d22414e24a014d8869c50b865335f28fcf3a1b1d 1 parent f1949f5
@bantonsson bantonsson authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+target/
+project/project/
View
55 README.md
@@ -1,4 +1,57 @@
-A [giter8] [Akka] 2.0 tutorial project using [Java] and [Maven].
+A [giter8] [Akka] 2.0 tutorial project using [Java] and [Maven].
+
+Prerequisites to use this tutorial is to have [Java], [Maven] and [giter8] installed.
+
+The next step is to open a terminal window and:
+
+ $ cd /Users/theuser/code (or where ever you would like your code to end up)
+ $ g8 typesafehub/akka-first-tutorial-java
+
+ Akka 2.0 Pi Calculation Tutorial with Java and Maven
+
+ name [Akka Pi Calculation Tutorial]:
+ group_id [org.example]:
+ artifact_id [pi-calculation]:
+ package [org.example]:
+ version [0.1-SNAPSHOT]:
+ akka_version [2.0]:
+
+ Applied typesafehub/akka-first-tutorial-java.g8 in akka-pi-calculation-tutorial
+
+Okay, so now you have created the tutorial as a local project on your computer.
+To run and test it use [Maven]:
+
+ $ cd akka-pi-calculation-tutorial
+ $ mvn compile exec:java
+ [INFO] Scanning for projects...
+ [INFO]
+ [INFO] ------------------------------------------------------------------------
+ [INFO] Building Akka Pi Calculation Tutorial 0.1-SNAPSHOT
+ [INFO] ------------------------------------------------------------------------
+ [INFO]
+ [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ pi-calculation ---
+ [debug] execute contextualize
+ [INFO] skip non existing resourceDirectory /Users/theuser/code/akka-pi-calculation-tutorial/src/main/resources
+ [INFO]
+ [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ pi-calculation ---
+ [WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
+ [INFO] Compiling 1 source file to /Users/theuser/code/akka-pi-calculation-tutorial/target/classes
+ [INFO]
+ [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ pi-calculation >>>
+ [INFO]
+ [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ pi-calculation <<<
+ [INFO]
+ [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ pi-calculation ---
+ Pi approximation: 3.1415926435897883
+ Calculation time: 565 milliseconds
+ [INFO] ------------------------------------------------------------------------
+ [INFO] BUILD SUCCESS
+ [INFO] ------------------------------------------------------------------------
+ [INFO] Total time: 3.027s
+ [INFO] Finished at: Thu Mar 08 21:03:48 CET 2012
+ [INFO] Final Memory: 9M/81M
+ [INFO] ------------------------------------------------------------------------
+
[giter8]: https://github.com/n8han/giter8
[Akka]: http://akka.io
View
1  build.sbt
@@ -0,0 +1 @@
+seq(giter8Settings :_*)
View
1  project/build.properties
@@ -0,0 +1 @@
+sbt.version=0.11.2
View
1  project/plugins.sbt
@@ -0,0 +1 @@
+addSbtPlugin("net.databinder" % "giter8-plugin" % "0.3.2")
View
8 src/main/g8/default.properties
@@ -0,0 +1,8 @@
+description=Akka 2.0 Pi Calculation Tutorial with Java and Maven
+
+name=Akka Pi Calculation Tutorial
+group_id=org.example
+artifact_id=pi-calculation
+package=org.example
+version=0.1-SNAPSHOT
+akka_version=2.0
View
43 src/main/g8/pom.xml
@@ -0,0 +1,43 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+ http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>$group_id$</groupId>
+ <artifactId>$artifact_id$</artifactId>
+ <version>$version$</version>
+ <name>$name$</name>
+
+ <properties>
+ <akka.version>$akka_version$</akka.version>
+ </properties>
+
+ <repositories>
+ <repository>
+ <id>typesafe-releases</id>
+ <url>http://repo.typesafe.com/typesafe/releases</url>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.typesafe.akka</groupId>
+ <artifactId>akka-actor</artifactId>
+ <version>\${akka.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>1.2.1</version>
+ <configuration>
+ <mainClass>$package$.Pi</mainClass>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
View
179 src/main/g8/src/main/java/$package$/Pi.java
@@ -0,0 +1,179 @@
+/**
+ * Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
+ */
+
+package $package$;
+
+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());
+
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.