move code to src/test
* so that it compiles and tests pass * fix some additional snip references in getting started
This commit is contained in:
parent
413df8e0f4
commit
59f53e1a22
289 changed files with 45 additions and 45 deletions
|
|
@ -0,0 +1,150 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.routing;
|
||||
|
||||
import akka.testkit.AkkaJUnitActorSystemResource;
|
||||
|
||||
import jdocs.AbstractJavaTest;
|
||||
import akka.testkit.javadsl.TestKit;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
|
||||
//#imports1
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.routing.ConsistentHashingRouter.ConsistentHashable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.io.Serializable;
|
||||
//#imports1
|
||||
|
||||
|
||||
//#imports2
|
||||
import akka.actor.Props;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.routing.ConsistentHashingPool;
|
||||
import akka.routing.ConsistentHashingRouter.ConsistentHashMapper;
|
||||
import akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope;
|
||||
//#imports2
|
||||
|
||||
public class ConsistentHashingRouterDocTest extends AbstractJavaTest {
|
||||
|
||||
@ClassRule
|
||||
public static AkkaJUnitActorSystemResource actorSystemResource =
|
||||
new AkkaJUnitActorSystemResource("ConsistentHashingRouterDocTest");
|
||||
|
||||
private final ActorSystem system = actorSystemResource.getSystem();
|
||||
|
||||
static
|
||||
//#cache-actor
|
||||
public class Cache extends AbstractActor {
|
||||
Map<String, String> cache = new HashMap<String, String>();
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Entry.class, entry -> {
|
||||
cache.put(entry.key, entry.value);
|
||||
})
|
||||
.match(Get.class, get -> {
|
||||
Object value = cache.get(get.key);
|
||||
getSender().tell(value == null ? NOT_FOUND : value, getSelf());
|
||||
})
|
||||
.match(Evict.class, evict -> {
|
||||
cache.remove(evict.key);
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
//#cache-actor
|
||||
static
|
||||
//#cache-actor
|
||||
public final class Evict implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final String key;
|
||||
public Evict(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
}
|
||||
|
||||
//#cache-actor
|
||||
static
|
||||
//#cache-actor
|
||||
public final class Get implements Serializable, ConsistentHashable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final String key;
|
||||
public Get(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
public Object consistentHashKey() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
//#cache-actor
|
||||
static
|
||||
//#cache-actor
|
||||
public final class Entry implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final String key;
|
||||
public final String value;
|
||||
public Entry(String key, String value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
//#cache-actor
|
||||
static
|
||||
//#cache-actor
|
||||
public final String NOT_FOUND = "NOT_FOUND";
|
||||
//#cache-actor
|
||||
|
||||
|
||||
@Test
|
||||
public void demonstrateUsageOfConsistentHashableRouter() {
|
||||
|
||||
new TestKit(system) {{
|
||||
|
||||
//#consistent-hashing-router
|
||||
|
||||
final ConsistentHashMapper hashMapper = new ConsistentHashMapper() {
|
||||
@Override
|
||||
public Object hashKey(Object message) {
|
||||
if (message instanceof Evict) {
|
||||
return ((Evict) message).key;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ActorRef cache = system.actorOf(
|
||||
new ConsistentHashingPool(10).withHashMapper(hashMapper).props(
|
||||
Props.create(Cache.class)),
|
||||
"cache");
|
||||
|
||||
cache.tell(new ConsistentHashableEnvelope(
|
||||
new Entry("hello", "HELLO"), "hello"), getRef());
|
||||
cache.tell(new ConsistentHashableEnvelope(
|
||||
new Entry("hi", "HI"), "hi"), getRef());
|
||||
|
||||
cache.tell(new Get("hello"), getRef());
|
||||
expectMsgEquals("HELLO");
|
||||
|
||||
cache.tell(new Get("hi"), getRef());
|
||||
expectMsgEquals("HI");
|
||||
|
||||
cache.tell(new Evict("hi"), getRef());
|
||||
cache.tell(new Get("hi"), getRef());
|
||||
expectMsgEquals(NOT_FOUND);
|
||||
|
||||
//#consistent-hashing-router
|
||||
}};
|
||||
}
|
||||
|
||||
}
|
||||
170
akka-docs/src/test/java/jdocs/routing/CustomRouterDocTest.java
Normal file
170
akka-docs/src/test/java/jdocs/routing/CustomRouterDocTest.java
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.routing;
|
||||
|
||||
import akka.routing.FromConfig;
|
||||
import akka.routing.RoundRobinRoutingLogic;
|
||||
import akka.routing.Routee;
|
||||
import akka.routing.RoutingLogic;
|
||||
import akka.routing.SeveralRoutees;
|
||||
import akka.testkit.AkkaJUnitActorSystemResource;
|
||||
|
||||
import jdocs.AbstractJavaTest;
|
||||
import akka.testkit.javadsl.TestKit;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
|
||||
import scala.collection.immutable.IndexedSeq;
|
||||
import static akka.japi.Util.immutableIndexedSeq;
|
||||
|
||||
import docs.routing.CustomRouterDocSpec;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
//#imports1
|
||||
import akka.actor.AbstractActor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//#imports1
|
||||
|
||||
|
||||
public class CustomRouterDocTest extends AbstractJavaTest {
|
||||
|
||||
@ClassRule
|
||||
public static AkkaJUnitActorSystemResource actorSystemResource =
|
||||
new AkkaJUnitActorSystemResource("CustomRouterDocTest",
|
||||
ConfigFactory.parseString(CustomRouterDocSpec.jconfig()));
|
||||
|
||||
private final ActorSystem system = actorSystemResource.getSystem();
|
||||
|
||||
static
|
||||
//#routing-logic
|
||||
public class RedundancyRoutingLogic implements RoutingLogic {
|
||||
private final int nbrCopies;
|
||||
|
||||
public RedundancyRoutingLogic(int nbrCopies) {
|
||||
this.nbrCopies = nbrCopies;
|
||||
}
|
||||
RoundRobinRoutingLogic roundRobin = new RoundRobinRoutingLogic();
|
||||
|
||||
@Override
|
||||
public Routee select(Object message, IndexedSeq<Routee> routees) {
|
||||
List<Routee> targets = new ArrayList<Routee>();
|
||||
for (int i = 0; i < nbrCopies; i++) {
|
||||
targets.add(roundRobin.select(message, routees));
|
||||
}
|
||||
return new SeveralRoutees(targets);
|
||||
}
|
||||
}
|
||||
//#routing-logic
|
||||
|
||||
static
|
||||
//#unit-test-logic
|
||||
public final class TestRoutee implements Routee {
|
||||
public final int n;
|
||||
|
||||
public TestRoutee(int n) {
|
||||
this.n = n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Object message, ActorRef sender) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof TestRoutee) &&
|
||||
n == ((TestRoutee) obj).n;
|
||||
}
|
||||
}
|
||||
|
||||
//#unit-test-logic
|
||||
|
||||
static public class Storage extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> {
|
||||
getSender().tell(message, getSelf());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unitTestRoutingLogic() {
|
||||
//#unit-test-logic
|
||||
RedundancyRoutingLogic logic = new RedundancyRoutingLogic(3);
|
||||
|
||||
List<Routee> routeeList = new ArrayList<Routee>();
|
||||
for (int n = 1; n <= 7; n++) {
|
||||
routeeList.add(new TestRoutee(n));
|
||||
}
|
||||
IndexedSeq<Routee> routees = immutableIndexedSeq(routeeList);
|
||||
|
||||
SeveralRoutees r1 = (SeveralRoutees) logic.select("msg", routees);
|
||||
assertEquals(r1.getRoutees().get(0), routeeList.get(0));
|
||||
assertEquals(r1.getRoutees().get(1), routeeList.get(1));
|
||||
assertEquals(r1.getRoutees().get(2), routeeList.get(2));
|
||||
|
||||
SeveralRoutees r2 = (SeveralRoutees) logic.select("msg", routees);
|
||||
assertEquals(r2.getRoutees().get(0), routeeList.get(3));
|
||||
assertEquals(r2.getRoutees().get(1), routeeList.get(4));
|
||||
assertEquals(r2.getRoutees().get(2), routeeList.get(5));
|
||||
|
||||
SeveralRoutees r3 = (SeveralRoutees) logic.select("msg", routees);
|
||||
assertEquals(r3.getRoutees().get(0), routeeList.get(6));
|
||||
assertEquals(r3.getRoutees().get(1), routeeList.get(0));
|
||||
assertEquals(r3.getRoutees().get(2), routeeList.get(1));
|
||||
|
||||
//#unit-test-logic
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateUsageOfCustomRouter() {
|
||||
new TestKit(system) {{
|
||||
//#usage-1
|
||||
for (int n = 1; n <= 10; n++) {
|
||||
system.actorOf(Props.create(Storage.class), "s" + n);
|
||||
}
|
||||
|
||||
List<String> paths = new ArrayList<String>();
|
||||
for (int n = 1; n <= 10; n++) {
|
||||
paths.add("/user/s" + n);
|
||||
}
|
||||
|
||||
ActorRef redundancy1 =
|
||||
system.actorOf(new RedundancyGroup(paths, 3).props(),
|
||||
"redundancy1");
|
||||
redundancy1.tell("important", getTestActor());
|
||||
//#usage-1
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
expectMsgEquals("important");
|
||||
}
|
||||
|
||||
//#usage-2
|
||||
ActorRef redundancy2 = system.actorOf(FromConfig.getInstance().props(),
|
||||
"redundancy2");
|
||||
redundancy2.tell("very important", getTestActor());
|
||||
//#usage-2
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
expectMsgEquals("very important");
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
}
|
||||
48
akka-docs/src/test/java/jdocs/routing/RedundancyGroup.java
Normal file
48
akka-docs/src/test/java/jdocs/routing/RedundancyGroup.java
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.routing;
|
||||
|
||||
//#group
|
||||
import java.util.List;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.dispatch.Dispatchers;
|
||||
import akka.routing.Router;
|
||||
|
||||
import com.typesafe.config.Config;
|
||||
|
||||
import akka.routing.GroupBase;
|
||||
import static jdocs.routing.CustomRouterDocTest.RedundancyRoutingLogic;
|
||||
|
||||
public class RedundancyGroup extends GroupBase {
|
||||
private final List<String> paths;
|
||||
private final int nbrCopies;
|
||||
|
||||
public RedundancyGroup(List<String> paths, int nbrCopies) {
|
||||
this.paths = paths;
|
||||
this.nbrCopies = nbrCopies;
|
||||
}
|
||||
|
||||
public RedundancyGroup(Config config) {
|
||||
this(config.getStringList("routees.paths"),
|
||||
config.getInt("nbr-copies"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.lang.Iterable<String> getPaths(ActorSystem system) {
|
||||
return paths;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Router createRouter(ActorSystem system) {
|
||||
return new Router(new RedundancyRoutingLogic(nbrCopies));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String routerDispatcher() {
|
||||
return Dispatchers.DefaultDispatcherId();
|
||||
}
|
||||
|
||||
}
|
||||
//#group
|
||||
490
akka-docs/src/test/java/jdocs/routing/RouterDocTest.java
Normal file
490
akka-docs/src/test/java/jdocs/routing/RouterDocTest.java
Normal file
|
|
@ -0,0 +1,490 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.routing;
|
||||
|
||||
import akka.testkit.AkkaJUnitActorSystemResource;
|
||||
|
||||
import jdocs.AbstractJavaTest;
|
||||
import akka.testkit.javadsl.TestKit;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import scala.concurrent.duration.Duration;
|
||||
import scala.concurrent.duration.FiniteDuration;
|
||||
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
|
||||
|
||||
//#imports1
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.Terminated;
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.routing.ActorRefRoutee;
|
||||
import akka.routing.Routee;
|
||||
import akka.routing.Router;
|
||||
|
||||
//#imports1
|
||||
|
||||
|
||||
//#imports2
|
||||
import akka.actor.Address;
|
||||
import akka.actor.AddressFromURIString;
|
||||
import akka.actor.Kill;
|
||||
import akka.actor.PoisonPill;
|
||||
import akka.actor.SupervisorStrategy;
|
||||
import akka.actor.OneForOneStrategy;
|
||||
import akka.remote.routing.RemoteRouterConfig;
|
||||
import akka.routing.Broadcast;
|
||||
import akka.routing.BroadcastGroup;
|
||||
import akka.routing.BroadcastPool;
|
||||
import akka.routing.ConsistentHashingGroup;
|
||||
import akka.routing.ConsistentHashingPool;
|
||||
import akka.routing.DefaultResizer;
|
||||
import akka.routing.FromConfig;
|
||||
import akka.routing.RandomGroup;
|
||||
import akka.routing.RandomPool;
|
||||
import akka.routing.RoundRobinGroup;
|
||||
import akka.routing.RoundRobinPool;
|
||||
import akka.routing.RoundRobinRoutingLogic;
|
||||
import akka.routing.ScatterGatherFirstCompletedGroup;
|
||||
import akka.routing.ScatterGatherFirstCompletedPool;
|
||||
import akka.routing.BalancingPool;
|
||||
import akka.routing.SmallestMailboxPool;
|
||||
import akka.routing.TailChoppingGroup;
|
||||
import akka.routing.TailChoppingPool;
|
||||
|
||||
//#imports2
|
||||
|
||||
public class RouterDocTest extends AbstractJavaTest {
|
||||
|
||||
@ClassRule
|
||||
public static AkkaJUnitActorSystemResource actorSystemResource =
|
||||
new AkkaJUnitActorSystemResource("RouterDocTest",
|
||||
ConfigFactory.parseString(docs.routing.RouterDocSpec.config()));
|
||||
|
||||
private final ActorSystem system = actorSystemResource.getSystem();
|
||||
|
||||
|
||||
static
|
||||
//#router-in-actor
|
||||
public final class Work implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final String payload;
|
||||
public Work(String payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
||||
|
||||
//#router-in-actor
|
||||
static
|
||||
//#router-in-actor
|
||||
public class Master extends AbstractActor {
|
||||
|
||||
Router router;
|
||||
{
|
||||
List<Routee> routees = new ArrayList<Routee>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
ActorRef r = getContext().actorOf(Props.create(Worker.class));
|
||||
getContext().watch(r);
|
||||
routees.add(new ActorRefRoutee(r));
|
||||
}
|
||||
router = new Router(new RoundRobinRoutingLogic(), routees);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Work.class, message -> {
|
||||
router.route(message, getSender());
|
||||
})
|
||||
.match(Terminated.class, message -> {
|
||||
router = router.removeRoutee(message.actor());
|
||||
ActorRef r = getContext().actorOf(Props.create(Worker.class));
|
||||
getContext().watch(r);
|
||||
router = router.addRoutee(new ActorRefRoutee(r));
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
//#router-in-actor
|
||||
|
||||
static public class Worker extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().build();
|
||||
}
|
||||
}
|
||||
|
||||
static public class Echo extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> getSender().tell(message, getSelf()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
static public class Replier extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> {
|
||||
//#reply-with-self
|
||||
getSender().tell("reply", getSelf());
|
||||
//#reply-with-self
|
||||
|
||||
//#reply-with-parent
|
||||
getSender().tell("reply", getContext().getParent());
|
||||
//#reply-with-parent
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
//#create-worker-actors
|
||||
public class Workers extends AbstractActor {
|
||||
@Override public void preStart() {
|
||||
getContext().actorOf(Props.create(Worker.class), "w1");
|
||||
getContext().actorOf(Props.create(Worker.class), "w2");
|
||||
getContext().actorOf(Props.create(Worker.class), "w3");
|
||||
}
|
||||
// ...
|
||||
//#create-worker-actors
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().build();
|
||||
}
|
||||
}
|
||||
|
||||
static public class Parent extends AbstractActor {
|
||||
|
||||
//#paths
|
||||
List<String> paths = Arrays.asList("/user/workers/w1", "/user/workers/w2",
|
||||
"/user/workers/w3");
|
||||
//#paths
|
||||
|
||||
//#round-robin-pool-1
|
||||
ActorRef router1 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(Props.create(Worker.class)),
|
||||
"router1");
|
||||
//#round-robin-pool-1
|
||||
|
||||
//#round-robin-pool-2
|
||||
ActorRef router2 =
|
||||
getContext().actorOf(new RoundRobinPool(5).props(Props.create(Worker.class)),
|
||||
"router2");
|
||||
//#round-robin-pool-2
|
||||
|
||||
//#round-robin-group-1
|
||||
ActorRef router3 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(), "router3");
|
||||
//#round-robin-group-1
|
||||
|
||||
//#round-robin-group-2
|
||||
ActorRef router4 =
|
||||
getContext().actorOf(new RoundRobinGroup(paths).props(), "router4");
|
||||
//#round-robin-group-2
|
||||
|
||||
//#random-pool-1
|
||||
ActorRef router5 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router5");
|
||||
//#random-pool-1
|
||||
|
||||
//#random-pool-2
|
||||
ActorRef router6 =
|
||||
getContext().actorOf(new RandomPool(5).props(Props.create(Worker.class)),
|
||||
"router6");
|
||||
//#random-pool-2
|
||||
|
||||
//#random-group-1
|
||||
ActorRef router7 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(), "router7");
|
||||
//#random-group-1
|
||||
|
||||
//#random-group-2
|
||||
ActorRef router8 =
|
||||
getContext().actorOf(new RandomGroup(paths).props(), "router8");
|
||||
//#random-group-2
|
||||
|
||||
//#balancing-pool-1
|
||||
ActorRef router9 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router9");
|
||||
//#balancing-pool-1
|
||||
|
||||
//#balancing-pool-2
|
||||
ActorRef router10 =
|
||||
getContext().actorOf(new BalancingPool(5).props(
|
||||
Props.create(Worker.class)), "router10");
|
||||
//#balancing-pool-2
|
||||
|
||||
//#smallest-mailbox-pool-1
|
||||
ActorRef router11 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router11");
|
||||
//#smallest-mailbox-pool-1
|
||||
|
||||
//#smallest-mailbox-pool-2
|
||||
ActorRef router12 =
|
||||
getContext().actorOf(new SmallestMailboxPool(5).props(
|
||||
Props.create(Worker.class)), "router12");
|
||||
//#smallest-mailbox-pool-2
|
||||
|
||||
//#broadcast-pool-1
|
||||
ActorRef router13 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router13");
|
||||
//#broadcast-pool-1
|
||||
|
||||
//#broadcast-pool-2
|
||||
ActorRef router14 =
|
||||
getContext().actorOf(new BroadcastPool(5).props(Props.create(Worker.class)),
|
||||
"router14");
|
||||
//#broadcast-pool-2
|
||||
|
||||
//#broadcast-group-1
|
||||
ActorRef router15 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(), "router15");
|
||||
//#broadcast-group-1
|
||||
|
||||
//#broadcast-group-2
|
||||
ActorRef router16 =
|
||||
getContext().actorOf(new BroadcastGroup(paths).props(), "router16");
|
||||
//#broadcast-group-2
|
||||
|
||||
//#scatter-gather-pool-1
|
||||
ActorRef router17 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router17");
|
||||
//#scatter-gather-pool-1
|
||||
|
||||
//#scatter-gather-pool-2
|
||||
FiniteDuration within = FiniteDuration.create(10, TimeUnit.SECONDS);
|
||||
ActorRef router18 =
|
||||
getContext().actorOf(new ScatterGatherFirstCompletedPool(5, within).props(
|
||||
Props.create(Worker.class)), "router18");
|
||||
//#scatter-gather-pool-2
|
||||
|
||||
//#scatter-gather-group-1
|
||||
ActorRef router19 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(), "router19");
|
||||
//#scatter-gather-group-1
|
||||
|
||||
//#scatter-gather-group-2
|
||||
FiniteDuration within2 = FiniteDuration.create(10, TimeUnit.SECONDS);
|
||||
ActorRef router20 =
|
||||
getContext().actorOf(new ScatterGatherFirstCompletedGroup(paths, within2).props(),
|
||||
"router20");
|
||||
//#scatter-gather-group-2
|
||||
|
||||
//#tail-chopping-pool-1
|
||||
ActorRef router21 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router21");
|
||||
//#tail-chopping-pool-1
|
||||
|
||||
//#tail-chopping-pool-2
|
||||
FiniteDuration within3 = FiniteDuration.create(10, TimeUnit.SECONDS);
|
||||
FiniteDuration interval = FiniteDuration.create(20, TimeUnit.MILLISECONDS);
|
||||
ActorRef router22 =
|
||||
getContext().actorOf(new TailChoppingPool(5, within3, interval).props(
|
||||
Props.create(Worker.class)), "router22");
|
||||
//#tail-chopping-pool-2
|
||||
|
||||
//#tail-chopping-group-1
|
||||
ActorRef router23 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(), "router23");
|
||||
//#tail-chopping-group-1
|
||||
|
||||
//#tail-chopping-group-2
|
||||
FiniteDuration within4 = FiniteDuration.create(10, TimeUnit.SECONDS);
|
||||
FiniteDuration interval2 = FiniteDuration.create(20, TimeUnit.MILLISECONDS);
|
||||
ActorRef router24 =
|
||||
getContext().actorOf(new TailChoppingGroup(paths, within4, interval2).props(),
|
||||
"router24");
|
||||
//#tail-chopping-group-2
|
||||
|
||||
//#consistent-hashing-pool-1
|
||||
ActorRef router25 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(Props.create(Worker.class)),
|
||||
"router25");
|
||||
//#consistent-hashing-pool-1
|
||||
|
||||
//#consistent-hashing-pool-2
|
||||
ActorRef router26 =
|
||||
getContext().actorOf(new ConsistentHashingPool(5).props(
|
||||
Props.create(Worker.class)), "router26");
|
||||
//#consistent-hashing-pool-2
|
||||
|
||||
//#consistent-hashing-group-1
|
||||
ActorRef router27 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(), "router27");
|
||||
//#consistent-hashing-group-1
|
||||
|
||||
//#consistent-hashing-group-2
|
||||
ActorRef router28 =
|
||||
getContext().actorOf(new ConsistentHashingGroup(paths).props(), "router28");
|
||||
//#consistent-hashing-group-2
|
||||
|
||||
//#resize-pool-1
|
||||
ActorRef router29 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router29");
|
||||
//#resize-pool-1
|
||||
|
||||
//#resize-pool-2
|
||||
DefaultResizer resizer = new DefaultResizer(2, 15);
|
||||
ActorRef router30 =
|
||||
getContext().actorOf(new RoundRobinPool(5).withResizer(resizer).props(
|
||||
Props.create(Worker.class)), "router30");
|
||||
//#resize-pool-2
|
||||
|
||||
//#optimal-size-exploring-resize-pool
|
||||
ActorRef router31 =
|
||||
getContext().actorOf(FromConfig.getInstance().props(
|
||||
Props.create(Worker.class)), "router31");
|
||||
//#optimal-size-exploring-resize-pool
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().build();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createActors() {
|
||||
//#create-workers
|
||||
system.actorOf(Props.create(Workers.class), "workers");
|
||||
//#create-workers
|
||||
|
||||
//#create-parent
|
||||
system.actorOf(Props.create(Parent.class), "parent");
|
||||
//#create-parent
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateDispatcher() {
|
||||
//#dispatchers
|
||||
Props props =
|
||||
// “head” router actor will run on "router-dispatcher" dispatcher
|
||||
// Worker routees will run on "pool-dispatcher" dispatcher
|
||||
new RandomPool(5).withDispatcher("router-dispatcher").props(
|
||||
Props.create(Worker.class));
|
||||
ActorRef router = system.actorOf(props, "poolWithDispatcher");
|
||||
//#dispatchers
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateBroadcast() {
|
||||
new TestKit(system) {{
|
||||
ActorRef router = system.actorOf(new RoundRobinPool(5).props(
|
||||
Props.create(Echo.class)));
|
||||
//#broadcastDavyJonesWarning
|
||||
router.tell(new Broadcast("Watch out for Davy Jones' locker"), getTestActor());
|
||||
//#broadcastDavyJonesWarning
|
||||
assertEquals(5, receiveN(5).size());
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstratePoisonPill() {
|
||||
new TestKit(system) {{
|
||||
ActorRef router = watch(system.actorOf(new RoundRobinPool(5).props(
|
||||
Props.create(Echo.class))));
|
||||
//#poisonPill
|
||||
router.tell(PoisonPill.getInstance(), getTestActor());
|
||||
//#poisonPill
|
||||
expectTerminated(router);
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateBroadcastPoisonPill() {
|
||||
new TestKit(system) {{
|
||||
ActorRef router = watch(system.actorOf(new RoundRobinPool(5).props(
|
||||
Props.create(Echo.class))));
|
||||
//#broadcastPoisonPill
|
||||
router.tell(new Broadcast(PoisonPill.getInstance()), getTestActor());
|
||||
//#broadcastPoisonPill
|
||||
expectTerminated(router);
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateKill() {
|
||||
new TestKit(system) {{
|
||||
ActorRef router = watch(system.actorOf(new RoundRobinPool(5).props(
|
||||
Props.create(Echo.class))));
|
||||
//#kill
|
||||
router.tell(Kill.getInstance(), getTestActor());
|
||||
//#kill
|
||||
expectTerminated(router);
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateBroadcastKill() {
|
||||
new TestKit(system) {{
|
||||
ActorRef router = watch(system.actorOf(new RoundRobinPool(5).props(
|
||||
Props.create(Echo.class))));
|
||||
//#broadcastKill
|
||||
router.tell(new Broadcast(Kill.getInstance()), getTestActor());
|
||||
//#broadcastKill
|
||||
expectTerminated(router);
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateRemoteDeploy() {
|
||||
//#remoteRoutees
|
||||
Address[] addresses = {
|
||||
new Address("akka.tcp", "remotesys", "otherhost", 1234),
|
||||
AddressFromURIString.parse("akka.tcp://othersys@anotherhost:1234")};
|
||||
ActorRef routerRemote = system.actorOf(
|
||||
new RemoteRouterConfig(new RoundRobinPool(5), addresses).props(
|
||||
Props.create(Echo.class)));
|
||||
//#remoteRoutees
|
||||
}
|
||||
|
||||
// only compile
|
||||
public void demonstrateRemoteDeployWithArtery() {
|
||||
//#remoteRoutees-artery
|
||||
Address[] addresses = {
|
||||
new Address("akka", "remotesys", "otherhost", 1234),
|
||||
AddressFromURIString.parse("akka://othersys@anotherhost:1234")};
|
||||
ActorRef routerRemote = system.actorOf(
|
||||
new RemoteRouterConfig(new RoundRobinPool(5), addresses).props(
|
||||
Props.create(Echo.class)));
|
||||
//#remoteRoutees-artery
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateSupervisor() {
|
||||
//#supervision
|
||||
final SupervisorStrategy strategy =
|
||||
new OneForOneStrategy(5, Duration.create(1, TimeUnit.MINUTES),
|
||||
Collections.<Class<? extends Throwable>>singletonList(Exception.class));
|
||||
final ActorRef router = system.actorOf(new RoundRobinPool(5).
|
||||
withSupervisorStrategy(strategy).props(Props.create(Echo.class)));
|
||||
//#supervision
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue