2012-01-05 17:59:19 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.jrouting;
|
2012-01-05 17:59:19 +01:00
|
|
|
|
2012-09-19 23:55:53 +02:00
|
|
|
import static akka.pattern.Patterns.ask;
|
|
|
|
|
import static docs.jrouting.CustomRouterDocTestBase.Message.DemocratCountResult;
|
|
|
|
|
import static docs.jrouting.CustomRouterDocTestBase.Message.DemocratVote;
|
|
|
|
|
import static docs.jrouting.CustomRouterDocTestBase.Message.RepublicanCountResult;
|
|
|
|
|
import static docs.jrouting.CustomRouterDocTestBase.Message.RepublicanVote;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
2012-01-05 17:59:19 +01:00
|
|
|
import java.util.Arrays;
|
2012-09-19 23:55:53 +02:00
|
|
|
import java.util.List;
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2012-06-29 16:06:26 +02:00
|
|
|
import scala.concurrent.Await;
|
2012-07-04 15:25:30 +02:00
|
|
|
import scala.concurrent.Future;
|
2012-09-19 23:55:53 +02:00
|
|
|
import scala.concurrent.util.Duration;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.OneForOneStrategy;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.actor.SupervisorStrategy;
|
|
|
|
|
import akka.actor.UntypedActor;
|
2012-02-10 14:13:40 +01:00
|
|
|
import akka.dispatch.Dispatchers;
|
2012-09-19 23:55:53 +02:00
|
|
|
import akka.routing.CustomRoute;
|
|
|
|
|
import akka.routing.CustomRouterConfig;
|
|
|
|
|
import akka.routing.Destination;
|
|
|
|
|
import akka.routing.RoundRobinRouter;
|
|
|
|
|
import akka.routing.RouteeProvider;
|
2012-01-05 17:59:19 +01:00
|
|
|
import akka.testkit.AkkaSpec;
|
2012-09-19 23:55:53 +02:00
|
|
|
import akka.util.Timeout;
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
public class CustomRouterDocTestBase {
|
|
|
|
|
|
|
|
|
|
ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() {
|
|
|
|
|
system = ActorSystem.create("MySystem", AkkaSpec.testConf());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() {
|
|
|
|
|
system.shutdown();
|
|
|
|
|
}
|
2012-02-10 14:13:40 +01:00
|
|
|
|
|
|
|
|
public static class MyActor extends UntypedActor {
|
|
|
|
|
@Override public void onReceive(Object o) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateDispatchers() {
|
|
|
|
|
//#dispatchers
|
|
|
|
|
final ActorRef router = system.actorOf(new Props(MyActor.class)
|
2012-10-01 20:35:19 +02:00
|
|
|
// “head” router will run on "head" dispatcher
|
2012-09-26 10:56:25 +02:00
|
|
|
.withRouter(new RoundRobinRouter(5).withDispatcher("head"))
|
2012-10-01 20:35:19 +02:00
|
|
|
// MyActor “workers” will run on "workers" dispatcher
|
2012-09-26 10:56:25 +02:00
|
|
|
.withDispatcher("workers"));
|
2012-02-10 14:13:40 +01:00
|
|
|
//#dispatchers
|
|
|
|
|
}
|
2012-02-18 22:15:39 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateSupervisor() {
|
|
|
|
|
//#supervision
|
2012-09-26 10:56:25 +02:00
|
|
|
final SupervisorStrategy strategy =
|
|
|
|
|
new OneForOneStrategy(5, Duration.parse("1 minute"),
|
2012-02-19 10:12:20 +01:00
|
|
|
new Class<?>[] { Exception.class });
|
2012-02-18 22:15:39 +01:00
|
|
|
final ActorRef router = system.actorOf(new Props(MyActor.class)
|
|
|
|
|
.withRouter(new RoundRobinRouter(5).withSupervisorStrategy(strategy)));
|
|
|
|
|
//#supervision
|
|
|
|
|
}
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
//#crTest
|
|
|
|
|
@Test
|
2012-02-16 12:31:49 +01:00
|
|
|
public void countVotesAsIntendedNotAsInFlorida() throws Exception {
|
2012-09-26 10:56:25 +02:00
|
|
|
ActorRef routedActor = system.actorOf(
|
|
|
|
|
new Props().withRouter(new VoteCountRouter()));
|
2012-09-19 23:55:53 +02:00
|
|
|
routedActor.tell(DemocratVote, null);
|
|
|
|
|
routedActor.tell(DemocratVote, null);
|
|
|
|
|
routedActor.tell(RepublicanVote, null);
|
|
|
|
|
routedActor.tell(DemocratVote, null);
|
|
|
|
|
routedActor.tell(RepublicanVote, null);
|
2012-09-14 10:08:40 +02:00
|
|
|
Timeout timeout = new Timeout(Duration.create(1, "seconds"));
|
2012-09-26 10:56:25 +02:00
|
|
|
Future<Object> democratsResult =
|
|
|
|
|
ask(routedActor, DemocratCountResult, timeout);
|
|
|
|
|
Future<Object> republicansResult =
|
|
|
|
|
ask(routedActor, RepublicanCountResult, timeout);
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
assertEquals(3, Await.result(democratsResult, timeout.duration()));
|
|
|
|
|
assertEquals(2, Await.result(republicansResult, timeout.duration()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#crTest
|
|
|
|
|
|
|
|
|
|
//#CustomRouter
|
|
|
|
|
//#crMessages
|
|
|
|
|
enum Message {
|
|
|
|
|
DemocratVote, DemocratCountResult, RepublicanVote, RepublicanCountResult
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#crMessages
|
2012-09-26 10:56:25 +02:00
|
|
|
//#CustomRouter
|
|
|
|
|
static
|
|
|
|
|
//#CustomRouter
|
2012-01-05 17:59:19 +01:00
|
|
|
//#crActors
|
2012-09-26 10:56:25 +02:00
|
|
|
public class DemocratActor extends UntypedActor {
|
2012-01-05 17:59:19 +01:00
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
switch ((Message) msg) {
|
|
|
|
|
case DemocratVote:
|
|
|
|
|
counter++;
|
|
|
|
|
break;
|
|
|
|
|
case DemocratCountResult:
|
|
|
|
|
getSender().tell(counter, getSelf());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
//#crActors
|
|
|
|
|
//#CustomRouter
|
|
|
|
|
static
|
|
|
|
|
//#CustomRouter
|
|
|
|
|
//#crActors
|
|
|
|
|
public class RepublicanActor extends UntypedActor {
|
2012-01-05 17:59:19 +01:00
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
switch ((Message) msg) {
|
|
|
|
|
case RepublicanVote:
|
|
|
|
|
counter++;
|
|
|
|
|
break;
|
|
|
|
|
case RepublicanCountResult:
|
|
|
|
|
getSender().tell(counter, getSelf());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#crActors
|
2012-09-26 10:56:25 +02:00
|
|
|
//#CustomRouter
|
|
|
|
|
static
|
|
|
|
|
//#CustomRouter
|
2012-01-05 17:59:19 +01:00
|
|
|
//#crRouter
|
2012-09-26 10:56:25 +02:00
|
|
|
public class VoteCountRouter extends CustomRouterConfig {
|
2012-02-10 14:13:40 +01:00
|
|
|
|
|
|
|
|
@Override public String routerDispatcher() {
|
|
|
|
|
return Dispatchers.DefaultDispatcherId();
|
|
|
|
|
}
|
2012-02-18 22:15:39 +01:00
|
|
|
|
|
|
|
|
@Override public SupervisorStrategy supervisorStrategy() {
|
|
|
|
|
return SupervisorStrategy.defaultStrategy();
|
|
|
|
|
}
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
//#crRoute
|
|
|
|
|
@Override
|
2012-08-30 12:48:11 +02:00
|
|
|
public CustomRoute createCustomRoute(RouteeProvider routeeProvider) {
|
2012-09-26 10:56:25 +02:00
|
|
|
final ActorRef democratActor =
|
|
|
|
|
routeeProvider.context().actorOf(new Props(DemocratActor.class), "d");
|
|
|
|
|
final ActorRef republicanActor =
|
|
|
|
|
routeeProvider.context().actorOf(new Props(RepublicanActor.class), "r");
|
|
|
|
|
List<ActorRef> routees =
|
|
|
|
|
Arrays.asList(new ActorRef[] { democratActor, republicanActor });
|
2012-01-05 17:59:19 +01:00
|
|
|
|
|
|
|
|
//#crRegisterRoutees
|
2012-01-17 08:45:07 +01:00
|
|
|
routeeProvider.registerRoutees(routees);
|
2012-01-05 17:59:19 +01:00
|
|
|
//#crRegisterRoutees
|
|
|
|
|
|
|
|
|
|
//#crRoutingLogic
|
|
|
|
|
return new CustomRoute() {
|
|
|
|
|
@Override
|
|
|
|
|
public Iterable<Destination> destinationsFor(ActorRef sender, Object msg) {
|
|
|
|
|
switch ((Message) msg) {
|
|
|
|
|
case DemocratVote:
|
|
|
|
|
case DemocratCountResult:
|
2012-09-26 10:56:25 +02:00
|
|
|
return Arrays.asList(
|
|
|
|
|
new Destination[] { new Destination(sender, democratActor) });
|
2012-01-05 17:59:19 +01:00
|
|
|
case RepublicanVote:
|
|
|
|
|
case RepublicanCountResult:
|
2012-09-26 10:56:25 +02:00
|
|
|
return Arrays.asList(
|
|
|
|
|
new Destination[] { new Destination(sender, republicanActor) });
|
2012-01-05 17:59:19 +01:00
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown message: " + msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
//#crRoutingLogic
|
|
|
|
|
}
|
|
|
|
|
//#crRoute
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#crRouter
|
|
|
|
|
//#CustomRouter
|
|
|
|
|
}
|