From 4b2f1984ec36e4fdac2660d30d03ebe8eedba1b6 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Wed, 4 Sep 2019 07:20:46 +0200 Subject: [PATCH] doc: stylish routers.md, #24717 --- .../java/jdocs/akka/typed/RouterTest.java | 37 ++++++++++--------- .../scala/docs/akka/typed/RouterSpec.scala | 8 ++-- akka-docs/src/main/paradox/typed/routers.md | 4 +- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/RouterTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/RouterTest.java index 5013465983..e5f39ed39f 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/RouterTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/RouterTest.java @@ -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 behavior = - Behaviors.setup( - context -> { - context.getLog().info("Starting worker"); + static final Behavior 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 onDoLog(ActorContext 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 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 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 = context.spawn(Worker.behavior, "worker"); + ActorRef worker = context.spawn(Worker.create(), "worker"); context.getSystem().receptionist().tell(Receptionist.register(serviceKey, worker)); GroupRouter 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)); } diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/RouterSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/RouterSpec.scala index 77b0506d1e..a5567834cf 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/RouterSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/RouterSpec.scala @@ -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") } diff --git a/akka-docs/src/main/paradox/typed/routers.md b/akka-docs/src/main/paradox/typed/routers.md index 90c2ff7b1d..0aec247bd1 100644 --- a/akka-docs/src/main/paradox/typed/routers.md +++ b/akka-docs/src/main/paradox/typed/routers.md @@ -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. \ No newline at end of file +In a high throughput use cases the sequential routing could be a bottle neck. Akka Typed does not provide an optimized tool for this.