Merge pull request #27623 from akka/wip-24717-doc-apply-style11-patriknw

doc: stylish routers.md, #24717
This commit is contained in:
Patrik Nordwall 2019-09-05 12:41:54 +02:00 committed by GitHub
commit 525863bb85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 23 deletions

View file

@ -12,6 +12,7 @@ import akka.actor.typed.ActorSystem;
import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior;
import akka.actor.typed.SupervisorStrategy;
import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.GroupRouter;
import akka.actor.typed.javadsl.PoolRouter;
@ -35,20 +36,21 @@ public class RouterTest {
}
}
static final Behavior<Command> behavior =
Behaviors.setup(
context -> {
context.getLog().info("Starting worker");
static final Behavior<Command> create() {
return Behaviors.setup(
context -> {
context.getLog().info("Starting worker");
return Behaviors.receive(Command.class)
.onMessage(
DoLog.class,
doLog -> {
context.getLog().info("Got message {}", doLog.text);
return Behaviors.same();
})
.build();
});
return Behaviors.receive(Command.class)
.onMessage(DoLog.class, doLog -> onDoLog(context, doLog))
.build();
});
}
private static Behavior<Command> onDoLog(ActorContext<Command> context, DoLog doLog) {
context.getLog().info("Got message {}", doLog.text);
return Behaviors.same();
}
}
// #pool
@ -57,12 +59,13 @@ public class RouterTest {
return Behaviors.setup(
context -> {
// #pool
int poolSize = 4;
PoolRouter<Worker.Command> pool =
Routers.pool(
4,
poolSize,
() ->
// make sure the workers are restarted if they fail
Behaviors.supervise(Worker.behavior).onFailure(SupervisorStrategy.restart()));
Behaviors.supervise(Worker.create()).onFailure(SupervisorStrategy.restart()));
ActorRef<Worker.Command> router = context.spawn(pool, "worker-pool");
for (int i = 0; i < 10; i++) {
@ -85,7 +88,7 @@ public class RouterTest {
// #group
// this would likely happen elsewhere - if we create it locally we
// can just as well use a pool
ActorRef<Worker.Command> worker = context.spawn(Worker.behavior, "worker");
ActorRef<Worker.Command> worker = context.spawn(Worker.create(), "worker");
context.getSystem().receptionist().tell(Receptionist.register(serviceKey, worker));
GroupRouter<Worker.Command> group = Routers.group(serviceKey);
@ -94,7 +97,7 @@ public class RouterTest {
// note that since registration of workers goes through the receptionist there is no
// guarantee the router has seen any workers yet if we hit it directly like this and
// these messages may end up in dead letters - in a real application you would not use
// a group router like this - it is to keep the sample simple
// a group router immediately like this - it is to keep the sample simple
for (int i = 0; i < 10; i++) {
router.tell(new Worker.DoLog("msg " + i));
}

View file

@ -23,12 +23,12 @@ object RouterSpec {
sealed trait Command
case class DoLog(text: String) extends Command
def apply(): Behavior[Command] = Behaviors.setup { ctx =>
ctx.log.info("Starting worker")
def apply(): Behavior[Command] = Behaviors.setup { context =>
context.log.info("Starting worker")
Behaviors.receiveMessage {
case DoLog(text) =>
ctx.log.info("Got message {}", text)
context.log.info("Got message {}", text)
Behaviors.same
}
}
@ -102,7 +102,7 @@ class RouterSpec extends ScalaTestWithActorTestKit("akka.loglevel=warning") with
// note that since registration of workers goes through the receptionist there is no
// guarantee the router has seen any workers yet if we hit it directly like this and
// these messages may end up in dead letters - in a real application you would not use
// a group router like this - it is to keep the sample simple
// a group router immediately like this - it is to keep the sample simple
(0 to 10).foreach { n =>
router ! Worker.DoLog(s"msg $n")
}

View file

@ -28,7 +28,7 @@ then forward messages to.
If a child is stopped the pool router removes it from its set of routees. When the last child stops the router itself stops.
To make a resilient router that deals with failures the routee `Behavior` must be supervised.
Note that it is important that the factory returns a new behavior instance for every call to the factory or else
Note that it is important that the `Routers.pool` factory returns a new behavior instance for every call to the factory or else
routees may end up sharing mutable state and not work as expected.
Scala
@ -92,4 +92,4 @@ it will not give better performance to create more routees than there are thread
Since the router itself is an actor and has a mailbox this means that messages are routed sequentially to the routees
where it can be processed in parallel (depending on the available threads in the dispatcher).
In a high throughput use cases the sequential routing could be a bottle neck. Akka Typed does not provide an optimized tool for this.
In a high throughput use cases the sequential routing could be a bottle neck. Akka Typed does not provide an optimized tool for this.