2018-10-29 17:19:37 +08:00
|
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2013-09-19 08:00:05 +02:00
|
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
|
package jdocs.routing;
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
|
import jdocs.AbstractJavaTest;
|
2017-03-17 03:02:47 +08:00
|
|
|
|
import akka.testkit.javadsl.TestKit;
|
2013-09-19 08:00:05 +02:00
|
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2018-06-26 15:41:30 +02:00
|
|
|
|
import java.time.Duration;
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #imports1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
|
import akka.actor.Terminated;
|
2017-02-04 11:51:30 +05:00
|
|
|
|
import akka.actor.AbstractActor;
|
2013-09-19 08:00:05 +02:00
|
|
|
|
import akka.routing.ActorRefRoutee;
|
|
|
|
|
|
import akka.routing.Routee;
|
|
|
|
|
|
import akka.routing.Router;
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #imports1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #imports2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
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;
|
2014-06-26 14:59:08 +02:00
|
|
|
|
import akka.routing.BalancingPool;
|
2013-09-19 08:00:05 +02:00
|
|
|
|
import akka.routing.SmallestMailboxPool;
|
2014-06-05 21:56:05 +02:00
|
|
|
|
import akka.routing.TailChoppingGroup;
|
|
|
|
|
|
import akka.routing.TailChoppingPool;
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #imports2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
|
public class RouterDocTest extends AbstractJavaTest {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
new AkkaJUnitActorSystemResource(
|
|
|
|
|
|
"RouterDocTest", ConfigFactory.parseString(docs.routing.RouterDocSpec.config()));
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #router-in-actor
|
|
|
|
|
|
static final class Work implements Serializable {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
public final String payload;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
public Work(String payload) {
|
|
|
|
|
|
this.payload = payload;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #router-in-actor
|
|
|
|
|
|
public
|
|
|
|
|
|
// #router-in-actor
|
|
|
|
|
|
static class Master extends AbstractActor {
|
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
Router router;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.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();
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #router-in-actor
|
|
|
|
|
|
|
|
|
|
|
|
public static class Worker extends AbstractActor {
|
2017-02-04 11:51:30 +05:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder().build();
|
|
|
|
|
|
}
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
|
|
|
|
|
public static class Echo extends AbstractActor {
|
2017-02-04 11:51:30 +05:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
return receiveBuilder().matchAny(message -> getSender().tell(message, getSelf())).build();
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
|
|
|
|
|
public static class Replier extends AbstractActor {
|
2017-02-04 11:51:30 +05:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchAny(
|
|
|
|
|
|
message -> {
|
|
|
|
|
|
// #reply-with-self
|
|
|
|
|
|
getSender().tell("reply", getSelf());
|
|
|
|
|
|
// #reply-with-self
|
|
|
|
|
|
|
|
|
|
|
|
// #reply-with-parent
|
|
|
|
|
|
getSender().tell("reply", getContext().getParent());
|
|
|
|
|
|
// #reply-with-parent
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #create-worker-actors
|
|
|
|
|
|
static class Workers extends AbstractActor {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void preStart() {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
getContext().actorOf(Props.create(Worker.class), "w1");
|
|
|
|
|
|
getContext().actorOf(Props.create(Worker.class), "w2");
|
|
|
|
|
|
getContext().actorOf(Props.create(Worker.class), "w3");
|
|
|
|
|
|
}
|
|
|
|
|
|
// ...
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #create-worker-actors
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder().build();
|
|
|
|
|
|
}
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public static class Parent extends AbstractActor {
|
|
|
|
|
|
|
|
|
|
|
|
// #paths
|
|
|
|
|
|
List<String> paths = Arrays.asList("/user/workers/w1", "/user/workers/w2", "/user/workers/w3");
|
|
|
|
|
|
// #paths
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #round-robin-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
ActorRef router1 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router1");
|
|
|
|
|
|
// #round-robin-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #round-robin-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
ActorRef router2 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().actorOf(new RoundRobinPool(5).props(Props.create(Worker.class)), "router2");
|
|
|
|
|
|
// #round-robin-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #round-robin-group-1
|
|
|
|
|
|
ActorRef router3 = getContext().actorOf(FromConfig.getInstance().props(), "router3");
|
|
|
|
|
|
// #round-robin-group-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #round-robin-group-2
|
|
|
|
|
|
ActorRef router4 = getContext().actorOf(new RoundRobinGroup(paths).props(), "router4");
|
|
|
|
|
|
// #round-robin-group-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #random-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
ActorRef router5 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router5");
|
|
|
|
|
|
// #random-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #random-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
ActorRef router6 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
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
|
2013-09-19 08:00:05 +02:00
|
|
|
|
ActorRef router9 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router9");
|
|
|
|
|
|
// #balancing-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #balancing-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
ActorRef router10 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().actorOf(new BalancingPool(5).props(Props.create(Worker.class)), "router10");
|
|
|
|
|
|
// #balancing-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #smallest-mailbox-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
ActorRef router11 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router11");
|
|
|
|
|
|
// #smallest-mailbox-pool-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #smallest-mailbox-pool-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
ActorRef router12 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(new SmallestMailboxPool(5).props(Props.create(Worker.class)), "router12");
|
|
|
|
|
|
// #smallest-mailbox-pool-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #broadcast-pool-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
ActorRef router13 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router13");
|
|
|
|
|
|
// #broadcast-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #broadcast-pool-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
ActorRef router14 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().actorOf(new BroadcastPool(5).props(Props.create(Worker.class)), "router14");
|
|
|
|
|
|
// #broadcast-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #broadcast-group-1
|
|
|
|
|
|
ActorRef router15 = getContext().actorOf(FromConfig.getInstance().props(), "router15");
|
|
|
|
|
|
// #broadcast-group-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #broadcast-group-2
|
|
|
|
|
|
ActorRef router16 = getContext().actorOf(new BroadcastGroup(paths).props(), "router16");
|
|
|
|
|
|
// #broadcast-group-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #scatter-gather-pool-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
ActorRef router17 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router17");
|
|
|
|
|
|
// #scatter-gather-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #scatter-gather-pool-2
|
2018-06-26 15:41:30 +02:00
|
|
|
|
Duration within = Duration.ofSeconds(10);
|
2014-01-10 17:14:10 +01:00
|
|
|
|
ActorRef router18 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(
|
|
|
|
|
|
new ScatterGatherFirstCompletedPool(5, within).props(Props.create(Worker.class)),
|
|
|
|
|
|
"router18");
|
|
|
|
|
|
// #scatter-gather-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #scatter-gather-group-1
|
|
|
|
|
|
ActorRef router19 = getContext().actorOf(FromConfig.getInstance().props(), "router19");
|
|
|
|
|
|
// #scatter-gather-group-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #scatter-gather-group-2
|
2018-06-26 15:41:30 +02:00
|
|
|
|
Duration within2 = Duration.ofSeconds(10);
|
2014-01-10 17:14:10 +01:00
|
|
|
|
ActorRef router20 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(new ScatterGatherFirstCompletedGroup(paths, within2).props(), "router20");
|
|
|
|
|
|
// #scatter-gather-group-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #tail-chopping-pool-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
ActorRef router21 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router21");
|
|
|
|
|
|
// #tail-chopping-pool-1
|
2014-06-05 21:56:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #tail-chopping-pool-2
|
2018-06-26 15:41:30 +02:00
|
|
|
|
Duration within3 = Duration.ofSeconds(10);
|
|
|
|
|
|
Duration interval = Duration.ofMillis(20);
|
2014-06-05 21:56:05 +02:00
|
|
|
|
ActorRef router22 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(
|
|
|
|
|
|
new TailChoppingPool(5, within3, interval).props(Props.create(Worker.class)),
|
|
|
|
|
|
"router22");
|
|
|
|
|
|
// #tail-chopping-pool-2
|
2014-06-05 21:56:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #tail-chopping-group-1
|
|
|
|
|
|
ActorRef router23 = getContext().actorOf(FromConfig.getInstance().props(), "router23");
|
|
|
|
|
|
// #tail-chopping-group-1
|
2014-06-05 21:56:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #tail-chopping-group-2
|
2018-06-26 15:41:30 +02:00
|
|
|
|
Duration within4 = Duration.ofSeconds(10);
|
|
|
|
|
|
Duration interval2 = Duration.ofMillis(20);
|
2014-06-05 21:56:05 +02:00
|
|
|
|
ActorRef router24 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().actorOf(new TailChoppingGroup(paths, within4, interval2).props(), "router24");
|
|
|
|
|
|
// #tail-chopping-group-2
|
2014-06-05 21:56:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #consistent-hashing-pool-1
|
2014-06-05 21:56:05 +02:00
|
|
|
|
ActorRef router25 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router25");
|
|
|
|
|
|
// #consistent-hashing-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #consistent-hashing-pool-2
|
2014-06-05 21:56:05 +02:00
|
|
|
|
ActorRef router26 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(new ConsistentHashingPool(5).props(Props.create(Worker.class)), "router26");
|
|
|
|
|
|
// #consistent-hashing-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #consistent-hashing-group-1
|
|
|
|
|
|
ActorRef router27 = getContext().actorOf(FromConfig.getInstance().props(), "router27");
|
|
|
|
|
|
// #consistent-hashing-group-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #consistent-hashing-group-2
|
|
|
|
|
|
ActorRef router28 = getContext().actorOf(new ConsistentHashingGroup(paths).props(), "router28");
|
|
|
|
|
|
// #consistent-hashing-group-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #resize-pool-1
|
2014-06-05 21:56:05 +02:00
|
|
|
|
ActorRef router29 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router29");
|
|
|
|
|
|
// #resize-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #resize-pool-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
DefaultResizer resizer = new DefaultResizer(2, 15);
|
2014-06-05 21:56:05 +02:00
|
|
|
|
ActorRef router30 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(
|
|
|
|
|
|
new RoundRobinPool(5).withResizer(resizer).props(Props.create(Worker.class)),
|
|
|
|
|
|
"router30");
|
|
|
|
|
|
// #resize-pool-2
|
2015-08-21 21:03:57 -04:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #optimal-size-exploring-resize-pool
|
2015-08-21 21:03:57 -04:00
|
|
|
|
ActorRef router31 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext()
|
|
|
|
|
|
.actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router31");
|
|
|
|
|
|
// #optimal-size-exploring-resize-pool
|
2015-08-21 21:03:57 -04:00
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder().build();
|
|
|
|
|
|
}
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void createActors() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #create-workers
|
2013-09-19 08:00:05 +02:00
|
|
|
|
system.actorOf(Props.create(Workers.class), "workers");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #create-workers
|
|
|
|
|
|
|
|
|
|
|
|
// #create-parent
|
2013-09-19 08:00:05 +02:00
|
|
|
|
system.actorOf(Props.create(Parent.class), "parent");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #create-parent
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateDispatcher() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #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));
|
2013-10-16 13:02:35 +02:00
|
|
|
|
ActorRef router = system.actorOf(props, "poolWithDispatcher");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #dispatchers
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateBroadcast() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
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());
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstratePoisonPill() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateBroadcastPoisonPill() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateKill() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateBroadcastKill() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateRemoteDeploy() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #remoteRoutees
|
2013-09-19 08:00:05 +02:00
|
|
|
|
Address[] addresses = {
|
2019-05-15 18:01:34 +02:00
|
|
|
|
new Address("akka", "remotesys", "otherhost", 1234),
|
|
|
|
|
|
AddressFromURIString.parse("akka://othersys@anotherhost:1234")
|
2019-01-12 04:00:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
ActorRef routerRemote =
|
|
|
|
|
|
system.actorOf(
|
|
|
|
|
|
new RemoteRouterConfig(new RoundRobinPool(5), addresses)
|
|
|
|
|
|
.props(Props.create(Echo.class)));
|
|
|
|
|
|
// #remoteRoutees
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2016-09-30 18:20:47 +02:00
|
|
|
|
// only compile
|
|
|
|
|
|
public void demonstrateRemoteDeployWithArtery() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #remoteRoutees-artery
|
2016-09-30 18:20:47 +02:00
|
|
|
|
Address[] addresses = {
|
|
|
|
|
|
new Address("akka", "remotesys", "otherhost", 1234),
|
2019-01-12 04:00:53 +08:00
|
|
|
|
AddressFromURIString.parse("akka://othersys@anotherhost:1234")
|
|
|
|
|
|
};
|
|
|
|
|
|
ActorRef routerRemote =
|
|
|
|
|
|
system.actorOf(
|
|
|
|
|
|
new RemoteRouterConfig(new RoundRobinPool(5), addresses)
|
|
|
|
|
|
.props(Props.create(Echo.class)));
|
|
|
|
|
|
// #remoteRoutees-artery
|
2016-09-30 18:20:47 +02:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateSupervisor() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #supervision
|
2013-09-19 08:00:05 +02:00
|
|
|
|
final SupervisorStrategy strategy =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
new OneForOneStrategy(
|
|
|
|
|
|
5,
|
|
|
|
|
|
Duration.ofMinutes(1),
|
|
|
|
|
|
Collections.<Class<? extends Throwable>>singletonList(Exception.class));
|
|
|
|
|
|
final ActorRef router =
|
|
|
|
|
|
system.actorOf(
|
|
|
|
|
|
new RoundRobinPool(5).withSupervisorStrategy(strategy).props(Props.create(Echo.class)));
|
|
|
|
|
|
// #supervision
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|