=doc #3689 Make activator templates for remote samples

This commit is contained in:
Patrik Nordwall 2013-11-26 09:17:50 +01:00
parent 23dd957ba2
commit b82698a354
53 changed files with 1163 additions and 1062 deletions

View file

@ -0,0 +1,48 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.remote.calculator;
import akka.actor.UntypedActor;
public class CalculatorActor 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());
Op.AddResult result = new Op.AddResult(add.getN1(), add.getN2(),
add.getN1() + add.getN2());
getSender().tell(result, getSelf());
} else if (message instanceof Op.Subtract) {
Op.Subtract subtract = (Op.Subtract) message;
System.out.println("Calculating " + subtract.getN1() + " - "
+ subtract.getN2());
Op.SubtractResult result = new Op.SubtractResult(subtract.getN1(),
subtract.getN2(), subtract.getN1() - subtract.getN2());
getSender().tell(result, getSelf());
} else if (message instanceof Op.Multiply) {
Op.Multiply multiply = (Op.Multiply) message;
System.out.println("Calculating " + multiply.getN1() + " * "
+ multiply.getN2());
Op.MultiplicationResult result = new Op.MultiplicationResult(
multiply.getN1(), multiply.getN2(), multiply.getN1()
* multiply.getN2());
getSender().tell(result, getSelf());
} else if (message instanceof Op.Divide) {
Op.Divide divide = (Op.Divide) message;
System.out.println("Calculating " + divide.getN1() + " / "
+ divide.getN2());
Op.DivisionResult result = new Op.DivisionResult(divide.getN1(),
divide.getN2(), divide.getN1() / divide.getN2());
getSender().tell(result, getSelf());
} else {
unhandled(message);
}
}
}

View file

@ -0,0 +1,36 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.remote.calculator;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
public class CreationActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof Op.MathOp) {
ActorRef calculator = getContext().actorOf(
Props.create(CalculatorActor.class));
calculator.tell(message, getSelf());
} else if (message instanceof Op.MultiplicationResult) {
Op.MultiplicationResult result = (Op.MultiplicationResult) message;
System.out.printf("Mul result: %d * %d = %d\n", result.getN1(),
result.getN2(), result.getResult());
getContext().stop(getSender());
} else if (message instanceof Op.DivisionResult) {
Op.DivisionResult result = (Op.DivisionResult) message;
System.out.printf("Div result: %.0f / %d = %.2f\n", result.getN1(),
result.getN2(), result.getResult());
getContext().stop(getSender());
} else {
unhandled(message);
}
}
}

View file

@ -0,0 +1,51 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.remote.calculator;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Random;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;
public class CreationApplication {
public static void main(String[] args) {
if (args.length == 0 || args[0].equals("CalculatorWorker"))
startRemoteWorkerSystem();
if (args.length == 0 || args[0].equals("Creation"))
startRemoteCreationSystem();
}
public static void startRemoteWorkerSystem() {
ActorSystem.create("CalculatorWorkerSystem",
ConfigFactory.load(("calculator")));
System.out.println("Started CalculatorWorkerSystem");
}
public static void startRemoteCreationSystem() {
final ActorSystem system = ActorSystem.create("CreationSystem",
ConfigFactory.load("remotecreation"));
final ActorRef actor = system.actorOf(Props.create(CreationActor.class),
"creationActor");
System.out.println("Started CreationSystem");
final Random r = new Random();
system.scheduler().schedule(Duration.create(1, SECONDS),
Duration.create(1, SECONDS), new Runnable() {
@Override
public void run() {
if (r.nextInt(100) % 2 == 0) {
actor.tell(new Op.Multiply(r.nextInt(100), r.nextInt(100)), null);
} else {
actor.tell(new Op.Divide(r.nextInt(10000), r.nextInt(99) + 1),
null);
}
}
}, system.dispatcher());
}
}

View file

