2012-01-05 17:59:19 +01:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2012-01-05 17:59:19 +01:00
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.jrouting;
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
import akka.routing.ScatterGatherFirstCompletedRouter;
|
|
|
|
|
import akka.routing.BroadcastRouter;
|
|
|
|
|
import akka.routing.RandomRouter;
|
|
|
|
|
import akka.routing.RoundRobinRouter;
|
2012-01-11 13:56:38 +01:00
|
|
|
import akka.routing.SmallestMailboxRouter;
|
2012-01-05 17:59:19 +01:00
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.Props;
|
2012-10-15 16:18:52 +02:00
|
|
|
import scala.concurrent.duration.Duration;
|
2012-01-05 17:59:19 +01:00
|
|
|
import akka.util.Timeout;
|
2012-07-04 15:25:30 +02:00
|
|
|
import scala.concurrent.Future;
|
2012-06-29 16:06:26 +02:00
|
|
|
import scala.concurrent.Await;
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
//#parentActor
|
|
|
|
|
public class ParentActor extends UntypedActor {
|
2012-02-16 12:31:49 +01:00
|
|
|
public void onReceive(Object msg) throws Exception {
|
2012-01-05 17:59:19 +01:00
|
|
|
if (msg.equals("rrr")) {
|
|
|
|
|
//#roundRobinRouter
|
|
|
|
|
ActorRef roundRobinRouter = getContext().actorOf(
|
2013-04-14 22:56:41 +02:00
|
|
|
Props.create(PrintlnActor.class).withRouter(new RoundRobinRouter(5)),
|
2012-09-26 10:56:25 +02:00
|
|
|
"router");
|
2012-01-05 17:59:19 +01:00
|
|
|
for (int i = 1; i <= 10; i++) {
|
|
|
|
|
roundRobinRouter.tell(i, getSelf());
|
|
|
|
|
}
|
|
|
|
|
//#roundRobinRouter
|
|
|
|
|
} else if (msg.equals("rr")) {
|
|
|
|
|
//#randomRouter
|
2012-09-26 10:56:25 +02:00
|
|
|
ActorRef randomRouter = getContext().actorOf(
|
2013-04-14 22:56:41 +02:00
|
|
|
Props.create(PrintlnActor.class).withRouter(new RandomRouter(5)),
|
2012-01-05 17:59:19 +01:00
|
|
|
"router");
|
|
|
|
|
for (int i = 1; i <= 10; i++) {
|
|
|
|
|
randomRouter.tell(i, getSelf());
|
|
|
|
|
}
|
|
|
|
|
//#randomRouter
|
2012-01-11 13:56:38 +01:00
|
|
|
} else if (msg.equals("smr")) {
|
|
|
|
|
//#smallestMailboxRouter
|
|
|
|
|
ActorRef smallestMailboxRouter = getContext().actorOf(
|
2013-04-14 22:56:41 +02:00
|
|
|
Props.create(PrintlnActor.class).withRouter(new SmallestMailboxRouter(5)),
|
2012-09-26 10:56:25 +02:00
|
|
|
"router");
|
2012-01-11 13:56:38 +01:00
|
|
|
for (int i = 1; i <= 10; i++) {
|
|
|
|
|
smallestMailboxRouter.tell(i, getSelf());
|
|
|
|
|
}
|
|
|
|
|
//#smallestMailboxRouter
|
2012-01-05 17:59:19 +01:00
|
|
|
} else if (msg.equals("br")) {
|
|
|
|
|
//#broadcastRouter
|
2012-09-26 10:56:25 +02:00
|
|
|
ActorRef broadcastRouter = getContext().actorOf(
|
2013-04-14 22:56:41 +02:00
|
|
|
Props.create(PrintlnActor.class).withRouter(new BroadcastRouter(5)), "router");
|
2012-01-05 17:59:19 +01:00
|
|
|
broadcastRouter.tell("this is a broadcast message", getSelf());
|
|
|
|
|
//#broadcastRouter
|
|
|
|
|
} else if (msg.equals("sgfcr")) {
|
|
|
|
|
//#scatterGatherFirstCompletedRouter
|
|
|
|
|
ActorRef scatterGatherFirstCompletedRouter = getContext().actorOf(
|
2013-04-14 22:56:41 +02:00
|
|
|
Props.create(FibonacciActor.class).withRouter(
|
2012-09-26 10:56:25 +02:00
|
|
|
new ScatterGatherFirstCompletedRouter(5, Duration.create(2, "seconds"))),
|
|
|
|
|
"router");
|
2012-09-14 10:08:40 +02:00
|
|
|
Timeout timeout = new Timeout(Duration.create(5, "seconds"));
|
2012-09-26 10:56:25 +02:00
|
|
|
Future<Object> futureResult = akka.pattern.Patterns.ask(
|
|
|
|
|
scatterGatherFirstCompletedRouter, new FibonacciActor.FibonacciNumber(10),
|
|
|
|
|
timeout);
|
2012-01-05 17:59:19 +01:00
|
|
|
int result = (Integer) Await.result(futureResult, timeout.duration());
|
|
|
|
|
//#scatterGatherFirstCompletedRouter
|
2012-09-26 10:56:25 +02:00
|
|
|
System.out.println(
|
|
|
|
|
String.format("The result of calculating Fibonacci for 10 is %d", result));
|
2012-01-05 17:59:19 +01:00
|
|
|
} else {
|
|
|
|
|
unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 01:47:48 +01:00
|
|
|
//#parentActor
|