diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/InternalMsg.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/InternalMsg.java new file mode 100644 index 0000000000..37cca0dd53 --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/InternalMsg.java @@ -0,0 +1,26 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.ActorRef; + +public class InternalMsg { + static class MathOpMsg { + private final ActorRef actor; + private final Op.MathOp mathOp; + + MathOpMsg(ActorRef actor, Op.MathOp mathOp) { + this.actor = actor; + this.mathOp = mathOp; + } + + public ActorRef getActor() { + return actor; + } + + public Op.MathOp getMathOp() { + return mathOp; + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java new file mode 100644 index 0000000000..36da1486c6 --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java @@ -0,0 +1,21 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.UntypedActor; + +public class JAdvancedCalculatorActor extends UntypedActor { + @Override + public void onReceive(Object message) throws Exception { + if (message instanceof Op.Multiply) { + Op.Multiply multiply = (Op.Multiply) message; + System.out.println("Calculating " + multiply.getN1() + " * " + multiply.getN2()); + getSender().tell(new Op.MultiplicationResult(multiply.getN1(), multiply.getN2(), multiply.getN1() * multiply.getN2())); + } else if (message instanceof Op.Divide) { + Op.Divide divide = (Op.Divide) message; + System.out.println("Calculating " + divide.getN1() + " / " + divide.getN2()); + getSender().tell(new Op.DivisionResult(divide.getN1(), divide.getN2(), divide.getN1() / divide.getN2())); + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalcApp.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalcApp.java new file mode 100644 index 0000000000..470c4d123a --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalcApp.java @@ -0,0 +1,13 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +public class JCalcApp { + + public static void main(String[] args) { + JCalculatorApplication app = new JCalculatorApplication(); + System.out.println("Started Calculator Application - waiting for messages"); + } + +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java new file mode 100644 index 0000000000..5144c90e0f --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java @@ -0,0 +1,28 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.ActorRef; +import akka.actor.ActorSystem; +import akka.actor.Props; +import akka.kernel.Bootable; +import com.typesafe.config.ConfigFactory; + +public class JCalculatorApplication implements Bootable { + private ActorSystem system; + + public JCalculatorApplication() { + system = ActorSystem.create("CalculatorApplication", ConfigFactory.load().getConfig("calculator")); + ActorRef actor = system.actorOf(new Props(JSimpleCalculatorActor.class), "simpleCalculator"); + } + + @Override + public void startup() { + } + + @Override + public void shutdown() { + system.shutdown(); + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java new file mode 100644 index 0000000000..dc303e55ca --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.UntypedActor; + +import java.text.DecimalFormat; +import java.text.NumberFormat; + +public class JCreationActor extends UntypedActor { + private static final NumberFormat formatter = new DecimalFormat("#0.00"); + + @Override + public void onReceive(Object message) throws Exception { + if (message instanceof InternalMsg.MathOpMsg) { + InternalMsg.MathOpMsg msg = (InternalMsg.MathOpMsg) message; + msg.getActor().tell(msg.getMathOp(), getSelf()); + } else if (message instanceof Op.MathResult) { + if (message instanceof Op.MultiplicationResult) { + Op.MultiplicationResult result = (Op.MultiplicationResult) message; + System.out.println("Mul result: " + result.getN1() + " * " + + result.getN2() + " = " + result.getResult()); + } else if (message instanceof Op.DivisionResult) { + Op.DivisionResult result = (Op.DivisionResult) message; + System.out.println("Div result: " + result.getN1() + " / " + + result.getN2() + " = " + formatter.format(result.getResult())); + } + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApp.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApp.java new file mode 100644 index 0000000000..67ac7f490a --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApp.java @@ -0,0 +1,26 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import java.util.Random; + +public class JCreationApp { + public static void main(String[] args) { + JCreationApplication app = new JCreationApplication(); + System.out.println("Started Creation Application"); + Random r = new Random(); + while (true) { + if (r.nextInt(100) % 2 == 0) { + app.doSomething(new Op.Multiply(r.nextInt(100), r.nextInt(100))); + } else { + app.doSomething(new Op.Divide(r.nextInt(10000), r.nextInt(99) + 1)); + } + + try { + Thread.sleep(200); + } catch (InterruptedException e) { + } + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java new file mode 100644 index 0000000000..82d5ed373f --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java @@ -0,0 +1,35 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.ActorRef; +import akka.actor.ActorSystem; +import akka.actor.Props; +import akka.kernel.Bootable; +import com.typesafe.config.ConfigFactory; + +public class JCreationApplication implements Bootable { + private ActorSystem system; + private ActorRef actor; + private ActorRef remoteActor; + + public JCreationApplication() { + system = ActorSystem.create("CreationApplication", ConfigFactory.load().getConfig("remotecreation")); + actor = system.actorOf(new Props(JCreationActor.class)); + remoteActor = system.actorOf(new Props(JAdvancedCalculatorActor.class), "advancedCalculator"); + } + + public void doSomething(Op.MathOp mathOp) { + actor.tell(new InternalMsg.MathOpMsg(remoteActor, mathOp)); + } + + @Override + public void startup() { + } + + @Override + public void shutdown() { + system.shutdown(); + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java new file mode 100644 index 0000000000..52a2f1d67c --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.UntypedActor; + +public class JLookupActor extends UntypedActor { + + @Override + public void onReceive(Object message) throws Exception { + if (message instanceof InternalMsg.MathOpMsg) { + InternalMsg.MathOpMsg msg = (InternalMsg.MathOpMsg) message; + msg.getActor().tell(msg.getMathOp(), getSelf()); + } else if (message instanceof Op.MathResult) { + if (message instanceof Op.AddResult) { + Op.AddResult result = (Op.AddResult) message; + System.out.println("Add result: " + result.getN1() + " + " + + result.getN2() + " = " + result.getResult()); + } else if (message instanceof Op.SubtractResult) { + Op.SubtractResult result = (Op.SubtractResult) message; + System.out.println("Sub result: " + result.getN1() + " - " + + result.getN2() + " = " + result.getResult()); + } + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApp.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApp.java new file mode 100644 index 0000000000..a854a1916d --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApp.java @@ -0,0 +1,26 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import java.util.Random; + +public class JLookupApp { + public static void main(String[] args) { + JLookupApplication app = new JLookupApplication(); + System.out.println("Started Lookup Application"); + Random r = new Random(); + while (true) { + if (r.nextInt(100) % 2 == 0) { + app.doSomething(new Op.Add(r.nextInt(100), r.nextInt(100))); + } else { + app.doSomething(new Op.Subtract(r.nextInt(100), r.nextInt(100))); + } + + try { + Thread.sleep(200); + } catch (InterruptedException e) { + } + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java new file mode 100644 index 0000000000..f70fda5baa --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java @@ -0,0 +1,35 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.ActorRef; +import akka.actor.ActorSystem; +import akka.actor.Props; +import akka.kernel.Bootable; +import com.typesafe.config.ConfigFactory; + +public class JLookupApplication implements Bootable { + private ActorSystem system; + private ActorRef actor; + private ActorRef remoteActor; + + public JLookupApplication() { + system = ActorSystem.create("LookupApplication", ConfigFactory.load().getConfig("remotelookup")); + actor = system.actorOf(new Props(JLookupActor.class)); + remoteActor = system.actorFor("akka://CalculatorApplication@127.0.0.1:2552/user/simpleCalculator"); + } + + public void doSomething(Op.MathOp mathOp) { + actor.tell(new InternalMsg.MathOpMsg(remoteActor, mathOp)); + } + + @Override + public void startup() { + } + + @Override + public void shutdown() { + system.shutdown(); + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java new file mode 100644 index 0000000000..738e4c7ae5 --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java @@ -0,0 +1,21 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import akka.actor.UntypedActor; + +public class JSimpleCalculatorActor extends UntypedActor { + @Override + public void onReceive(Object message) { + if (message instanceof Op.Add) { + Op.Add add = (Op.Add) message; + System.out.println("Calculating " + add.getN1() + " + " + add.getN2()); + getSender().tell(new Op.AddResult(add.getN1(), add.getN2(), add.getN1() + add.getN2())); + } else if (message instanceof Op.Subtract) { + Op.Subtract subtract = (Op.Subtract) message; + System.out.println("Calculating " + subtract.getN1() + " - " + subtract.getN2()); + getSender().tell(new Op.SubtractResult(subtract.getN1(), subtract.getN2(), subtract.getN1() - subtract.getN2())); + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/Op.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/Op.java new file mode 100644 index 0000000000..1c461b0405 --- /dev/null +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/Op.java @@ -0,0 +1,181 @@ +/** + * Copyright (C) 2009-2011 Typesafe Inc. + */ +package sample.remote.calculator.java; + +import java.io.Serializable; + +public class Op { + + public interface MathOp extends Serializable {} + + public interface MathResult extends Serializable {} + + static class Add implements MathOp { + private final int n1; + private final int n2; + + public Add(int n1, int n2) { + this.n1 = n1; + this.n2 = n2; + } + + public int getN1() { + return n1; + } + + public int getN2() { + return n2; + } + } + + static class AddResult implements MathResult { + private final int n1; + private final int n2; + private final int result; + + public AddResult(int n1, int n2, int result) { + this.n1 = n1; + this.n2 = n2; + this.result = result; + } + + public int getN1() { + return n1; + } + + public int getN2() { + return n2; + } + + public int getResult() { + return result; + } + } + + static class Subtract implements MathOp { + private final int n1; + private final int n2; + + public Subtract(int n1, int n2) { + this.n1 = n1; + this.n2 = n2; + } + + public int getN1() { + return n1; + } + + public int getN2() { + return n2; + } + } + + static class SubtractResult implements MathResult { + private final int n1; + private final int n2; + private final int result; + + public SubtractResult(int n1, int n2, int result) { + this.n1 = n1; + this.n2 = n2; + this.result = result; + } + + public int getN1() { + return n1; + } + + public int getN2() { + return n2; + } + + public int getResult() { + return result; + } + } + + static class Multiply implements MathOp { + private final int n1; + private final int n2; + + public Multiply(int n1, int n2) { + this.n1 = n1; + this.n2 = n2; + } + + public int getN1() { + return n1; + } + + public int getN2() { + return n2; + } + } + + static class MultiplicationResult implements MathResult { + private final int n1; + private final int n2; + private final int result; + + public MultiplicationResult(int n1, int n2, int result) { + this.n1 = n1; + this.n2 = n2; + this.result = result; + } + + public int getN1() { + return n1; + } + + public int getN2() { + return n2; + } + + public int getResult() { + return result; + } + } + + static class Divide implements MathOp { + private final double n1; + private final int n2; + + public Divide(double n1, int n2) { + this.n1 = n1; + this.n2 = n2; + } + + public double getN1() { + return n1; + } + + public int getN2() { + return n2; + } + } + + static class DivisionResult implements MathResult { + private final double n1; + private final int n2; + private final double result; + + public DivisionResult(double n1, int n2, double result) { + this.n1 = n1; + this.n2 = n2; + this.result = result; + } + + public double getN1() { + return n1; + } + + public int getN2() { + return n2; + } + + public double getResult() { + return result; + } + } +} diff --git a/akka-samples/akka-sample-remote/src/main/scala/sample/remote/calculator/MathOp.scala b/akka-samples/akka-sample-remote/src/main/scala/sample/remote/calculator/MathOp.scala index 5dde9625a9..f2a718cbdc 100644 --- a/akka-samples/akka-sample-remote/src/main/scala/sample/remote/calculator/MathOp.scala +++ b/akka-samples/akka-sample-remote/src/main/scala/sample/remote/calculator/MathOp.scala @@ -13,7 +13,7 @@ case class Subtract(nbr1: Int, nbr2: Int) extends MathOp case class Multiply(nbr1: Int, nbr2: Int) extends MathOp -case class Divide(nbr1: Int, nbr2: Int) extends MathOp +case class Divide(nbr1: Double, nbr2: Int) extends MathOp trait MathResult @@ -31,7 +31,7 @@ class AdvancedCalculatorActor extends Actor { println("Calculating %d * %d".format(n1, n2)) sender ! MultiplicationResult(n1, n2, n1 * n2) case Divide(n1, n2) ⇒ - println("Calculating %d / %d".format(n1, n2)) + println("Calculating %.0f / %d".format(n1, n2)) sender ! DivisionResult(n1, n2, n1 / n2) } } \ No newline at end of file