@ -0,0 +1,86 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.remote.calculator;
import static java.util.concurrent.TimeUnit.SECONDS;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.ActorIdentity;
import akka.actor.Identify;
import akka.actor.Terminated;
import akka.actor.UntypedActor;
import akka.actor.ReceiveTimeout;
import akka.japi.Procedure;
public class LookupActor extends UntypedActor {
private final String path;
private ActorRef calculator = null;
public LookupActor(String path) {
this.path = path;
sendIdentifyRequest();
}
private void sendIdentifyRequest() {
getContext().actorSelection(path).tell(new Identify(path), getSelf());
getContext()
.system()
.scheduler()
.scheduleOnce(Duration.create(3, SECONDS), getSelf(),
ReceiveTimeout.getInstance(), getContext().dispatcher(), getSelf());
}
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof ActorIdentity) {
calculator = ((ActorIdentity) message).getRef();
if (calculator == null) {
System.out.println("Remote actor not available: " + path);
} else {
getContext().watch(calculator);
getContext().become(active, true);
}
} else if (message instanceof ReceiveTimeout) {
sendIdentifyRequest();
} else {
System.out.println("Not ready yet");
}
}
Procedure<Object> active = new Procedure<Object>() {
@Override
public void apply(Object message) {
if (message instanceof Op.MathOp) {
// send message to server actor
calculator.tell(message, getSelf());
} else if (message instanceof Op.AddResult) {
Op.AddResult result = (Op.AddResult) message;
System.out.printf("Add result: %d + %d = %d\n", result.getN1(),
result.getN2(), result.getResult());
} else if (message instanceof Op.SubtractResult) {
Op.SubtractResult result = (Op.SubtractResult) message;
System.out.printf("Sub result: %d - %d = %d\n", result.getN1(),
result.getN2(), result.getResult());
} else if (message instanceof Terminated) {
System.out.println("Calculator terminated");
sendIdentifyRequest();
getContext().unbecome();
} else if (message instanceof ReceiveTimeout) {
// ignore
} else {
unhandled(message);
}
}
};
}

View file

@ -0,0 +1,53 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.remote.calculator;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Random;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;
public class LookupApplication {
public static void main(String[] args) {
if (args.length == 0 || args[0].equals("Calculator"))
startRemoteCalculatorSystem();
if (args.length == 0 || args[0].equals("Lookup"))
startRemoteLookupSystem();
}
public static void startRemoteCalculatorSystem() {
final ActorSystem system = ActorSystem.create("CalculatorSystem",
ConfigFactory.load(("calculator")));
system.actorOf(Props.create(CalculatorActor.class), "calculator");
System.out.println("Started CalculatorSystem");
}
public static void startRemoteLookupSystem() {
final ActorSystem system = ActorSystem.create("LookupSystem",
ConfigFactory.load("remotelookup"));
final String path = "akka.tcp://CalculatorSystem@127.0.0.1:2552/user/calculator";
final ActorRef actor = system.actorOf(
Props.create(LookupActor.class, path), "lookupActor");
System.out.println("Started LookupSystem");
final Random r = new Random();
system.scheduler().schedule(Duration.create(1, SECONDS),
Duration.create(1, SECONDS), new Runnable() {
@Override
public void run() {
if (r.nextInt(100) % 2 == 0) {
actor.tell(new Op.Add(r.nextInt(100), r.nextInt(100)), null);
} else {
actor.tell(new Op.Subtract(r.nextInt(100), r.nextInt(100)), null);
}
}
}, system.dispatcher());
}
}

View file

@ -0,0 +1,191 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.remote.calculator;
import java.io.Serializable;
public class Op {
public interface MathOp extends Serializable {
}
public interface MathResult extends Serializable {
}
static class Add implements MathOp {
private static final long serialVersionUID = 1L;
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 static final long serialVersionUID = 1L;
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 static final long serialVersionUID = 1L;
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 static final long serialVersionUID = 1L;
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 static final long serialVersionUID = 1L;
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 static final long serialVersionUID = 1L;
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 static final long serialVersionUID = 1L;
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 static final long serialVersionUID = 1L;
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;
}
}
}