Merge branch 'master' of github.com:jboner/akka
This commit is contained in:
commit
ea150eff61
13 changed files with 472 additions and 2 deletions
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue