From b2bef35f0c13934799a487a72aff573df7899a11 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Tue, 3 Sep 2019 13:07:21 +0200 Subject: [PATCH] doc: stylish getting started tutuorial, #24717 (#27596) * Move messages from DeviceProtocol to Device (in .java) * rename Message to Command * rename createBehavior to create * rename message handle methods to onX * private constructor * get rid of DeviceManagerProtocol too * rename Message to Command --- .../main/paradox/typed/guide/tutorial_3.md | 12 +- .../main/paradox/typed/guide/tutorial_4.md | 12 +- .../main/paradox/typed/guide/tutorial_5.md | 2 +- .../tutorial_1/ActorHierarchyExperiments.java | 35 ++--- .../java/jdocs/typed/tutorial_2/IotMain.java | 2 +- .../jdocs/typed/tutorial_2/IotSupervisor.java | 6 +- .../java/jdocs/typed/tutorial_3/Device.java | 74 ++++++--- .../typed/tutorial_3/DeviceProtocol.java | 58 ------- .../jdocs/typed/tutorial_3/DeviceTest.java | 31 ++-- .../{DeviceProtocol.java => Device.java} | 8 +- .../typed/tutorial_3/inprogress2/Device.java | 51 ++++-- .../inprogress2/DeviceProtocol.java | 38 ----- .../{DeviceProtocol.java => Device.java} | 12 +- .../java/jdocs/typed/tutorial_4/Device.java | 78 ++++++--- .../jdocs/typed/tutorial_4/DeviceGroup.java | 56 +++---- .../typed/tutorial_4/DeviceGroupTest.java | 28 ++-- .../jdocs/typed/tutorial_4/DeviceManager.java | 83 ++++++++-- .../tutorial_4/DeviceManagerProtocol.java | 67 -------- .../typed/tutorial_4/DeviceManagerTest.java | 6 +- .../typed/tutorial_4/DeviceProtocol.java | 64 -------- .../jdocs/typed/tutorial_4/DeviceTest.java | 30 ++-- .../java/jdocs/typed/tutorial_5/Device.java | 71 +++++++-- .../jdocs/typed/tutorial_5/DeviceGroup.java | 66 ++++---- .../typed/tutorial_5/DeviceGroupQuery.java | 98 ++++++------ .../tutorial_5/DeviceGroupQueryTest.java | 95 +++++------ .../typed/tutorial_5/DeviceGroupTest.java | 48 +++--- .../jdocs/typed/tutorial_5/DeviceManager.java | 148 ++++++++++++++++-- .../tutorial_5/DeviceManagerProtocol.java | 134 ---------------- .../typed/tutorial_5/DeviceManagerTest.java | 6 +- .../typed/tutorial_5/DeviceProtocol.java | 62 -------- .../jdocs/typed/tutorial_5/DeviceTest.java | 30 ++-- .../test/scala/typed/tutorial_3/Device.scala | 16 +- .../typed/tutorial_3/DeviceInProgress.scala | 22 +-- .../test/scala/typed/tutorial_4/Device.scala | 18 +-- .../scala/typed/tutorial_4/DeviceGroup.scala | 20 +-- .../typed/tutorial_4/DeviceManager.scala | 27 ++-- .../test/scala/typed/tutorial_5/Device.scala | 18 +-- .../scala/typed/tutorial_5/DeviceGroup.scala | 26 +-- .../typed/tutorial_5/DeviceGroupQuery.scala | 52 +++--- .../tutorial_5/DeviceGroupQuerySpec.scala | 22 +-- .../typed/tutorial_5/DeviceManager.scala | 34 ++-- 41 files changed, 833 insertions(+), 933 deletions(-) delete mode 100644 akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceProtocol.java rename akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/{DeviceProtocol.java => Device.java} (73%) delete mode 100644 akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/DeviceProtocol.java rename akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/{DeviceProtocol.java => Device.java} (50%) delete mode 100644 akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerProtocol.java delete mode 100644 akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceProtocol.java delete mode 100644 akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerProtocol.java delete mode 100644 akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceProtocol.java diff --git a/akka-docs/src/main/paradox/typed/guide/tutorial_3.md b/akka-docs/src/main/paradox/typed/guide/tutorial_3.md index 95f7024a44..179cca4d71 100644 --- a/akka-docs/src/main/paradox/typed/guide/tutorial_3.md +++ b/akka-docs/src/main/paradox/typed/guide/tutorial_3.md @@ -30,7 +30,7 @@ Scala : @@snip [DeviceInProgress.scala](/akka-docs/src/test/scala/typed/tutorial_3/DeviceInProgress.scala) { #read-protocol-1 } Java -: @@snip [DeviceInProgress.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/DeviceProtocol.java) { #read-protocol-1 } +: @@snip [Device.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/Device.java) { #read-protocol-1 } Note that the `ReadTemperature` message contains the @scala[`ActorRef[RespondTemperature]`]@java[`ActorRef`] that the device actor will use when replying to the request. @@ -118,7 +118,7 @@ Scala : @@snip [DeviceInProgress.scala](/akka-docs/src/test/scala/typed/tutorial_3/DeviceInProgress.scala) { #read-protocol-2 } Java -: @@snip [DeviceInProgress2.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/DeviceProtocol.java) { #read-protocol-2 } +: @@snip [Device.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/Device.java) { #read-protocol-2 } ## Implementing the device actor and its read protocol @@ -132,8 +132,8 @@ Java Note in the code that: -* The @scala[apply method in the companion object]@java[static method] defines how to construct the `Behavior` for the `Device` actor. The parameters include an ID for the device and the group to which it belongs, which we will use later. -* The messages we reasoned about previously are defined in @scala[the companion object.]@java[DeviceProtocol class that was shown earlier.] +* The @scala[`apply` method in the companion object]@java[static `create` method] defines how to construct the `Behavior` for the `Device` actor. The parameters include an ID for the device and the group to which it belongs, which we will use later. +* The messages we reasoned about previously are defined in @scala[the companion object.]@java[Device class that was shown earlier.] * In the `Device` class, the value of `lastTemperatureReading` is initially set to @scala[`None`]@java[`Optional.empty()`], and the actor will report it back if queried. ## Testing the actor @@ -159,7 +159,7 @@ Scala : @@snip [DeviceInProgress.scala](/akka-docs/src/test/scala/typed/tutorial_3/DeviceInProgress.scala) { #write-protocol-1 } Java -: @@snip [DeviceInProgress3.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/DeviceProtocol.java) { #write-protocol-1 } +: @@snip [Device.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/Device.java) { #write-protocol-1 } However, this approach does not take into account that the sender of the record temperature message can never be sure if the message was processed or not. We have seen that Akka does not guarantee delivery of these messages and leaves it to the application to provide success notifications. In our case, we would like to send an acknowledgment to the sender once we have updated our last temperature recording, e.g. replying with a `TemperatureRecorded` message. Just like in the case of temperature queries and responses, it is also a good idea to include an ID field to provide maximum flexibility. @@ -168,7 +168,7 @@ Scala : @@snip [DeviceInProgress.scala](/akka-docs/src/test/scala/typed/tutorial_3/Device.scala) { #write-protocol } Java -: @@snip [DeviceInProgress3.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceProtocol.java) { #write-protocol } +: @@snip [Device.java](/akka-docs/src/test/java/jdocs/typed/tutorial_3/Device.java) { #write-protocol } ## Actor with read and write messages diff --git a/akka-docs/src/main/paradox/typed/guide/tutorial_4.md b/akka-docs/src/main/paradox/typed/guide/tutorial_4.md index a30ea8814c..2cf228e503 100644 --- a/akka-docs/src/main/paradox/typed/guide/tutorial_4.md +++ b/akka-docs/src/main/paradox/typed/guide/tutorial_4.md @@ -70,7 +70,7 @@ Scala : @@snip [DeviceManager.scala](/akka-docs/src/test/scala/typed/tutorial_4/DeviceManager.scala) { #device-registration-msgs } Java -: @@snip [DeviceManager.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerProtocol.java) { #device-registration-msgs } +: @@snip [DeviceManager.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManager.java) { #device-registration-msgs } In this case we have not included a request ID field in the messages. Since registration happens once, when the component connects the system to some network protocol, the ID is not important. However, it is usually a best practice to include a request ID. @@ -141,7 +141,7 @@ Scala : @@snip [DeviceManager.scala](/akka-docs/src/test/scala/typed/tutorial_4/DeviceManager.scala) { #device-list-msgs } Java -: @@snip [DeviceManager.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerProtocol.java) { #device-list-msgs } +: @@snip [DeviceManager.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManager.java) { #device-list-msgs } Scala @@ -156,17 +156,17 @@ We are almost ready to test the removal of devices. But, we still need the follo * To be notified once the device actor is stopped. We can use the _Death Watch_ facility for this purpose, too. Scala -: @@snip [DeviceManager.scala](/akka-docs/src/test/scala/typed/tutorial_4/Device.scala) { #passivate-msg } +: @@snip [Device.scala](/akka-docs/src/test/scala/typed/tutorial_4/Device.scala) { #passivate-msg } Java -: @@snip [DeviceManager.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceProtocol.java) { #passivate-msg } +: @@snip [Device.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/Device.java) { #passivate-msg } Scala -: @@snip [DeviceManager.scala](/akka-docs/src/test/scala/typed/tutorial_4/Device.scala) { #device-with-passivate } +: @@snip [Device.scala](/akka-docs/src/test/scala/typed/tutorial_4/Device.scala) { #device-with-passivate } Java -: @@snip [DeviceManager.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/Device.java) { #device-with-passivate } +: @@snip [Device.java](/akka-docs/src/test/java/jdocs/typed/tutorial_4/Device.java) { #device-with-passivate } We add two more test cases now. In the first, we test that we get back the list of proper IDs once we have added a few devices. The second test case makes sure that the device ID is properly removed after the device actor has been stopped. The `TestProbe` has a `expectTerminated` method that we can easily use to assert that the device actor has been terminated. diff --git a/akka-docs/src/main/paradox/typed/guide/tutorial_5.md b/akka-docs/src/main/paradox/typed/guide/tutorial_5.md index c8c937aeb8..f4fc56262f 100644 --- a/akka-docs/src/main/paradox/typed/guide/tutorial_5.md +++ b/akka-docs/src/main/paradox/typed/guide/tutorial_5.md @@ -41,7 +41,7 @@ Scala : @@snip [DeviceGroup.scala](/akka-docs/src/test/scala/typed/tutorial_5/DeviceManager.scala) { #query-protocol } Java -: @@snip [DeviceGroup.java](/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerProtocol.java) { #query-protocol } +: @@snip [DeviceGroup.java](/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManager.java) { #query-protocol } ## Implementing the query diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_1/ActorHierarchyExperiments.java b/akka-docs/src/test/java/jdocs/typed/tutorial_1/ActorHierarchyExperiments.java index 4a1bcc5923..1a6a3478ee 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_1/ActorHierarchyExperiments.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_1/ActorHierarchyExperiments.java @@ -24,7 +24,7 @@ import akka.actor.typed.javadsl.Receive; class PrintMyActorRefActor extends AbstractBehavior { - static Behavior createBehavior() { + static Behavior create() { return Behaviors.setup(PrintMyActorRefActor::new); } @@ -50,7 +50,7 @@ class PrintMyActorRefActor extends AbstractBehavior { // #start-stop class StartStopActor1 extends AbstractBehavior { - static Behavior createBehavior() { + static Behavior create() { return Behaviors.setup(context -> new StartStopActor1()); } @@ -62,11 +62,11 @@ class StartStopActor1 extends AbstractBehavior { public Receive createReceive() { return newReceiveBuilder() .onMessageEquals("stop", Behaviors::stopped) - .onSignal(PostStop.class, signal -> postStop()) + .onSignal(PostStop.class, signal -> onPostStop()) .build(); } - private Behavior postStop() { + private Behavior onPostStop() { System.out.println("first stopped"); return this; } @@ -74,7 +74,7 @@ class StartStopActor1 extends AbstractBehavior { class StartStopActor2 extends AbstractBehavior { - static Behavior createBehavior() { + static Behavior create() { return Behaviors.setup(context -> new StartStopActor2()); } @@ -84,10 +84,10 @@ class StartStopActor2 extends AbstractBehavior { @Override public Receive createReceive() { - return newReceiveBuilder().onSignal(PostStop.class, signal -> postStop()).build(); + return newReceiveBuilder().onSignal(PostStop.class, signal -> onPostStop()).build(); } - private Behavior postStop() { + private Behavior onPostStop() { System.out.println("second stopped"); return this; } @@ -97,7 +97,7 @@ class StartStopActor2 extends AbstractBehavior { // #supervise class SupervisingActor extends AbstractBehavior { - static Behavior createBehavior() { + static Behavior create() { return Behaviors.setup(SupervisingActor::new); } @@ -106,17 +106,16 @@ class SupervisingActor extends AbstractBehavior { private SupervisingActor(ActorContext context) { child = context.spawn( - Behaviors.supervise(SupervisedActor.createBehavior()) - .onFailure(SupervisorStrategy.restart()), + Behaviors.supervise(SupervisedActor.create()).onFailure(SupervisorStrategy.restart()), "supervised-actor"); } @Override public Receive createReceive() { - return newReceiveBuilder().onMessageEquals("failChild", this::failChild).build(); + return newReceiveBuilder().onMessageEquals("failChild", this::onFailChild).build(); } - private Behavior failChild() { + private Behavior onFailChild() { child.tell("fail"); return this; } @@ -124,7 +123,7 @@ class SupervisingActor extends AbstractBehavior { class SupervisedActor extends AbstractBehavior { - static Behavior createBehavior() { + static Behavior create() { return Behaviors.setup(context -> new SupervisedActor()); } @@ -162,7 +161,7 @@ class SupervisedActor extends AbstractBehavior { class Main extends AbstractBehavior { - static Behavior createBehavior() { + static Behavior create() { return Behaviors.setup(Main::new); } @@ -178,7 +177,7 @@ class Main extends AbstractBehavior { } private Behavior start() { - ActorRef firstRef = context.spawn(PrintMyActorRefActor.createBehavior(), "first-actor"); + ActorRef firstRef = context.spawn(PrintMyActorRefActor.create(), "first-actor"); System.out.println("First: " + firstRef); firstRef.tell("printit"); @@ -188,7 +187,7 @@ class Main extends AbstractBehavior { public class ActorHierarchyExperiments { public static void main(String[] args) { - ActorRef testSystem = ActorSystem.create(Main.createBehavior(), "testSystem"); + ActorRef testSystem = ActorSystem.create(Main.create(), "testSystem"); testSystem.tell("start"); } } @@ -201,7 +200,7 @@ class ActorHierarchyExperimentsTest extends JUnitSuite { @Test public void testStartAndStopActors() { // #start-stop-main - ActorRef first = testKit.spawn(StartStopActor1.createBehavior(), "first"); + ActorRef first = testKit.spawn(StartStopActor1.create(), "first"); first.tell("stop"); // #start-stop-main } @@ -210,7 +209,7 @@ class ActorHierarchyExperimentsTest extends JUnitSuite { public void testSuperviseActors() throws Exception { // #supervise-main ActorRef supervisingActor = - testKit.spawn(SupervisingActor.createBehavior(), "supervising-actor"); + testKit.spawn(SupervisingActor.create(), "supervising-actor"); supervisingActor.tell("failChild"); // #supervise-main Thread.sleep(200); // allow for the println/logging to complete diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotMain.java b/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotMain.java index d2e9c7236b..7de7f0060b 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotMain.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotMain.java @@ -11,7 +11,7 @@ public class IotMain { public static void main(String[] args) { // Create ActorSystem and top level supervisor - ActorSystem.create(IotSupervisor.createBehavior(), "iot-system"); + ActorSystem.create(IotSupervisor.create(), "iot-system"); } } // #iot-app diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotSupervisor.java b/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotSupervisor.java index c0b9907f5c..c2f1f992d9 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotSupervisor.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_2/IotSupervisor.java @@ -14,7 +14,7 @@ import akka.actor.typed.javadsl.Receive; public class IotSupervisor extends AbstractBehavior { - public static Behavior createBehavior() { + public static Behavior create() { return Behaviors.setup(IotSupervisor::new); } @@ -28,10 +28,10 @@ public class IotSupervisor extends AbstractBehavior { // No need to handle any messages @Override public Receive createReceive() { - return newReceiveBuilder().onSignal(PostStop.class, signal -> postStop()).build(); + return newReceiveBuilder().onSignal(PostStop.class, signal -> onPostStop()).build(); } - private IotSupervisor postStop() { + private IotSupervisor onPostStop() { context.getLog().info("IoT Application stopped"); return this; } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_3/Device.java b/akka-docs/src/test/java/jdocs/typed/tutorial_3/Device.java index a22589583b..7b372ee6c2 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_3/Device.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_3/Device.java @@ -8,6 +8,7 @@ package jdocs.typed.tutorial_3; import java.util.Optional; +import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.PostStop; import akka.actor.typed.javadsl.AbstractBehavior; @@ -15,28 +16,63 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; -// #full-device -import static jdocs.typed.tutorial_3.DeviceProtocol.*; -/* -//#full-device -import static com.lightbend.akka.sample.DeviceProtocol.*; -//#full-device -*/ -// #full-device +public class Device extends AbstractBehavior { -public class Device extends AbstractBehavior { + public interface Command {} - public static Behavior createBehavior(String groupId, String deviceId) { + // #write-protocol + public static final class RecordTemperature implements Command { + final long requestId; + final double value; + final ActorRef replyTo; + + public RecordTemperature(long requestId, double value, ActorRef replyTo) { + this.requestId = requestId; + this.value = value; + this.replyTo = replyTo; + } + } + + public static final class TemperatureRecorded { + final long requestId; + + public TemperatureRecorded(long requestId) { + this.requestId = requestId; + } + } + // #write-protocol + + public static final class ReadTemperature implements Command { + final long requestId; + final ActorRef replyTo; + + public ReadTemperature(long requestId, ActorRef replyTo) { + this.requestId = requestId; + this.replyTo = replyTo; + } + } + + public static final class RespondTemperature { + final long requestId; + final Optional value; + + public RespondTemperature(long requestId, Optional value) { + this.requestId = requestId; + this.value = value; + } + } + + public static Behavior create(String groupId, String deviceId) { return Behaviors.setup(context -> new Device(context, groupId, deviceId)); } - private final ActorContext context; + private final ActorContext context; private final String groupId; private final String deviceId; private Optional lastTemperatureReading = Optional.empty(); - public Device(ActorContext context, String groupId, String deviceId) { + private Device(ActorContext context, String groupId, String deviceId) { this.context = context; this.groupId = groupId; this.deviceId = deviceId; @@ -45,27 +81,27 @@ public class Device extends AbstractBehavior { } @Override - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() - .onMessage(RecordTemperature.class, this::recordTemperature) - .onMessage(ReadTemperature.class, this::readTemperature) - .onSignal(PostStop.class, signal -> postStop()) + .onMessage(RecordTemperature.class, this::onRecordTemperature) + .onMessage(ReadTemperature.class, this::onReadTemperature) + .onSignal(PostStop.class, signal -> onPostStop()) .build(); } - private Behavior recordTemperature(RecordTemperature r) { + private Behavior onRecordTemperature(RecordTemperature r) { context.getLog().info("Recorded temperature reading {} with {}", r.value, r.requestId); lastTemperatureReading = Optional.of(r.value); r.replyTo.tell(new TemperatureRecorded(r.requestId)); return this; } - private Behavior readTemperature(ReadTemperature r) { + private Behavior onReadTemperature(ReadTemperature r) { r.replyTo.tell(new RespondTemperature(r.requestId, lastTemperatureReading)); return this; } - private Behavior postStop() { + private Behavior onPostStop() { context.getLog().info("Device actor {}-{} stopped", groupId, deviceId); return Behaviors.stopped(); } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceProtocol.java deleted file mode 100644 index 88cf230f76..0000000000 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceProtocol.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2009-2019 Lightbend Inc. - */ - -package jdocs.typed.tutorial_3; - -import akka.actor.typed.ActorRef; - -import java.util.Optional; - -abstract class DeviceProtocol { - // no instances of DeviceProtocol class - private DeviceProtocol() {} - - interface DeviceMessage {} - - // #write-protocol - public static final class RecordTemperature implements DeviceMessage { - final long requestId; - final double value; - final ActorRef replyTo; - - public RecordTemperature(long requestId, double value, ActorRef replyTo) { - this.requestId = requestId; - this.value = value; - this.replyTo = replyTo; - } - } - - public static final class TemperatureRecorded { - final long requestId; - - public TemperatureRecorded(long requestId) { - this.requestId = requestId; - } - } - // #write-protocol - - public static final class ReadTemperature implements DeviceMessage { - final long requestId; - final ActorRef replyTo; - - public ReadTemperature(long requestId, ActorRef replyTo) { - this.requestId = requestId; - this.replyTo = replyTo; - } - } - - public static final class RespondTemperature { - final long requestId; - final Optional value; - - public RespondTemperature(long requestId, Optional value) { - this.requestId = requestId; - this.value = value; - } - } -} diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceTest.java index 1e224e2c01..16d37f326d 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_3/DeviceTest.java @@ -15,10 +15,8 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; // #device-read-test -import static jdocs.typed.tutorial_3.DeviceProtocol.*; /* //#device-read-test -import static com.lightbend.akka.sample.DeviceProtocol.*; public class DeviceTest { //#device-read-test @@ -30,10 +28,11 @@ public class DeviceTest extends org.scalatest.junit.JUnitSuite { @Test public void testReplyWithEmptyReadingIfNoTemperatureIsKnown() { - TestProbe probe = testKit.createTestProbe(RespondTemperature.class); - ActorRef deviceActor = testKit.spawn(Device.createBehavior("group", "device")); - deviceActor.tell(new ReadTemperature(42L, probe.getRef())); - RespondTemperature response = probe.receiveMessage(); + TestProbe probe = + testKit.createTestProbe(Device.RespondTemperature.class); + ActorRef deviceActor = testKit.spawn(Device.create("group", "device")); + deviceActor.tell(new Device.ReadTemperature(42L, probe.getRef())); + Device.RespondTemperature response = probe.receiveMessage(); assertEquals(42L, response.requestId); assertEquals(Optional.empty(), response.value); } @@ -42,23 +41,25 @@ public class DeviceTest extends org.scalatest.junit.JUnitSuite { // #device-write-read-test @Test public void testReplyWithLatestTemperatureReading() { - TestProbe recordProbe = testKit.createTestProbe(TemperatureRecorded.class); - TestProbe readProbe = testKit.createTestProbe(RespondTemperature.class); - ActorRef deviceActor = testKit.spawn(Device.createBehavior("group", "device")); + TestProbe recordProbe = + testKit.createTestProbe(Device.TemperatureRecorded.class); + TestProbe readProbe = + testKit.createTestProbe(Device.RespondTemperature.class); + ActorRef deviceActor = testKit.spawn(Device.create("group", "device")); - deviceActor.tell(new RecordTemperature(1L, 24.0, recordProbe.getRef())); + deviceActor.tell(new Device.RecordTemperature(1L, 24.0, recordProbe.getRef())); assertEquals(1L, recordProbe.receiveMessage().requestId); - deviceActor.tell(new ReadTemperature(2L, readProbe.getRef())); - RespondTemperature response1 = readProbe.receiveMessage(); + deviceActor.tell(new Device.ReadTemperature(2L, readProbe.getRef())); + Device.RespondTemperature response1 = readProbe.receiveMessage(); assertEquals(2L, response1.requestId); assertEquals(Optional.of(24.0), response1.value); - deviceActor.tell(new RecordTemperature(3L, 55.0, recordProbe.getRef())); + deviceActor.tell(new Device.RecordTemperature(3L, 55.0, recordProbe.getRef())); assertEquals(3L, recordProbe.receiveMessage().requestId); - deviceActor.tell(new ReadTemperature(4L, readProbe.getRef())); - RespondTemperature response2 = readProbe.receiveMessage(); + deviceActor.tell(new Device.ReadTemperature(4L, readProbe.getRef())); + Device.RespondTemperature response2 = readProbe.receiveMessage(); assertEquals(4L, response2.requestId); assertEquals(Optional.of(55.0), response2.value); } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/DeviceProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/Device.java similarity index 73% rename from akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/DeviceProtocol.java rename to akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/Device.java index 6edd6f4bc7..d3a436ebc7 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/DeviceProtocol.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress1/Device.java @@ -9,13 +9,11 @@ import akka.actor.typed.ActorRef; import java.util.Optional; // #read-protocol-1 -abstract class DeviceProtocol { - // no instances of DeviceProtocol class - private DeviceProtocol() {} +public class Device { - interface DeviceMessage {} + public interface Command {} - public static final class ReadTemperature implements DeviceMessage { + public static final class ReadTemperature implements Command { final ActorRef replyTo; public ReadTemperature(ActorRef replyTo) { diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/Device.java b/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/Device.java index dc451ac8f0..ffac718347 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/Device.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/Device.java @@ -6,6 +6,7 @@ package jdocs.typed.tutorial_3.inprogress2; // #device-with-read +import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.PostStop; import akka.actor.typed.javadsl.AbstractBehavior; @@ -15,28 +16,42 @@ import akka.actor.typed.javadsl.Receive; import java.util.Optional; -// #device-with-read -import static jdocs.typed.tutorial_3.inprogress2.DeviceProtocol.*; -/* -//#device-with-read -import static com.lightbend.akka.sample.DeviceProtocol.*; -//#device-with-read -*/ -// #device-with-read +// #read-protocol-2 +public class Device extends AbstractBehavior { + public interface Command {} -public class Device extends AbstractBehavior { + public static final class ReadTemperature implements Command { + final long requestId; + final ActorRef replyTo; - public static Behavior createBehavior(String groupId, String deviceId) { + public ReadTemperature(long requestId, ActorRef replyTo) { + this.requestId = requestId; + this.replyTo = replyTo; + } + } + + public static final class RespondTemperature { + final long requestId; + final Optional value; + + public RespondTemperature(long requestId, Optional value) { + this.requestId = requestId; + this.value = value; + } + } + // #read-protocol-2 + + public static Behavior create(String groupId, String deviceId) { return Behaviors.setup(context -> new Device(context, groupId, deviceId)); } - private final ActorContext context; + private final ActorContext context; private final String groupId; private final String deviceId; private Optional lastTemperatureReading = Optional.empty(); - public Device(ActorContext context, String groupId, String deviceId) { + private Device(ActorContext context, String groupId, String deviceId) { this.context = context; this.groupId = groupId; this.deviceId = deviceId; @@ -45,22 +60,24 @@ public class Device extends AbstractBehavior { } @Override - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() - .onMessage(ReadTemperature.class, this::readTemperature) - .onSignal(PostStop.class, signal -> postStop()) + .onMessage(ReadTemperature.class, this::onReadTemperature) + .onSignal(PostStop.class, signal -> onPostStop()) .build(); } - private Behavior readTemperature(ReadTemperature r) { + private Behavior onReadTemperature(ReadTemperature r) { r.replyTo.tell(new RespondTemperature(r.requestId, lastTemperatureReading)); return this; } - private Device postStop() { + private Device onPostStop() { context.getLog().info("Device actor {}-{} stopped", groupId, deviceId); return this; } + // #read-protocol-2 } +// #read-protocol-2 // #device-with-read diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/DeviceProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/DeviceProtocol.java deleted file mode 100644 index 483ecc0428..0000000000 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress2/DeviceProtocol.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2018-2019 Lightbend Inc. - */ - -package jdocs.typed.tutorial_3.inprogress2; - -import akka.actor.typed.ActorRef; - -import java.util.Optional; - -// #read-protocol-2 -abstract class DeviceProtocol { - // no instances of DeviceProtocol class - private DeviceProtocol() {} - - interface DeviceMessage {} - - public static final class ReadTemperature implements DeviceMessage { - final long requestId; - final ActorRef replyTo; - - public ReadTemperature(long requestId, ActorRef replyTo) { - this.requestId = requestId; - this.replyTo = replyTo; - } - } - - public static final class RespondTemperature { - final long requestId; - final Optional value; - - public RespondTemperature(long requestId, Optional value) { - this.requestId = requestId; - this.value = value; - } - } -} -// #read-protocol-2 diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/DeviceProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/Device.java similarity index 50% rename from akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/DeviceProtocol.java rename to akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/Device.java index 1dd2dffbc0..084eafa012 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/DeviceProtocol.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_3/inprogress3/Device.java @@ -4,18 +4,12 @@ package jdocs.typed.tutorial_3.inprogress3; -import akka.actor.typed.ActorRef; +public class Device { -import java.util.Optional; - -abstract class DeviceProtocol { - // no instances of DeviceProtocol class - private DeviceProtocol() {} - - interface DeviceMessage {} + public interface Command {} // #write-protocol-1 - public static final class RecordTemperature implements DeviceMessage { + public static final class RecordTemperature implements Command { final double value; public RecordTemperature(double value) { diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/Device.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/Device.java index ae4fa714d5..59abd8bc6b 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/Device.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_4/Device.java @@ -8,6 +8,7 @@ package jdocs.typed.tutorial_4; import java.util.Optional; +import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.PostStop; import akka.actor.typed.javadsl.AbstractBehavior; @@ -15,28 +16,67 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; -// #device-with-passivate -import static jdocs.typed.tutorial_4.DeviceProtocol.*; -/* -//#device-with-passivate -import static com.lightbend.akka.sample.DeviceProtocol.*; -//#device-with-passivate -*/ -// #device-with-passivate +public class Device extends AbstractBehavior { -public class Device extends AbstractBehavior { + public interface Command {} - public static Behavior createBehavior(String groupId, String deviceId) { + public static final class RecordTemperature implements Command { + final long requestId; + final double value; + final ActorRef replyTo; + + public RecordTemperature(long requestId, double value, ActorRef replyTo) { + this.requestId = requestId; + this.value = value; + this.replyTo = replyTo; + } + } + + public static final class TemperatureRecorded { + final long requestId; + + public TemperatureRecorded(long requestId) { + this.requestId = requestId; + } + } + + public static final class ReadTemperature implements Command { + final long requestId; + final ActorRef replyTo; + + public ReadTemperature(long requestId, ActorRef replyTo) { + this.requestId = requestId; + this.replyTo = replyTo; + } + } + + public static final class RespondTemperature { + final long requestId; + final Optional value; + + public RespondTemperature(long requestId, Optional value) { + this.requestId = requestId; + this.value = value; + } + } + + // #passivate-msg + static enum Passivate implements Command { + INSTANCE + } + // #passivate-msg + + public static Behavior create(String groupId, String deviceId) { return Behaviors.setup(context -> new Device(context, groupId, deviceId)); } - private final ActorContext context; + private final ActorContext context; private final String groupId; private final String deviceId; private Optional lastTemperatureReading = Optional.empty(); - public Device(ActorContext context, String groupId, String deviceId) { + private Device(ActorContext context, String groupId, String deviceId) { this.context = context; this.groupId = groupId; this.deviceId = deviceId; @@ -45,28 +85,28 @@ public class Device extends AbstractBehavior { } @Override - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() - .onMessage(RecordTemperature.class, this::recordTemperature) - .onMessage(ReadTemperature.class, this::readTemperature) + .onMessage(RecordTemperature.class, this::onRecordTemperature) + .onMessage(ReadTemperature.class, this::onReadTemperature) .onMessage(Passivate.class, m -> Behaviors.stopped()) - .onSignal(PostStop.class, signal -> postStop()) + .onSignal(PostStop.class, signal -> onPostStop()) .build(); } - private Behavior recordTemperature(RecordTemperature r) { + private Behavior onRecordTemperature(RecordTemperature r) { context.getLog().info("Recorded temperature reading {} with {}", r.value, r.requestId); lastTemperatureReading = Optional.of(r.value); r.replyTo.tell(new TemperatureRecorded(r.requestId)); return this; } - private Behavior readTemperature(ReadTemperature r) { + private Behavior onReadTemperature(ReadTemperature r) { r.replyTo.tell(new RespondTemperature(r.requestId, lastTemperatureReading)); return this; } - private Behavior postStop() { + private Behavior onPostStop() { context.getLog().info("Device actor {}-{} stopped", groupId, deviceId); return Behaviors.stopped(); } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroup.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroup.java index 5285e09d97..2170a6cb47 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroup.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroup.java @@ -15,26 +15,20 @@ import akka.actor.typed.javadsl.Receive; import java.util.HashMap; import java.util.Map; -import static jdocs.typed.tutorial_4.DeviceManagerProtocol.*; -import static jdocs.typed.tutorial_4.DeviceProtocol.DeviceMessage; - // #device-group-full // #device-group-remove // #device-group-register -public class DeviceGroup extends AbstractBehavior { +public class DeviceGroup extends AbstractBehavior { - public static Behavior createBehavior(String groupId) { - return Behaviors.setup(context -> new DeviceGroup(context, groupId)); - } + public interface Command {} // #device-terminated - private class DeviceTerminated implements DeviceGroupMessage { - public final ActorRef device; + private class DeviceTerminated implements Command { + public final ActorRef device; public final String groupId; public final String deviceId; - DeviceTerminated( - ActorRef device, String groupId, String deviceId) { + DeviceTerminated(ActorRef device, String groupId, String deviceId) { this.device = device; this.groupId = groupId; this.deviceId = deviceId; @@ -42,32 +36,35 @@ public class DeviceGroup extends AbstractBehavior { } // #device-terminated - private final ActorContext context; - private final String groupId; - private final Map> deviceIdToActor = new HashMap<>(); + public static Behavior create(String groupId) { + return Behaviors.setup(context -> new DeviceGroup(context, groupId)); + } - public DeviceGroup(ActorContext context, String groupId) { + private final ActorContext context; + private final String groupId; + private final Map> deviceIdToActor = new HashMap<>(); + + private DeviceGroup(ActorContext context, String groupId) { this.context = context; this.groupId = groupId; context.getLog().info("DeviceGroup {} started", groupId); } - private DeviceGroup onTrackDevice(RequestTrackDevice trackMsg) { + private DeviceGroup onTrackDevice(DeviceManager.RequestTrackDevice trackMsg) { if (this.groupId.equals(trackMsg.groupId)) { - ActorRef deviceActor = deviceIdToActor.get(trackMsg.deviceId); + ActorRef deviceActor = deviceIdToActor.get(trackMsg.deviceId); if (deviceActor != null) { - trackMsg.replyTo.tell(new DeviceRegistered(deviceActor)); + trackMsg.replyTo.tell(new DeviceManager.DeviceRegistered(deviceActor)); } else { context.getLog().info("Creating device actor for {}", trackMsg.deviceId); deviceActor = - context.spawn( - Device.createBehavior(groupId, trackMsg.deviceId), "device-" + trackMsg.deviceId); + context.spawn(Device.create(groupId, trackMsg.deviceId), "device-" + trackMsg.deviceId); // #device-group-register context.watchWith( deviceActor, new DeviceTerminated(deviceActor, groupId, trackMsg.deviceId)); // #device-group-register deviceIdToActor.put(trackMsg.deviceId, deviceActor); - trackMsg.replyTo.tell(new DeviceRegistered(deviceActor)); + trackMsg.replyTo.tell(new DeviceManager.DeviceRegistered(deviceActor)); } } else { context @@ -83,8 +80,8 @@ public class DeviceGroup extends AbstractBehavior { // #device-group-register // #device-group-remove - private DeviceGroup onDeviceList(RequestDeviceList r) { - r.replyTo.tell(new ReplyDeviceList(r.requestId, deviceIdToActor.keySet())); + private DeviceGroup onDeviceList(DeviceManager.RequestDeviceList r) { + r.replyTo.tell(new DeviceManager.ReplyDeviceList(r.requestId, deviceIdToActor.keySet())); return this; } // #device-group-remove @@ -97,20 +94,23 @@ public class DeviceGroup extends AbstractBehavior { // #device-group-register @Override - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() - .onMessage(RequestTrackDevice.class, this::onTrackDevice) + .onMessage(DeviceManager.RequestTrackDevice.class, this::onTrackDevice) // #device-group-register // #device-group-remove - .onMessage(RequestDeviceList.class, r -> r.groupId.equals(groupId), this::onDeviceList) + .onMessage( + DeviceManager.RequestDeviceList.class, + r -> r.groupId.equals(groupId), + this::onDeviceList) // #device-group-remove .onMessage(DeviceTerminated.class, this::onTerminated) - .onSignal(PostStop.class, signal -> postStop()) + .onSignal(PostStop.class, signal -> onPostStop()) // #device-group-register .build(); } - private DeviceGroup postStop() { + private DeviceGroup onPostStop() { context.getLog().info("DeviceGroup {} stopped", groupId); return this; } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroupTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroupTest.java index 48cab29d19..2b93ebe5a6 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroupTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceGroupTest.java @@ -16,8 +16,11 @@ import java.util.stream.Stream; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static jdocs.typed.tutorial_4.DeviceManagerProtocol.*; -import static jdocs.typed.tutorial_4.DeviceProtocol.*; + +import static jdocs.typed.tutorial_4.DeviceManager.DeviceRegistered; +import static jdocs.typed.tutorial_4.DeviceManager.RequestTrackDevice; +import static jdocs.typed.tutorial_4.DeviceManager.ReplyDeviceList; +import static jdocs.typed.tutorial_4.DeviceManager.RequestDeviceList; public class DeviceGroupTest extends JUnitSuite { @@ -27,7 +30,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testReplyToRegistrationRequests() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device", probe.getRef())); DeviceRegistered registered1 = probe.receiveMessage(); @@ -38,17 +41,18 @@ public class DeviceGroupTest extends JUnitSuite { assertNotEquals(registered1.device, registered2.device); // Check that the device actors are working - TestProbe recordProbe = testKit.createTestProbe(TemperatureRecorded.class); - registered1.device.tell(new RecordTemperature(0L, 1.0, recordProbe.getRef())); + TestProbe recordProbe = + testKit.createTestProbe(Device.TemperatureRecorded.class); + registered1.device.tell(new Device.RecordTemperature(0L, 1.0, recordProbe.getRef())); assertEquals(0L, recordProbe.receiveMessage().requestId); - registered2.device.tell(new RecordTemperature(1L, 2.0, recordProbe.getRef())); + registered2.device.tell(new Device.RecordTemperature(1L, 2.0, recordProbe.getRef())); assertEquals(1L, recordProbe.receiveMessage().requestId); } @Test public void testIgnoreWrongRegistrationRequests() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("wrongGroup", "device1", probe.getRef())); probe.expectNoMessage(); } @@ -58,7 +62,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testReturnSameActorForSameDeviceId() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device", probe.getRef())); DeviceRegistered registered1 = probe.receiveMessage(); @@ -74,7 +78,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testListActiveDevices() { TestProbe registeredProbe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device1", registeredProbe.getRef())); registeredProbe.receiveMessage(); @@ -93,7 +97,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testListActiveDevicesAfterOneShutsDown() { TestProbe registeredProbe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device1", registeredProbe.getRef())); DeviceRegistered registered1 = registeredProbe.receiveMessage(); @@ -101,7 +105,7 @@ public class DeviceGroupTest extends JUnitSuite { groupActor.tell(new RequestTrackDevice("group", "device2", registeredProbe.getRef())); DeviceRegistered registered2 = registeredProbe.receiveMessage(); - ActorRef toShutDown = registered1.device; + ActorRef toShutDown = registered1.device; TestProbe deviceListProbe = testKit.createTestProbe(ReplyDeviceList.class); @@ -110,7 +114,7 @@ public class DeviceGroupTest extends JUnitSuite { assertEquals(0L, reply.requestId); assertEquals(Stream.of("device1", "device2").collect(Collectors.toSet()), reply.ids); - toShutDown.tell(Passivate.INSTANCE); + toShutDown.tell(Device.Passivate.INSTANCE); registeredProbe.expectTerminated(toShutDown, registeredProbe.getRemainingOrDefault()); // using awaitAssert to retry because it might take longer for the groupActor diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManager.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManager.java index eb024f1fad..d85dc27b50 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManager.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManager.java @@ -15,17 +15,62 @@ import akka.actor.typed.javadsl.Receive; import java.util.Collections; import java.util.HashMap; import java.util.Map; - -import static jdocs.typed.tutorial_4.DeviceManagerProtocol.*; +import java.util.Set; // #device-manager-full -public class DeviceManager extends AbstractBehavior { +// #device-registration-msgs +public class DeviceManager extends AbstractBehavior { - public static Behavior createBehavior() { - return Behaviors.setup(DeviceManager::new); + public interface Command {} + + public static final class RequestTrackDevice + implements DeviceManager.Command, DeviceGroup.Command { + public final String groupId; + public final String deviceId; + public final ActorRef replyTo; + + public RequestTrackDevice(String groupId, String deviceId, ActorRef replyTo) { + this.groupId = groupId; + this.deviceId = deviceId; + this.replyTo = replyTo; + } } - private static class DeviceGroupTerminated implements DeviceManagerMessage { + public static final class DeviceRegistered { + public final ActorRef device; + + public DeviceRegistered(ActorRef device) { + this.device = device; + } + } + // #device-registration-msgs + + // #device-list-msgs + public static final class RequestDeviceList + implements DeviceManager.Command, DeviceGroup.Command { + final long requestId; + final String groupId; + final ActorRef replyTo; + + public RequestDeviceList(long requestId, String groupId, ActorRef replyTo) { + this.requestId = requestId; + this.groupId = groupId; + this.replyTo = replyTo; + } + } + + public static final class ReplyDeviceList { + final long requestId; + final Set ids; + + public ReplyDeviceList(long requestId, Set ids) { + this.requestId = requestId; + this.ids = ids; + } + } + // #device-list-msgs + + private static class DeviceGroupTerminated implements DeviceManager.Command { public final String groupId; DeviceGroupTerminated(String groupId) { @@ -33,23 +78,27 @@ public class DeviceManager extends AbstractBehavior { } } - private final ActorContext context; - private final Map> groupIdToActor = new HashMap<>(); + public static Behavior create() { + return Behaviors.setup(DeviceManager::new); + } - public DeviceManager(ActorContext context) { + private final ActorContext context; + private final Map> groupIdToActor = new HashMap<>(); + + private DeviceManager(ActorContext context) { this.context = context; context.getLog().info("DeviceManager started"); } private DeviceManager onTrackDevice(RequestTrackDevice trackMsg) { String groupId = trackMsg.groupId; - ActorRef ref = groupIdToActor.get(groupId); + ActorRef ref = groupIdToActor.get(groupId); if (ref != null) { ref.tell(trackMsg); } else { context.getLog().info("Creating device group actor for {}", groupId); - ActorRef groupActor = - context.spawn(DeviceGroup.createBehavior(groupId), "group-" + groupId); + ActorRef groupActor = + context.spawn(DeviceGroup.create(groupId), "group-" + groupId); context.watchWith(groupActor, new DeviceGroupTerminated(groupId)); groupActor.tell(trackMsg); groupIdToActor.put(groupId, groupActor); @@ -58,7 +107,7 @@ public class DeviceManager extends AbstractBehavior { } private DeviceManager onRequestDeviceList(RequestDeviceList request) { - ActorRef ref = groupIdToActor.get(request.groupId); + ActorRef ref = groupIdToActor.get(request.groupId); if (ref != null) { ref.tell(request); } else { @@ -73,18 +122,20 @@ public class DeviceManager extends AbstractBehavior { return this; } - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() .onMessage(RequestTrackDevice.class, this::onTrackDevice) .onMessage(RequestDeviceList.class, this::onRequestDeviceList) .onMessage(DeviceGroupTerminated.class, this::onTerminated) - .onSignal(PostStop.class, signal -> postStop()) + .onSignal(PostStop.class, signal -> onPostStop()) .build(); } - private DeviceManager postStop() { + private DeviceManager onPostStop() { context.getLog().info("DeviceManager stopped"); return this; } + // #device-registration-msgs } +// #device-registration-msgs // #device-manager-full diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerProtocol.java deleted file mode 100644 index b9a0628dde..0000000000 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerProtocol.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2009-2019 Lightbend Inc. - */ - -package jdocs.typed.tutorial_4; - -import akka.actor.typed.ActorRef; - -import java.util.Set; - -// #device-registration-msgs -abstract class DeviceManagerProtocol { - // no instances of DeviceManagerProtocol class - private DeviceManagerProtocol() {} - - interface DeviceManagerMessage {} - - interface DeviceGroupMessage {} - - public static final class RequestTrackDevice implements DeviceManagerMessage, DeviceGroupMessage { - public final String groupId; - public final String deviceId; - public final ActorRef replyTo; - - public RequestTrackDevice(String groupId, String deviceId, ActorRef replyTo) { - this.groupId = groupId; - this.deviceId = deviceId; - this.replyTo = replyTo; - } - } - - public static final class DeviceRegistered { - public final ActorRef device; - - public DeviceRegistered(ActorRef device) { - this.device = device; - } - } - // #device-registration-msgs - - // #device-list-msgs - public static final class RequestDeviceList implements DeviceManagerMessage, DeviceGroupMessage { - final long requestId; - final String groupId; - final ActorRef replyTo; - - public RequestDeviceList(long requestId, String groupId, ActorRef replyTo) { - this.requestId = requestId; - this.groupId = groupId; - this.replyTo = replyTo; - } - } - - public static final class ReplyDeviceList { - final long requestId; - final Set ids; - - public ReplyDeviceList(long requestId, Set ids) { - this.requestId = requestId; - this.ids = ids; - } - } - // #device-list-msgs - - // #device-registration-msgs -} -// #device-registration-msgs diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerTest.java index e395e93538..1ce05d756f 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceManagerTest.java @@ -11,9 +11,11 @@ import org.junit.ClassRule; import org.junit.Test; import org.scalatest.junit.JUnitSuite; -import static jdocs.typed.tutorial_4.DeviceManagerProtocol.*; import static org.junit.Assert.assertNotEquals; +import static jdocs.typed.tutorial_4.DeviceManager.DeviceRegistered; +import static jdocs.typed.tutorial_4.DeviceManager.RequestTrackDevice; + public class DeviceManagerTest extends JUnitSuite { @ClassRule public static final TestKitJunitResource testKit = new TestKitJunitResource(); @@ -21,7 +23,7 @@ public class DeviceManagerTest extends JUnitSuite { @Test public void testReplyToRegistrationRequests() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef managerActor = testKit.spawn(DeviceManager.createBehavior()); + ActorRef managerActor = testKit.spawn(DeviceManager.create()); managerActor.tell(new RequestTrackDevice("group1", "device", probe.getRef())); DeviceRegistered registered1 = probe.receiveMessage(); diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceProtocol.java deleted file mode 100644 index e760c52d53..0000000000 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceProtocol.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2009-2019 Lightbend Inc. - */ - -package jdocs.typed.tutorial_4; - -import akka.actor.typed.ActorRef; - -import java.util.Optional; -import java.util.Set; - -abstract class DeviceProtocol { - // no instances of DeviceProtocol class - private DeviceProtocol() {} - - interface DeviceMessage {} - - public static final class RecordTemperature implements DeviceMessage { - final long requestId; - final double value; - final ActorRef replyTo; - - public RecordTemperature(long requestId, double value, ActorRef replyTo) { - this.requestId = requestId; - this.value = value; - this.replyTo = replyTo; - } - } - - public static final class TemperatureRecorded { - final long requestId; - - public TemperatureRecorded(long requestId) { - this.requestId = requestId; - } - } - - public static final class ReadTemperature implements DeviceMessage { - final long requestId; - final ActorRef replyTo; - - public ReadTemperature(long requestId, ActorRef replyTo) { - this.requestId = requestId; - this.replyTo = replyTo; - } - } - - public static final class RespondTemperature { - final long requestId; - final Optional value; - - public RespondTemperature(long requestId, Optional value) { - this.requestId = requestId; - this.value = value; - } - } - - // #passivate-msg - static enum Passivate implements DeviceMessage { - INSTANCE - } - // #passivate-msg - -} diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceTest.java index ccbd967e92..5cf49965c1 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_4/DeviceTest.java @@ -14,7 +14,6 @@ import org.scalatest.junit.JUnitSuite; import java.util.Optional; import static org.junit.Assert.assertEquals; -import static jdocs.typed.tutorial_4.DeviceProtocol.*; public class DeviceTest extends JUnitSuite { @@ -23,10 +22,11 @@ public class DeviceTest extends JUnitSuite { // #device-read-test @Test public void testReplyWithEmptyReadingIfNoTemperatureIsKnown() { - TestProbe probe = testKit.createTestProbe(RespondTemperature.class); - ActorRef deviceActor = testKit.spawn(Device.createBehavior("group", "device")); - deviceActor.tell(new ReadTemperature(42L, probe.getRef())); - RespondTemperature response = probe.receiveMessage(); + TestProbe probe = + testKit.createTestProbe(Device.RespondTemperature.class); + ActorRef deviceActor = testKit.spawn(Device.create("group", "device")); + deviceActor.tell(new Device.ReadTemperature(42L, probe.getRef())); + Device.RespondTemperature response = probe.receiveMessage(); assertEquals(42L, response.requestId); assertEquals(Optional.empty(), response.value); } @@ -35,23 +35,25 @@ public class DeviceTest extends JUnitSuite { // #device-write-read-test @Test public void testReplyWithLatestTemperatureReading() { - TestProbe recordProbe = testKit.createTestProbe(TemperatureRecorded.class); - TestProbe readProbe = testKit.createTestProbe(RespondTemperature.class); - ActorRef deviceActor = testKit.spawn(Device.createBehavior("group", "device")); + TestProbe recordProbe = + testKit.createTestProbe(Device.TemperatureRecorded.class); + TestProbe readProbe = + testKit.createTestProbe(Device.RespondTemperature.class); + ActorRef deviceActor = testKit.spawn(Device.create("group", "device")); - deviceActor.tell(new RecordTemperature(1L, 24.0, recordProbe.getRef())); + deviceActor.tell(new Device.RecordTemperature(1L, 24.0, recordProbe.getRef())); assertEquals(1L, recordProbe.receiveMessage().requestId); - deviceActor.tell(new ReadTemperature(2L, readProbe.getRef())); - RespondTemperature response1 = readProbe.receiveMessage(); + deviceActor.tell(new Device.ReadTemperature(2L, readProbe.getRef())); + Device.RespondTemperature response1 = readProbe.receiveMessage(); assertEquals(2L, response1.requestId); assertEquals(Optional.of(24.0), response1.value); - deviceActor.tell(new RecordTemperature(3L, 55.0, recordProbe.getRef())); + deviceActor.tell(new Device.RecordTemperature(3L, 55.0, recordProbe.getRef())); assertEquals(3L, recordProbe.receiveMessage().requestId); - deviceActor.tell(new ReadTemperature(4L, readProbe.getRef())); - RespondTemperature response2 = readProbe.receiveMessage(); + deviceActor.tell(new Device.ReadTemperature(4L, readProbe.getRef())); + Device.RespondTemperature response2 = readProbe.receiveMessage(); assertEquals(4L, response2.requestId); assertEquals(Optional.of(55.0), response2.value); } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/Device.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/Device.java index 824ebecf14..dcd385403e 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/Device.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/Device.java @@ -6,6 +6,7 @@ package jdocs.typed.tutorial_5; import java.util.Optional; +import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.PostStop; import akka.actor.typed.javadsl.AbstractBehavior; @@ -13,21 +14,67 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; -import static jdocs.typed.tutorial_5.DeviceProtocol.*; +public class Device extends AbstractBehavior { -public class Device extends AbstractBehavior { + public interface Command {} - public static Behavior createBehavior(String groupId, String deviceId) { + public static final class RecordTemperature implements Command { + final long requestId; + final double value; + final ActorRef replyTo; + + public RecordTemperature(long requestId, double value, ActorRef replyTo) { + this.requestId = requestId; + this.value = value; + this.replyTo = replyTo; + } + } + + public static final class TemperatureRecorded { + final long requestId; + + public TemperatureRecorded(long requestId) { + this.requestId = requestId; + } + } + + public static final class ReadTemperature implements Command { + final long requestId; + final ActorRef replyTo; + + public ReadTemperature(long requestId, ActorRef replyTo) { + this.requestId = requestId; + this.replyTo = replyTo; + } + } + + public static final class RespondTemperature { + final long requestId; + final String deviceId; + final Optional value; + + public RespondTemperature(long requestId, String deviceId, Optional value) { + this.requestId = requestId; + this.deviceId = deviceId; + this.value = value; + } + } + + static enum Passivate implements Command { + INSTANCE + } + + public static Behavior create(String groupId, String deviceId) { return Behaviors.setup(context -> new Device(context, groupId, deviceId)); } - private final ActorContext context; + private final ActorContext context; private final String groupId; private final String deviceId; private Optional lastTemperatureReading = Optional.empty(); - public Device(ActorContext context, String groupId, String deviceId) { + private Device(ActorContext context, String groupId, String deviceId) { this.context = context; this.groupId = groupId; this.deviceId = deviceId; @@ -36,28 +83,28 @@ public class Device extends AbstractBehavior { } @Override - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() - .onMessage(RecordTemperature.class, this::recordTemperature) - .onMessage(ReadTemperature.class, this::readTemperature) + .onMessage(RecordTemperature.class, this::onRecordTemperature) + .onMessage(ReadTemperature.class, this::onReadTemperature) .onMessage(Passivate.class, m -> Behaviors.stopped()) - .onSignal(PostStop.class, signal -> postStop()) + .onSignal(PostStop.class, signal -> onPostStop()) .build(); } - private Behavior recordTemperature(RecordTemperature r) { + private Behavior onRecordTemperature(RecordTemperature r) { context.getLog().info("Recorded temperature reading {} with {}", r.value, r.requestId); lastTemperatureReading = Optional.of(r.value); r.replyTo.tell(new TemperatureRecorded(r.requestId)); return this; } - private Behavior readTemperature(ReadTemperature r) { + private Behavior onReadTemperature(ReadTemperature r) { r.replyTo.tell(new RespondTemperature(r.requestId, deviceId, lastTemperatureReading)); return this; } - private Behavior postStop() { + private Behavior onPostStop() { context.getLog().info("Device actor {}-{} stopped", groupId, deviceId); return Behaviors.stopped(); } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroup.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroup.java index ecd36b007b..e3b699306b 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroup.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroup.java @@ -16,54 +16,51 @@ import java.time.Duration; import java.util.HashMap; import java.util.Map; -import static jdocs.typed.tutorial_5.DeviceManagerProtocol.*; -import static jdocs.typed.tutorial_5.DeviceProtocol.DeviceMessage; - // #query-added -public class DeviceGroup extends AbstractBehavior { +public class DeviceGroup extends AbstractBehavior { - public static Behavior createBehavior(String groupId) { - return Behaviors.setup(context -> new DeviceGroup(context, groupId)); - } + public interface Command {} - private class DeviceTerminated implements DeviceGroupMessage { - public final ActorRef device; + private class DeviceTerminated implements Command { + public final ActorRef device; public final String groupId; public final String deviceId; - DeviceTerminated( - ActorRef device, String groupId, String deviceId) { + DeviceTerminated(ActorRef device, String groupId, String deviceId) { this.device = device; this.groupId = groupId; this.deviceId = deviceId; } } - private final ActorContext context; - private final String groupId; - private final Map> deviceIdToActor = new HashMap<>(); + public static Behavior create(String groupId) { + return Behaviors.setup(context -> new DeviceGroup(context, groupId)); + } - public DeviceGroup(ActorContext context, String groupId) { + private final ActorContext context; + private final String groupId; + private final Map> deviceIdToActor = new HashMap<>(); + + private DeviceGroup(ActorContext context, String groupId) { this.context = context; this.groupId = groupId; context.getLog().info("DeviceGroup {} started", groupId); } // #query-added - private DeviceGroup onTrackDevice(RequestTrackDevice trackMsg) { + private DeviceGroup onTrackDevice(DeviceManager.RequestTrackDevice trackMsg) { if (this.groupId.equals(trackMsg.groupId)) { - ActorRef deviceActor = deviceIdToActor.get(trackMsg.deviceId); + ActorRef deviceActor = deviceIdToActor.get(trackMsg.deviceId); if (deviceActor != null) { - trackMsg.replyTo.tell(new DeviceRegistered(deviceActor)); + trackMsg.replyTo.tell(new DeviceManager.DeviceRegistered(deviceActor)); } else { context.getLog().info("Creating device actor for {}", trackMsg.deviceId); deviceActor = - context.spawn( - Device.createBehavior(groupId, trackMsg.deviceId), "device-" + trackMsg.deviceId); + context.spawn(Device.create(groupId, trackMsg.deviceId), "device-" + trackMsg.deviceId); context.watchWith( deviceActor, new DeviceTerminated(deviceActor, groupId, trackMsg.deviceId)); deviceIdToActor.put(trackMsg.deviceId, deviceActor); - trackMsg.replyTo.tell(new DeviceRegistered(deviceActor)); + trackMsg.replyTo.tell(new DeviceManager.DeviceRegistered(deviceActor)); } } else { context @@ -76,8 +73,8 @@ public class DeviceGroup extends AbstractBehavior { return this; } - private DeviceGroup onDeviceList(RequestDeviceList r) { - r.replyTo.tell(new ReplyDeviceList(r.requestId, deviceIdToActor.keySet())); + private DeviceGroup onDeviceList(DeviceManager.RequestDeviceList r) { + r.replyTo.tell(new DeviceManager.ReplyDeviceList(r.requestId, deviceIdToActor.keySet())); return this; } @@ -87,14 +84,14 @@ public class DeviceGroup extends AbstractBehavior { return this; } - private DeviceGroup postStop() { + private DeviceGroup onPostStop() { context.getLog().info("DeviceGroup {} stopped", groupId); return this; } // #query-added - private DeviceGroup onAllTemperatures(RequestAllTemperatures r) { + private DeviceGroup onAllTemperatures(DeviceManager.RequestAllTemperatures r) { // since Java collections are mutable, we want to avoid sharing them between actors (since // multiple Actors (threads) // modifying the same mutable data-structure is not safe), and perform a defensive copy of the @@ -102,27 +99,32 @@ public class DeviceGroup extends AbstractBehavior { // // Feel free to use your favourite immutable data-structures library with Akka in Java // applications! - Map> deviceIdToActorCopy = new HashMap<>(this.deviceIdToActor); + Map> deviceIdToActorCopy = new HashMap<>(this.deviceIdToActor); context.spawnAnonymous( - DeviceGroupQuery.createBehavior( + DeviceGroupQuery.create( deviceIdToActorCopy, r.requestId, r.replyTo, Duration.ofSeconds(3))); return this; } @Override - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() // #query-added - .onMessage(RequestTrackDevice.class, this::onTrackDevice) - .onMessage(RequestDeviceList.class, r -> r.groupId.equals(groupId), this::onDeviceList) + .onMessage(DeviceManager.RequestTrackDevice.class, this::onTrackDevice) + .onMessage( + DeviceManager.RequestDeviceList.class, + r -> r.groupId.equals(groupId), + this::onDeviceList) .onMessage(DeviceTerminated.class, this::onTerminated) - .onSignal(PostStop.class, signal -> postStop()) + .onSignal(PostStop.class, signal -> onPostStop()) // #query-added // ... other cases omitted .onMessage( - RequestAllTemperatures.class, r -> r.groupId.equals(groupId), this::onAllTemperatures) + DeviceManager.RequestAllTemperatures.class, + r -> r.groupId.equals(groupId), + this::onAllTemperatures) .build(); } } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQuery.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQuery.java index a5bfe82441..48552da852 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQuery.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQuery.java @@ -18,16 +18,36 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import static jdocs.typed.tutorial_5.DeviceManagerProtocol.*; - // #query-full // #query-outline -public class DeviceGroupQuery extends AbstractBehavior { +public class DeviceGroupQuery extends AbstractBehavior { - public static Behavior createBehavior( - Map> deviceIdToActor, + public interface Command {} + + private static enum CollectionTimeout implements Command { + INSTANCE + } + + static class WrappedRespondTemperature implements Command { + final Device.RespondTemperature response; + + WrappedRespondTemperature(Device.RespondTemperature response) { + this.response = response; + } + } + + private static class DeviceTerminated implements Command { + final String deviceId; + + private DeviceTerminated(String deviceId) { + this.deviceId = deviceId; + } + } + + public static Behavior create( + Map> deviceIdToActor, long requestId, - ActorRef requester, + ActorRef requester, Duration timeout) { return Behaviors.setup( context -> @@ -37,56 +57,34 @@ public class DeviceGroupQuery extends AbstractBehavior deviceIdToActor, requestId, requester, timeout, context, timers))); } - private static enum CollectionTimeout implements DeviceGroupQueryMessage { - INSTANCE - } - - static class WrappedRespondTemperature implements DeviceGroupQueryMessage { - final DeviceProtocol.RespondTemperature response; - - WrappedRespondTemperature(DeviceProtocol.RespondTemperature response) { - this.response = response; - } - } - - private static class DeviceTerminated implements DeviceGroupQueryMessage { - final String deviceId; - - private DeviceTerminated(String deviceId) { - this.deviceId = deviceId; - } - } - private final long requestId; - private final ActorRef requester; + private final ActorRef requester; // #query-outline // #query-state - private Map repliesSoFar = new HashMap<>(); + private Map repliesSoFar = new HashMap<>(); private final Set stillWaiting; // #query-state // #query-outline - public DeviceGroupQuery( - Map> deviceIdToActor, + private DeviceGroupQuery( + Map> deviceIdToActor, long requestId, - ActorRef requester, + ActorRef requester, Duration timeout, - ActorContext context, - TimerScheduler timers) { + ActorContext context, + TimerScheduler timers) { this.requestId = requestId; this.requester = requester; timers.startSingleTimer(CollectionTimeout.class, CollectionTimeout.INSTANCE, timeout); - ActorRef respondTemperatureAdapter = - context.messageAdapter( - DeviceProtocol.RespondTemperature.class, WrappedRespondTemperature::new); + ActorRef respondTemperatureAdapter = + context.messageAdapter(Device.RespondTemperature.class, WrappedRespondTemperature::new); - for (Map.Entry> entry : - deviceIdToActor.entrySet()) { + for (Map.Entry> entry : deviceIdToActor.entrySet()) { context.watchWith(entry.getValue(), new DeviceTerminated(entry.getKey())); - entry.getValue().tell(new DeviceProtocol.ReadTemperature(0L, respondTemperatureAdapter)); + entry.getValue().tell(new Device.ReadTemperature(0L, respondTemperatureAdapter)); } stillWaiting = new HashSet<>(deviceIdToActor.keySet()); } @@ -94,7 +92,7 @@ public class DeviceGroupQuery extends AbstractBehavior // #query-outline // #query-state @Override - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() .onMessage(WrappedRespondTemperature.class, this::onRespondTemperature) .onMessage(DeviceTerminated.class, this::onDeviceTerminated) @@ -102,12 +100,12 @@ public class DeviceGroupQuery extends AbstractBehavior .build(); } - private Behavior onRespondTemperature(WrappedRespondTemperature r) { - TemperatureReading reading = + private Behavior onRespondTemperature(WrappedRespondTemperature r) { + DeviceManager.TemperatureReading reading = r.response .value - .map(v -> (TemperatureReading) new Temperature(v)) - .orElse(TemperatureNotAvailable.INSTANCE); + .map(v -> (DeviceManager.TemperatureReading) new DeviceManager.Temperature(v)) + .orElse(DeviceManager.TemperatureNotAvailable.INSTANCE); String deviceId = r.response.deviceId; repliesSoFar.put(deviceId, reading); @@ -116,17 +114,17 @@ public class DeviceGroupQuery extends AbstractBehavior return respondWhenAllCollected(); } - private Behavior onDeviceTerminated(DeviceTerminated terminated) { + private Behavior onDeviceTerminated(DeviceTerminated terminated) { if (stillWaiting.contains(terminated.deviceId)) { - repliesSoFar.put(terminated.deviceId, DeviceNotAvailable.INSTANCE); + repliesSoFar.put(terminated.deviceId, DeviceManager.DeviceNotAvailable.INSTANCE); stillWaiting.remove(terminated.deviceId); } return respondWhenAllCollected(); } - private Behavior onCollectionTimeout(CollectionTimeout timeout) { + private Behavior onCollectionTimeout(CollectionTimeout timeout) { for (String deviceId : stillWaiting) { - repliesSoFar.put(deviceId, DeviceTimedOut.INSTANCE); + repliesSoFar.put(deviceId, DeviceManager.DeviceTimedOut.INSTANCE); } stillWaiting.clear(); return respondWhenAllCollected(); @@ -134,9 +132,9 @@ public class DeviceGroupQuery extends AbstractBehavior // #query-state // #query-collect-reply - private Behavior respondWhenAllCollected() { + private Behavior respondWhenAllCollected() { if (stillWaiting.isEmpty()) { - requester.tell(new RespondAllTemperatures(requestId, repliesSoFar)); + requester.tell(new DeviceManager.RespondAllTemperatures(requestId, repliesSoFar)); return Behaviors.stopped(); } else { return this; diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQueryTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQueryTest.java index d7c617b4f1..5b12e08fd4 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQueryTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQueryTest.java @@ -16,10 +16,15 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; -import static jdocs.typed.tutorial_5.DeviceManagerProtocol.*; -import static jdocs.typed.tutorial_5.DeviceProtocol.*; import static org.junit.Assert.assertEquals; +import static jdocs.typed.tutorial_5.DeviceManager.RespondAllTemperatures; +import static jdocs.typed.tutorial_5.DeviceManager.TemperatureReading; +import static jdocs.typed.tutorial_5.DeviceManager.Temperature; +import static jdocs.typed.tutorial_5.DeviceManager.TemperatureNotAvailable; +import static jdocs.typed.tutorial_5.DeviceManager.DeviceTimedOut; +import static jdocs.typed.tutorial_5.DeviceManager.DeviceNotAvailable; + public class DeviceGroupQueryTest extends JUnitSuite { @ClassRule public static final TestKitJunitResource testKit = new TestKitJunitResource(); @@ -29,28 +34,28 @@ public class DeviceGroupQueryTest extends JUnitSuite { public void testReturnTemperatureValueForWorkingDevices() { TestProbe requester = testKit.createTestProbe(RespondAllTemperatures.class); - TestProbe device1 = testKit.createTestProbe(DeviceMessage.class); - TestProbe device2 = testKit.createTestProbe(DeviceMessage.class); + TestProbe device1 = testKit.createTestProbe(Device.Command.class); + TestProbe device2 = testKit.createTestProbe(Device.Command.class); - Map> deviceIdToActor = new HashMap<>(); + Map> deviceIdToActor = new HashMap<>(); deviceIdToActor.put("device1", device1.getRef()); deviceIdToActor.put("device2", device2.getRef()); - ActorRef queryActor = + ActorRef queryActor = testKit.spawn( - DeviceGroupQuery.createBehavior( + DeviceGroupQuery.create( deviceIdToActor, 1L, requester.getRef(), Duration.ofSeconds(3))); - device1.expectMessageClass(ReadTemperature.class); - device2.expectMessageClass(ReadTemperature.class); + device1.expectMessageClass(Device.ReadTemperature.class); + device2.expectMessageClass(Device.ReadTemperature.class); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device1", Optional.of(1.0)))); + new Device.RespondTemperature(0L, "device1", Optional.of(1.0)))); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device2", Optional.of(2.0)))); + new Device.RespondTemperature(0L, "device2", Optional.of(2.0)))); RespondAllTemperatures response = requester.receiveMessage(); assertEquals(1L, response.requestId); @@ -68,28 +73,28 @@ public class DeviceGroupQueryTest extends JUnitSuite { public void testReturnTemperatureNotAvailableForDevicesWithNoReadings() { TestProbe requester = testKit.createTestProbe(RespondAllTemperatures.class); - TestProbe device1 = testKit.createTestProbe(DeviceMessage.class); - TestProbe device2 = testKit.createTestProbe(DeviceMessage.class); + TestProbe device1 = testKit.createTestProbe(Device.Command.class); + TestProbe device2 = testKit.createTestProbe(Device.Command.class); - Map> deviceIdToActor = new HashMap<>(); + Map> deviceIdToActor = new HashMap<>(); deviceIdToActor.put("device1", device1.getRef()); deviceIdToActor.put("device2", device2.getRef()); - ActorRef queryActor = + ActorRef queryActor = testKit.spawn( - DeviceGroupQuery.createBehavior( + DeviceGroupQuery.create( deviceIdToActor, 1L, requester.getRef(), Duration.ofSeconds(3))); - assertEquals(0L, device1.expectMessageClass(ReadTemperature.class).requestId); - assertEquals(0L, device2.expectMessageClass(ReadTemperature.class).requestId); + assertEquals(0L, device1.expectMessageClass(Device.ReadTemperature.class).requestId); + assertEquals(0L, device2.expectMessageClass(Device.ReadTemperature.class).requestId); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device1", Optional.empty()))); + new Device.RespondTemperature(0L, "device1", Optional.empty()))); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device2", Optional.of(2.0)))); + new Device.RespondTemperature(0L, "device2", Optional.of(2.0)))); RespondAllTemperatures response = requester.receiveMessage(); assertEquals(1L, response.requestId); @@ -107,24 +112,24 @@ public class DeviceGroupQueryTest extends JUnitSuite { public void testReturnDeviceNotAvailableIfDeviceStopsBeforeAnswering() { TestProbe requester = testKit.createTestProbe(RespondAllTemperatures.class); - TestProbe device1 = testKit.createTestProbe(DeviceMessage.class); - TestProbe device2 = testKit.createTestProbe(DeviceMessage.class); + TestProbe device1 = testKit.createTestProbe(Device.Command.class); + TestProbe device2 = testKit.createTestProbe(Device.Command.class); - Map> deviceIdToActor = new HashMap<>(); + Map> deviceIdToActor = new HashMap<>(); deviceIdToActor.put("device1", device1.getRef()); deviceIdToActor.put("device2", device2.getRef()); - ActorRef queryActor = + ActorRef queryActor = testKit.spawn( - DeviceGroupQuery.createBehavior( + DeviceGroupQuery.create( deviceIdToActor, 1L, requester.getRef(), Duration.ofSeconds(3))); - assertEquals(0L, device1.expectMessageClass(ReadTemperature.class).requestId); - assertEquals(0L, device2.expectMessageClass(ReadTemperature.class).requestId); + assertEquals(0L, device1.expectMessageClass(Device.ReadTemperature.class).requestId); + assertEquals(0L, device2.expectMessageClass(Device.ReadTemperature.class).requestId); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device1", Optional.of(1.0)))); + new Device.RespondTemperature(0L, "device1", Optional.of(1.0)))); device2.stop(); @@ -144,28 +149,28 @@ public class DeviceGroupQueryTest extends JUnitSuite { public void testReturnTemperatureReadingEvenIfDeviceStopsAfterAnswering() { TestProbe requester = testKit.createTestProbe(RespondAllTemperatures.class); - TestProbe device1 = testKit.createTestProbe(DeviceMessage.class); - TestProbe device2 = testKit.createTestProbe(DeviceMessage.class); + TestProbe device1 = testKit.createTestProbe(Device.Command.class); + TestProbe device2 = testKit.createTestProbe(Device.Command.class); - Map> deviceIdToActor = new HashMap<>(); + Map> deviceIdToActor = new HashMap<>(); deviceIdToActor.put("device1", device1.getRef()); deviceIdToActor.put("device2", device2.getRef()); - ActorRef queryActor = + ActorRef queryActor = testKit.spawn( - DeviceGroupQuery.createBehavior( + DeviceGroupQuery.create( deviceIdToActor, 1L, requester.getRef(), Duration.ofSeconds(3))); - assertEquals(0L, device1.expectMessageClass(ReadTemperature.class).requestId); - assertEquals(0L, device2.expectMessageClass(ReadTemperature.class).requestId); + assertEquals(0L, device1.expectMessageClass(Device.ReadTemperature.class).requestId); + assertEquals(0L, device2.expectMessageClass(Device.ReadTemperature.class).requestId); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device1", Optional.of(1.0)))); + new Device.RespondTemperature(0L, "device1", Optional.of(1.0)))); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device2", Optional.of(2.0)))); + new Device.RespondTemperature(0L, "device2", Optional.of(2.0)))); device2.stop(); @@ -185,24 +190,24 @@ public class DeviceGroupQueryTest extends JUnitSuite { public void testReturnDeviceTimedOutIfDeviceDoesNotAnswerInTime() { TestProbe requester = testKit.createTestProbe(RespondAllTemperatures.class); - TestProbe device1 = testKit.createTestProbe(DeviceMessage.class); - TestProbe device2 = testKit.createTestProbe(DeviceMessage.class); + TestProbe device1 = testKit.createTestProbe(Device.Command.class); + TestProbe device2 = testKit.createTestProbe(Device.Command.class); - Map> deviceIdToActor = new HashMap<>(); + Map> deviceIdToActor = new HashMap<>(); deviceIdToActor.put("device1", device1.getRef()); deviceIdToActor.put("device2", device2.getRef()); - ActorRef queryActor = + ActorRef queryActor = testKit.spawn( - DeviceGroupQuery.createBehavior( + DeviceGroupQuery.create( deviceIdToActor, 1L, requester.getRef(), Duration.ofMillis(200))); - assertEquals(0L, device1.expectMessageClass(ReadTemperature.class).requestId); - assertEquals(0L, device2.expectMessageClass(ReadTemperature.class).requestId); + assertEquals(0L, device1.expectMessageClass(Device.ReadTemperature.class).requestId); + assertEquals(0L, device2.expectMessageClass(Device.ReadTemperature.class).requestId); queryActor.tell( new DeviceGroupQuery.WrappedRespondTemperature( - new RespondTemperature(0L, "device1", Optional.of(1.0)))); + new Device.RespondTemperature(0L, "device1", Optional.of(1.0)))); // no reply from device2 diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupTest.java index 15df4ed7dc..a425d88f04 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupTest.java @@ -16,11 +16,19 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; -import static jdocs.typed.tutorial_5.DeviceManagerProtocol.*; -import static jdocs.typed.tutorial_5.DeviceProtocol.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static jdocs.typed.tutorial_5.DeviceManager.RespondAllTemperatures; +import static jdocs.typed.tutorial_5.DeviceManager.TemperatureReading; +import static jdocs.typed.tutorial_5.DeviceManager.Temperature; +import static jdocs.typed.tutorial_5.DeviceManager.TemperatureNotAvailable; +import static jdocs.typed.tutorial_5.DeviceManager.DeviceRegistered; +import static jdocs.typed.tutorial_5.DeviceManager.RequestTrackDevice; +import static jdocs.typed.tutorial_5.DeviceManager.ReplyDeviceList; +import static jdocs.typed.tutorial_5.DeviceManager.RequestDeviceList; +import static jdocs.typed.tutorial_5.DeviceManager.RequestAllTemperatures; + public class DeviceGroupTest extends JUnitSuite { @ClassRule public static final TestKitJunitResource testKit = new TestKitJunitResource(); @@ -28,7 +36,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testReplyToRegistrationRequests() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device", probe.getRef())); DeviceRegistered registered1 = probe.receiveMessage(); @@ -39,17 +47,18 @@ public class DeviceGroupTest extends JUnitSuite { assertNotEquals(registered1.device, registered2.device); // Check that the device actors are working - TestProbe recordProbe = testKit.createTestProbe(TemperatureRecorded.class); - registered1.device.tell(new RecordTemperature(0L, 1.0, recordProbe.getRef())); + TestProbe recordProbe = + testKit.createTestProbe(Device.TemperatureRecorded.class); + registered1.device.tell(new Device.RecordTemperature(0L, 1.0, recordProbe.getRef())); assertEquals(0L, recordProbe.receiveMessage().requestId); - registered2.device.tell(new RecordTemperature(1L, 2.0, recordProbe.getRef())); + registered2.device.tell(new Device.RecordTemperature(1L, 2.0, recordProbe.getRef())); assertEquals(1L, recordProbe.receiveMessage().requestId); } @Test public void testIgnoreWrongRegistrationRequests() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("wrongGroup", "device1", probe.getRef())); probe.expectNoMessage(); } @@ -57,7 +66,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testReturnSameActorForSameDeviceId() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device", probe.getRef())); DeviceRegistered registered1 = probe.receiveMessage(); @@ -71,7 +80,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testListActiveDevices() { TestProbe registeredProbe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device1", registeredProbe.getRef())); registeredProbe.receiveMessage(); @@ -90,7 +99,7 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testListActiveDevicesAfterOneShutsDown() { TestProbe registeredProbe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device1", registeredProbe.getRef())); DeviceRegistered registered1 = registeredProbe.receiveMessage(); @@ -98,7 +107,7 @@ public class DeviceGroupTest extends JUnitSuite { groupActor.tell(new RequestTrackDevice("group", "device2", registeredProbe.getRef())); registeredProbe.receiveMessage(); - ActorRef toShutDown = registered1.device; + ActorRef toShutDown = registered1.device; TestProbe deviceListProbe = testKit.createTestProbe(ReplyDeviceList.class); @@ -107,7 +116,7 @@ public class DeviceGroupTest extends JUnitSuite { assertEquals(0L, reply.requestId); assertEquals(Stream.of("device1", "device2").collect(Collectors.toSet()), reply.ids); - toShutDown.tell(Passivate.INSTANCE); + toShutDown.tell(Device.Passivate.INSTANCE); registeredProbe.expectTerminated(toShutDown, registeredProbe.getRemainingOrDefault()); // using awaitAssert to retry because it might take longer for the groupActor @@ -126,22 +135,23 @@ public class DeviceGroupTest extends JUnitSuite { @Test public void testCollectTemperaturesFromAllActiveDevices() { TestProbe registeredProbe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef groupActor = testKit.spawn(DeviceGroup.createBehavior("group")); + ActorRef groupActor = testKit.spawn(DeviceGroup.create("group")); groupActor.tell(new RequestTrackDevice("group", "device1", registeredProbe.getRef())); - ActorRef deviceActor1 = registeredProbe.receiveMessage().device; + ActorRef deviceActor1 = registeredProbe.receiveMessage().device; groupActor.tell(new RequestTrackDevice("group", "device2", registeredProbe.getRef())); - ActorRef deviceActor2 = registeredProbe.receiveMessage().device; + ActorRef deviceActor2 = registeredProbe.receiveMessage().device; groupActor.tell(new RequestTrackDevice("group", "device3", registeredProbe.getRef())); - ActorRef deviceActor3 = registeredProbe.receiveMessage().device; + ActorRef deviceActor3 = registeredProbe.receiveMessage().device; // Check that the device actors are working - TestProbe recordProbe = testKit.createTestProbe(TemperatureRecorded.class); - deviceActor1.tell(new RecordTemperature(0L, 1.0, recordProbe.getRef())); + TestProbe recordProbe = + testKit.createTestProbe(Device.TemperatureRecorded.class); + deviceActor1.tell(new Device.RecordTemperature(0L, 1.0, recordProbe.getRef())); assertEquals(0L, recordProbe.receiveMessage().requestId); - deviceActor2.tell(new RecordTemperature(1L, 2.0, recordProbe.getRef())); + deviceActor2.tell(new Device.RecordTemperature(1L, 2.0, recordProbe.getRef())); assertEquals(1L, recordProbe.receiveMessage().requestId); // No temperature for device 3 diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManager.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManager.java index 8ab7c876ed..b6713024d7 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManager.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManager.java @@ -15,16 +15,128 @@ import akka.actor.typed.javadsl.Receive; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Set; -import static jdocs.typed.tutorial_5.DeviceManagerProtocol.*; +public class DeviceManager extends AbstractBehavior { -public class DeviceManager extends AbstractBehavior { + public interface Command {} - public static Behavior createBehavior() { - return Behaviors.setup(DeviceManager::new); + public static final class RequestTrackDevice + implements DeviceManager.Command, DeviceGroup.Command { + public final String groupId; + public final String deviceId; + public final ActorRef replyTo; + + public RequestTrackDevice(String groupId, String deviceId, ActorRef replyTo) { + this.groupId = groupId; + this.deviceId = deviceId; + this.replyTo = replyTo; + } } - private static class DeviceGroupTerminated implements DeviceManagerMessage { + public static final class DeviceRegistered { + public final ActorRef device; + + public DeviceRegistered(ActorRef device) { + this.device = device; + } + } + + public static final class RequestDeviceList + implements DeviceManager.Command, DeviceGroup.Command { + final long requestId; + final String groupId; + final ActorRef replyTo; + + public RequestDeviceList(long requestId, String groupId, ActorRef replyTo) { + this.requestId = requestId; + this.groupId = groupId; + this.replyTo = replyTo; + } + } + + public static final class ReplyDeviceList { + final long requestId; + final Set ids; + + public ReplyDeviceList(long requestId, Set ids) { + this.requestId = requestId; + this.ids = ids; + } + } + + // #query-protocol + + public static final class RequestAllTemperatures + implements DeviceGroupQuery.Command, DeviceGroup.Command, Command { + + final long requestId; + final String groupId; + final ActorRef replyTo; + + public RequestAllTemperatures( + long requestId, String groupId, ActorRef replyTo) { + this.requestId = requestId; + this.groupId = groupId; + this.replyTo = replyTo; + } + } + + public static final class RespondAllTemperatures { + final long requestId; + final Map temperatures; + + public RespondAllTemperatures(long requestId, Map temperatures) { + this.requestId = requestId; + this.temperatures = temperatures; + } + } + + public interface TemperatureReading {} + + public static final class Temperature implements TemperatureReading { + public final double value; + + public Temperature(double value) { + this.value = value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Temperature that = (Temperature) o; + + return Double.compare(that.value, value) == 0; + } + + @Override + public int hashCode() { + long temp = Double.doubleToLongBits(value); + return (int) (temp ^ (temp >>> 32)); + } + + @Override + public String toString() { + return "Temperature{" + "value=" + value + '}'; + } + } + + public enum TemperatureNotAvailable implements TemperatureReading { + INSTANCE + } + + public enum DeviceNotAvailable implements TemperatureReading { + INSTANCE + } + + public enum DeviceTimedOut implements TemperatureReading { + INSTANCE + } + // #query-protocol + + private static class DeviceGroupTerminated implements DeviceManager.Command { public final String groupId; DeviceGroupTerminated(String groupId) { @@ -32,23 +144,27 @@ public class DeviceManager extends AbstractBehavior { } } - private final ActorContext context; - private final Map> groupIdToActor = new HashMap<>(); + private final ActorContext context; + private final Map> groupIdToActor = new HashMap<>(); - public DeviceManager(ActorContext context) { + public static Behavior create() { + return Behaviors.setup(DeviceManager::new); + } + + private DeviceManager(ActorContext context) { this.context = context; context.getLog().info("DeviceManager started"); } private DeviceManager onTrackDevice(RequestTrackDevice trackMsg) { String groupId = trackMsg.groupId; - ActorRef ref = groupIdToActor.get(groupId); + ActorRef ref = groupIdToActor.get(groupId); if (ref != null) { ref.tell(trackMsg); } else { context.getLog().info("Creating device group actor for {}", groupId); - ActorRef groupActor = - context.spawn(DeviceGroup.createBehavior(groupId), "group-" + groupId); + ActorRef groupActor = + context.spawn(DeviceGroup.create(groupId), "group-" + groupId); context.watchWith(groupActor, new DeviceGroupTerminated(groupId)); groupActor.tell(trackMsg); groupIdToActor.put(groupId, groupActor); @@ -57,7 +173,7 @@ public class DeviceManager extends AbstractBehavior { } private DeviceManager onRequestDeviceList(RequestDeviceList request) { - ActorRef ref = groupIdToActor.get(request.groupId); + ActorRef ref = groupIdToActor.get(request.groupId); if (ref != null) { ref.tell(request); } else { @@ -67,7 +183,7 @@ public class DeviceManager extends AbstractBehavior { } private DeviceManager onRequestAllTemperatures(RequestAllTemperatures request) { - ActorRef ref = groupIdToActor.get(request.groupId); + ActorRef ref = groupIdToActor.get(request.groupId); if (ref != null) { ref.tell(request); } else { @@ -82,17 +198,17 @@ public class DeviceManager extends AbstractBehavior { return this; } - public Receive createReceive() { + public Receive createReceive() { return newReceiveBuilder() .onMessage(RequestTrackDevice.class, this::onTrackDevice) .onMessage(RequestDeviceList.class, this::onRequestDeviceList) .onMessage(RequestAllTemperatures.class, this::onRequestAllTemperatures) .onMessage(DeviceGroupTerminated.class, this::onTerminated) - .onSignal(PostStop.class, signal -> postStop()) + .onSignal(PostStop.class, signal -> onPostStop()) .build(); } - private DeviceManager postStop() { + private DeviceManager onPostStop() { context.getLog().info("DeviceManager stopped"); return this; } diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerProtocol.java deleted file mode 100644 index f337e33e84..0000000000 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerProtocol.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) 2009-2019 Lightbend Inc. - */ - -package jdocs.typed.tutorial_5; - -import akka.actor.typed.ActorRef; - -import java.util.Map; -import java.util.Set; - -abstract class DeviceManagerProtocol { - // no instances of DeviceManagerProtocol class - private DeviceManagerProtocol() {} - - interface DeviceManagerMessage {} - - interface DeviceGroupMessage {} - - public static final class RequestTrackDevice implements DeviceManagerMessage, DeviceGroupMessage { - public final String groupId; - public final String deviceId; - public final ActorRef replyTo; - - public RequestTrackDevice(String groupId, String deviceId, ActorRef replyTo) { - this.groupId = groupId; - this.deviceId = deviceId; - this.replyTo = replyTo; - } - } - - public static final class DeviceRegistered { - public final ActorRef device; - - public DeviceRegistered(ActorRef device) { - this.device = device; - } - } - - public static final class RequestDeviceList implements DeviceManagerMessage, DeviceGroupMessage { - final long requestId; - final String groupId; - final ActorRef replyTo; - - public RequestDeviceList(long requestId, String groupId, ActorRef replyTo) { - this.requestId = requestId; - this.groupId = groupId; - this.replyTo = replyTo; - } - } - - public static final class ReplyDeviceList { - final long requestId; - final Set ids; - - public ReplyDeviceList(long requestId, Set ids) { - this.requestId = requestId; - this.ids = ids; - } - } - - // #query-protocol - interface DeviceGroupQueryMessage {} - - public static final class RequestAllTemperatures - implements DeviceGroupQueryMessage, DeviceGroupMessage, DeviceManagerMessage { - - final long requestId; - final String groupId; - final ActorRef replyTo; - - public RequestAllTemperatures( - long requestId, String groupId, ActorRef replyTo) { - this.requestId = requestId; - this.groupId = groupId; - this.replyTo = replyTo; - } - } - - public static final class RespondAllTemperatures { - final long requestId; - final Map temperatures; - - public RespondAllTemperatures(long requestId, Map temperatures) { - this.requestId = requestId; - this.temperatures = temperatures; - } - } - - public static interface TemperatureReading {} - - public static final class Temperature implements TemperatureReading { - public final double value; - - public Temperature(double value) { - this.value = value; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Temperature that = (Temperature) o; - - return Double.compare(that.value, value) == 0; - } - - @Override - public int hashCode() { - long temp = Double.doubleToLongBits(value); - return (int) (temp ^ (temp >>> 32)); - } - - @Override - public String toString() { - return "Temperature{" + "value=" + value + '}'; - } - } - - public enum TemperatureNotAvailable implements TemperatureReading { - INSTANCE - } - - public enum DeviceNotAvailable implements TemperatureReading { - INSTANCE - } - - public enum DeviceTimedOut implements TemperatureReading { - INSTANCE - } - // #query-protocol - -} diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerTest.java index 56143b1e64..d05c9560ed 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceManagerTest.java @@ -11,9 +11,11 @@ import org.junit.ClassRule; import org.junit.Test; import org.scalatest.junit.JUnitSuite; -import static jdocs.typed.tutorial_5.DeviceManagerProtocol.*; import static org.junit.Assert.assertNotEquals; +import static jdocs.typed.tutorial_5.DeviceManager.DeviceRegistered; +import static jdocs.typed.tutorial_5.DeviceManager.RequestTrackDevice; + public class DeviceManagerTest extends JUnitSuite { @ClassRule public static final TestKitJunitResource testKit = new TestKitJunitResource(); @@ -21,7 +23,7 @@ public class DeviceManagerTest extends JUnitSuite { @Test public void testReplyToRegistrationRequests() { TestProbe probe = testKit.createTestProbe(DeviceRegistered.class); - ActorRef managerActor = testKit.spawn(DeviceManager.createBehavior()); + ActorRef managerActor = testKit.spawn(DeviceManager.create()); managerActor.tell(new RequestTrackDevice("group1", "device", probe.getRef())); DeviceRegistered registered1 = probe.receiveMessage(); diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceProtocol.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceProtocol.java deleted file mode 100644 index b007008544..0000000000 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceProtocol.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2009-2019 Lightbend Inc. - */ - -package jdocs.typed.tutorial_5; - -import akka.actor.typed.ActorRef; - -import java.util.Optional; - -abstract class DeviceProtocol { - // no instances of DeviceProtocol class - private DeviceProtocol() {} - - interface DeviceMessage {} - - public static final class RecordTemperature implements DeviceMessage { - final long requestId; - final double value; - final ActorRef replyTo; - - public RecordTemperature(long requestId, double value, ActorRef replyTo) { - this.requestId = requestId; - this.value = value; - this.replyTo = replyTo; - } - } - - public static final class TemperatureRecorded { - final long requestId; - - public TemperatureRecorded(long requestId) { - this.requestId = requestId; - } - } - - public static final class ReadTemperature implements DeviceMessage { - final long requestId; - final ActorRef replyTo; - - public ReadTemperature(long requestId, ActorRef replyTo) { - this.requestId = requestId; - this.replyTo = replyTo; - } - } - - public static final class RespondTemperature { - final long requestId; - final String deviceId; - final Optional value; - - public RespondTemperature(long requestId, String deviceId, Optional value) { - this.requestId = requestId; - this.deviceId = deviceId; - this.value = value; - } - } - - static enum Passivate implements DeviceMessage { - INSTANCE - } -} diff --git a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceTest.java b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceTest.java index 81d388adf3..59d3d071a7 100644 --- a/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceTest.java +++ b/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceTest.java @@ -13,7 +13,6 @@ import org.scalatest.junit.JUnitSuite; import java.util.Optional; -import static jdocs.typed.tutorial_5.DeviceProtocol.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -23,33 +22,36 @@ public class DeviceTest extends JUnitSuite { @Test public void testReplyWithEmptyReadingIfNoTemperatureIsKnown() { - TestProbe probe = testKit.createTestProbe(RespondTemperature.class); - ActorRef deviceActor = testKit.spawn(Device.createBehavior("group", "device")); - deviceActor.tell(new ReadTemperature(42L, probe.getRef())); - RespondTemperature response = probe.receiveMessage(); + TestProbe probe = + testKit.createTestProbe(Device.RespondTemperature.class); + ActorRef deviceActor = testKit.spawn(Device.create("group", "device")); + deviceActor.tell(new Device.ReadTemperature(42L, probe.getRef())); + Device.RespondTemperature response = probe.receiveMessage(); assertEquals(42L, response.requestId); assertEquals(Optional.empty(), response.value); } @Test public void testReplyWithLatestTemperatureReading() { - TestProbe recordProbe = testKit.createTestProbe(TemperatureRecorded.class); - TestProbe readProbe = testKit.createTestProbe(RespondTemperature.class); - ActorRef deviceActor = testKit.spawn(Device.createBehavior("group", "device")); + TestProbe recordProbe = + testKit.createTestProbe(Device.TemperatureRecorded.class); + TestProbe readProbe = + testKit.createTestProbe(Device.RespondTemperature.class); + ActorRef deviceActor = testKit.spawn(Device.create("group", "device")); - deviceActor.tell(new RecordTemperature(1L, 24.0, recordProbe.getRef())); + deviceActor.tell(new Device.RecordTemperature(1L, 24.0, recordProbe.getRef())); assertEquals(1L, recordProbe.receiveMessage().requestId); - deviceActor.tell(new ReadTemperature(2L, readProbe.getRef())); - RespondTemperature response1 = readProbe.receiveMessage(); + deviceActor.tell(new Device.ReadTemperature(2L, readProbe.getRef())); + Device.RespondTemperature response1 = readProbe.receiveMessage(); assertEquals(2L, response1.requestId); assertEquals(Optional.of(24.0), response1.value); - deviceActor.tell(new RecordTemperature(3L, 55.0, recordProbe.getRef())); + deviceActor.tell(new Device.RecordTemperature(3L, 55.0, recordProbe.getRef())); assertEquals(3L, recordProbe.receiveMessage().requestId); - deviceActor.tell(new ReadTemperature(4L, readProbe.getRef())); - RespondTemperature response2 = readProbe.receiveMessage(); + deviceActor.tell(new Device.ReadTemperature(4L, readProbe.getRef())); + Device.RespondTemperature response2 = readProbe.receiveMessage(); assertEquals(4L, response2.requestId); assertEquals(Optional.of(55.0), response2.value); } diff --git a/akka-docs/src/test/scala/typed/tutorial_3/Device.scala b/akka-docs/src/test/scala/typed/tutorial_3/Device.scala index 702aac395c..245b828920 100644 --- a/akka-docs/src/test/scala/typed/tutorial_3/Device.scala +++ b/akka-docs/src/test/scala/typed/tutorial_3/Device.scala @@ -14,30 +14,30 @@ import akka.actor.typed.scaladsl.ActorContext import akka.actor.typed.scaladsl.Behaviors object Device { - def apply(groupId: String, deviceId: String): Behavior[DeviceMessage] = + def apply(groupId: String, deviceId: String): Behavior[Command] = Behaviors.setup(context => new Device(context, groupId, deviceId)) - sealed trait DeviceMessage + sealed trait Command - final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends DeviceMessage + final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends Command final case class RespondTemperature(requestId: Long, value: Option[Double]) //#write-protocol final case class RecordTemperature(requestId: Long, value: Double, replyTo: ActorRef[TemperatureRecorded]) - extends DeviceMessage + extends Command final case class TemperatureRecorded(requestId: Long) //#write-protocol } -class Device(context: ActorContext[Device.DeviceMessage], groupId: String, deviceId: String) - extends AbstractBehavior[Device.DeviceMessage] { +class Device(context: ActorContext[Device.Command], groupId: String, deviceId: String) + extends AbstractBehavior[Device.Command] { import Device._ var lastTemperatureReading: Option[Double] = None context.log.info("Device actor {}-{} started", groupId, deviceId) - override def onMessage(msg: DeviceMessage): Behavior[DeviceMessage] = { + override def onMessage(msg: Command): Behavior[Command] = { msg match { case RecordTemperature(id, value, replyTo) => context.log.info("Recorded temperature reading {} with {}", value, id) @@ -51,7 +51,7 @@ class Device(context: ActorContext[Device.DeviceMessage], groupId: String, devic } } - override def onSignal: PartialFunction[Signal, Behavior[DeviceMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("Device actor {}-{} stopped", groupId, deviceId) this diff --git a/akka-docs/src/test/scala/typed/tutorial_3/DeviceInProgress.scala b/akka-docs/src/test/scala/typed/tutorial_3/DeviceInProgress.scala index 82818b0482..d41e6ea2de 100644 --- a/akka-docs/src/test/scala/typed/tutorial_3/DeviceInProgress.scala +++ b/akka-docs/src/test/scala/typed/tutorial_3/DeviceInProgress.scala @@ -14,8 +14,8 @@ object DeviceInProgress1 { import akka.actor.typed.ActorRef object Device { - sealed trait DeviceMessage - final case class ReadTemperature(replyTo: ActorRef[RespondTemperature]) extends DeviceMessage + sealed trait Command + final case class ReadTemperature(replyTo: ActorRef[RespondTemperature]) extends Command final case class RespondTemperature(value: Option[Double]) } //#read-protocol-1 @@ -32,25 +32,25 @@ object DeviceInProgress2 { import akka.actor.typed.scaladsl.Behaviors object Device { - def apply(groupId: String, deviceId: String): Behavior[DeviceMessage] = + def apply(groupId: String, deviceId: String): Behavior[Command] = Behaviors.setup(context => new Device(context, groupId, deviceId)) //#read-protocol-2 - sealed trait DeviceMessage - final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends DeviceMessage + sealed trait Command + final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends Command final case class RespondTemperature(requestId: Long, value: Option[Double]) //#read-protocol-2 } - class Device(context: ActorContext[Device.DeviceMessage], groupId: String, deviceId: String) - extends AbstractBehavior[Device.DeviceMessage] { + class Device(context: ActorContext[Device.Command], groupId: String, deviceId: String) + extends AbstractBehavior[Device.Command] { import Device._ var lastTemperatureReading: Option[Double] = None context.log.info("Device actor {}-{} started", groupId, deviceId) - override def onMessage(msg: DeviceMessage): Behavior[DeviceMessage] = { + override def onMessage(msg: Command): Behavior[Command] = { msg match { case ReadTemperature(id, replyTo) => replyTo ! RespondTemperature(id, lastTemperatureReading) @@ -58,7 +58,7 @@ object DeviceInProgress2 { } } - override def onSignal: PartialFunction[Signal, Behavior[DeviceMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("Device actor {}-{} stopped", groupId, deviceId) this @@ -73,8 +73,8 @@ object DeviceInProgress3 { object Device { //#write-protocol-1 - sealed trait DeviceMessage - final case class RecordTemperature(value: Double) extends DeviceMessage + sealed trait Command + final case class RecordTemperature(value: Double) extends Command //#write-protocol-1 } } diff --git a/akka-docs/src/test/scala/typed/tutorial_4/Device.scala b/akka-docs/src/test/scala/typed/tutorial_4/Device.scala index 251812e2ac..3c3bdefd6e 100644 --- a/akka-docs/src/test/scala/typed/tutorial_4/Device.scala +++ b/akka-docs/src/test/scala/typed/tutorial_4/Device.scala @@ -14,32 +14,32 @@ import akka.actor.typed.scaladsl.ActorContext import akka.actor.typed.scaladsl.Behaviors object Device { - def apply(groupId: String, deviceId: String): Behavior[DeviceMessage] = + def apply(groupId: String, deviceId: String): Behavior[Command] = Behaviors.setup(context => new Device(context, groupId, deviceId)) - sealed trait DeviceMessage + sealed trait Command - final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends DeviceMessage + final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends Command final case class RespondTemperature(requestId: Long, value: Option[Double]) final case class RecordTemperature(requestId: Long, value: Double, replyTo: ActorRef[TemperatureRecorded]) - extends DeviceMessage + extends Command final case class TemperatureRecorded(requestId: Long) //#passivate-msg - case object Passivate extends DeviceMessage + case object Passivate extends Command //#passivate-msg } -class Device(context: ActorContext[Device.DeviceMessage], groupId: String, deviceId: String) - extends AbstractBehavior[Device.DeviceMessage] { +class Device(context: ActorContext[Device.Command], groupId: String, deviceId: String) + extends AbstractBehavior[Device.Command] { import Device._ var lastTemperatureReading: Option[Double] = None context.log.info("Device actor {}-{} started", groupId, deviceId) - override def onMessage(msg: DeviceMessage): Behavior[DeviceMessage] = { + override def onMessage(msg: Command): Behavior[Command] = { msg match { case RecordTemperature(id, value, replyTo) => context.log.info("Recorded temperature reading {} with {}", value, id) @@ -56,7 +56,7 @@ class Device(context: ActorContext[Device.DeviceMessage], groupId: String, devic } } - override def onSignal: PartialFunction[Signal, Behavior[DeviceMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("Device actor {}-{} stopped", groupId, deviceId) this diff --git a/akka-docs/src/test/scala/typed/tutorial_4/DeviceGroup.scala b/akka-docs/src/test/scala/typed/tutorial_4/DeviceGroup.scala index c8f229633e..29b2c87d3d 100644 --- a/akka-docs/src/test/scala/typed/tutorial_4/DeviceGroup.scala +++ b/akka-docs/src/test/scala/typed/tutorial_4/DeviceGroup.scala @@ -15,29 +15,29 @@ import akka.actor.typed.scaladsl.Behaviors //#device-group-full //#device-group-register object DeviceGroup { - def apply(groupId: String): Behavior[DeviceGroupMessage] = + def apply(groupId: String): Behavior[Command] = Behaviors.setup(context => new DeviceGroup(context, groupId)) - trait DeviceGroupMessage + trait Command - private final case class DeviceTerminated(device: ActorRef[Device.DeviceMessage], groupId: String, deviceId: String) - extends DeviceGroupMessage + private final case class DeviceTerminated(device: ActorRef[Device.Command], groupId: String, deviceId: String) + extends Command } //#device-group-register //#device-group-register //#device-group-remove -class DeviceGroup(context: ActorContext[DeviceGroup.DeviceGroupMessage], groupId: String) - extends AbstractBehavior[DeviceGroup.DeviceGroupMessage] { +class DeviceGroup(context: ActorContext[DeviceGroup.Command], groupId: String) + extends AbstractBehavior[DeviceGroup.Command] { import DeviceGroup._ - import DeviceManager._ + import DeviceManager.{ DeviceRegistered, ReplyDeviceList, RequestDeviceList, RequestTrackDevice } - private var deviceIdToActor = Map.empty[String, ActorRef[Device.DeviceMessage]] + private var deviceIdToActor = Map.empty[String, ActorRef[Device.Command]] context.log.info("DeviceGroup {} started", groupId) - override def onMessage(msg: DeviceGroupMessage): Behavior[DeviceGroupMessage] = + override def onMessage(msg: Command): Behavior[Command] = msg match { case trackMsg @ RequestTrackDevice(`groupId`, deviceId, replyTo) => deviceIdToActor.get(deviceId) match { @@ -76,7 +76,7 @@ class DeviceGroup(context: ActorContext[DeviceGroup.DeviceGroupMessage], groupId //#device-group-register } - override def onSignal: PartialFunction[Signal, Behavior[DeviceGroupMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("DeviceGroup {} stopped", groupId) this diff --git a/akka-docs/src/test/scala/typed/tutorial_4/DeviceManager.scala b/akka-docs/src/test/scala/typed/tutorial_4/DeviceManager.scala index 98b86ba040..c9f5b5705a 100644 --- a/akka-docs/src/test/scala/typed/tutorial_4/DeviceManager.scala +++ b/akka-docs/src/test/scala/typed/tutorial_4/DeviceManager.scala @@ -14,44 +14,41 @@ import akka.actor.typed.scaladsl.Behaviors //#device-manager-full object DeviceManager { - def apply(): Behavior[DeviceManagerMessage] = + def apply(): Behavior[Command] = Behaviors.setup(context => new DeviceManager(context)) //#device-manager-msgs - import DeviceGroup.DeviceGroupMessage - sealed trait DeviceManagerMessage + sealed trait Command //#device-registration-msgs final case class RequestTrackDevice(groupId: String, deviceId: String, replyTo: ActorRef[DeviceRegistered]) - extends DeviceManagerMessage - with DeviceGroupMessage + extends DeviceManager.Command + with DeviceGroup.Command - final case class DeviceRegistered(device: ActorRef[Device.DeviceMessage]) + final case class DeviceRegistered(device: ActorRef[Device.Command]) //#device-registration-msgs //#device-list-msgs final case class RequestDeviceList(requestId: Long, groupId: String, replyTo: ActorRef[ReplyDeviceList]) - extends DeviceManagerMessage - with DeviceGroupMessage + extends DeviceManager.Command + with DeviceGroup.Command final case class ReplyDeviceList(requestId: Long, ids: Set[String]) //#device-list-msgs - private final case class DeviceGroupTerminated(groupId: String) extends DeviceManagerMessage + private final case class DeviceGroupTerminated(groupId: String) extends DeviceManager.Command //#device-manager-msgs } -class DeviceManager(context: ActorContext[DeviceManager.DeviceManagerMessage]) - extends AbstractBehavior[DeviceManager.DeviceManagerMessage] { +class DeviceManager(context: ActorContext[DeviceManager.Command]) extends AbstractBehavior[DeviceManager.Command] { import DeviceManager._ - import DeviceGroup.DeviceGroupMessage - var groupIdToActor = Map.empty[String, ActorRef[DeviceGroupMessage]] + var groupIdToActor = Map.empty[String, ActorRef[DeviceGroup.Command]] context.log.info("DeviceManager started") - override def onMessage(msg: DeviceManagerMessage): Behavior[DeviceManagerMessage] = + override def onMessage(msg: Command): Behavior[Command] = msg match { case trackMsg @ RequestTrackDevice(groupId, _, replyTo) => groupIdToActor.get(groupId) match { @@ -81,7 +78,7 @@ class DeviceManager(context: ActorContext[DeviceManager.DeviceManagerMessage]) this } - override def onSignal: PartialFunction[Signal, Behavior[DeviceManagerMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("DeviceManager stopped") this diff --git a/akka-docs/src/test/scala/typed/tutorial_5/Device.scala b/akka-docs/src/test/scala/typed/tutorial_5/Device.scala index fd599a9715..7c5439a040 100644 --- a/akka-docs/src/test/scala/typed/tutorial_5/Device.scala +++ b/akka-docs/src/test/scala/typed/tutorial_5/Device.scala @@ -14,30 +14,30 @@ import akka.actor.typed.scaladsl.ActorContext import akka.actor.typed.scaladsl.Behaviors object Device { - def apply(groupId: String, deviceId: String): Behavior[DeviceMessage] = + def apply(groupId: String, deviceId: String): Behavior[Command] = Behaviors.setup(context => new Device(context, groupId, deviceId)) - sealed trait DeviceMessage + sealed trait Command - final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends DeviceMessage + final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends Command final case class RespondTemperature(requestId: Long, deviceId: String, value: Option[Double]) final case class RecordTemperature(requestId: Long, value: Double, replyTo: ActorRef[TemperatureRecorded]) - extends DeviceMessage + extends Command final case class TemperatureRecorded(requestId: Long) - case object Passivate extends DeviceMessage + case object Passivate extends Command } -class Device(context: ActorContext[Device.DeviceMessage], groupId: String, deviceId: String) - extends AbstractBehavior[Device.DeviceMessage] { +class Device(context: ActorContext[Device.Command], groupId: String, deviceId: String) + extends AbstractBehavior[Device.Command] { import Device._ var lastTemperatureReading: Option[Double] = None context.log.info("Device actor {}-{} started", groupId, deviceId) - override def onMessage(msg: DeviceMessage): Behavior[DeviceMessage] = { + override def onMessage(msg: Command): Behavior[Command] = { msg match { case RecordTemperature(id, value, replyTo) => context.log.info("Recorded temperature reading {} with {}", value, id) @@ -54,7 +54,7 @@ class Device(context: ActorContext[Device.DeviceMessage], groupId: String, devic } } - override def onSignal: PartialFunction[Signal, Behavior[DeviceMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("Device actor {}-{} stopped", groupId, deviceId) this diff --git a/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroup.scala b/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroup.scala index dfc267f602..334dc552c9 100644 --- a/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroup.scala +++ b/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroup.scala @@ -14,27 +14,33 @@ import akka.actor.typed.scaladsl.ActorContext import akka.actor.typed.scaladsl.Behaviors object DeviceGroup { - def apply(groupId: String): Behavior[DeviceGroupMessage] = + def apply(groupId: String): Behavior[Command] = Behaviors.setup(context => new DeviceGroup(context, groupId)) - trait DeviceGroupMessage + trait Command - private final case class DeviceTerminated(device: ActorRef[Device.DeviceMessage], groupId: String, deviceId: String) - extends DeviceGroupMessage + private final case class DeviceTerminated(device: ActorRef[Device.Command], groupId: String, deviceId: String) + extends Command } //#query-added -class DeviceGroup(context: ActorContext[DeviceGroup.DeviceGroupMessage], groupId: String) - extends AbstractBehavior[DeviceGroup.DeviceGroupMessage] { +class DeviceGroup(context: ActorContext[DeviceGroup.Command], groupId: String) + extends AbstractBehavior[DeviceGroup.Command] { import DeviceGroup._ - import DeviceManager._ + import DeviceManager.{ + DeviceRegistered, + ReplyDeviceList, + RequestAllTemperatures, + RequestDeviceList, + RequestTrackDevice + } - private var deviceIdToActor = Map.empty[String, ActorRef[Device.DeviceMessage]] + private var deviceIdToActor = Map.empty[String, ActorRef[Device.Command]] context.log.info("DeviceGroup {} started", groupId) - override def onMessage(msg: DeviceGroupMessage): Behavior[DeviceGroupMessage] = + override def onMessage(msg: Command): Behavior[Command] = msg match { //#query-added case trackMsg @ RequestTrackDevice(`groupId`, deviceId, replyTo) => @@ -81,7 +87,7 @@ class DeviceGroup(context: ActorContext[DeviceGroup.DeviceGroupMessage], groupId Behaviors.unhandled } - override def onSignal: PartialFunction[Signal, Behavior[DeviceGroupMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("DeviceGroup {} stopped", groupId) this diff --git a/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuery.scala b/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuery.scala index ae5f0267bf..d0521f7ac7 100644 --- a/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuery.scala +++ b/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuery.scala @@ -12,25 +12,16 @@ import akka.actor.typed.scaladsl.AbstractBehavior import akka.actor.typed.scaladsl.ActorContext import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.TimerScheduler -import typed.tutorial_5.Device.DeviceMessage -import typed.tutorial_5.Device.ReadTemperature -import typed.tutorial_5.Device.RespondTemperature -import typed.tutorial_5.DeviceManager.DeviceNotAvailable -import typed.tutorial_5.DeviceManager.DeviceTimedOut -import typed.tutorial_5.DeviceManager.RespondAllTemperatures -import typed.tutorial_5.DeviceManager.Temperature -import typed.tutorial_5.DeviceManager.TemperatureNotAvailable -import typed.tutorial_5.DeviceManager.TemperatureReading //#query-full //#query-outline object DeviceGroupQuery { def apply( - deviceIdToActor: Map[String, ActorRef[Device.DeviceMessage]], + deviceIdToActor: Map[String, ActorRef[Device.Command]], requestId: Long, - requester: ActorRef[RespondAllTemperatures], - timeout: FiniteDuration): Behavior[DeviceGroupQueryMessage] = { + requester: ActorRef[DeviceManager.RespondAllTemperatures], + timeout: FiniteDuration): Behavior[Command] = { Behaviors.setup { context => Behaviors.withTimers { timers => new DeviceGroupQuery(deviceIdToActor, requestId, requester, timeout, context, timers) @@ -38,25 +29,32 @@ object DeviceGroupQuery { } } - trait DeviceGroupQueryMessage + trait Command - private case object CollectionTimeout extends DeviceGroupQueryMessage + private case object CollectionTimeout extends Command - final case class WrappedRespondTemperature(response: RespondTemperature) extends DeviceGroupQueryMessage + final case class WrappedRespondTemperature(response: Device.RespondTemperature) extends Command - private final case class DeviceTerminated(deviceId: String) extends DeviceGroupQueryMessage + private final case class DeviceTerminated(deviceId: String) extends Command } class DeviceGroupQuery( - deviceIdToActor: Map[String, ActorRef[DeviceMessage]], + deviceIdToActor: Map[String, ActorRef[Device.Command]], requestId: Long, - requester: ActorRef[RespondAllTemperatures], + requester: ActorRef[DeviceManager.RespondAllTemperatures], timeout: FiniteDuration, - context: ActorContext[DeviceGroupQuery.DeviceGroupQueryMessage], - timers: TimerScheduler[DeviceGroupQuery.DeviceGroupQueryMessage]) - extends AbstractBehavior[DeviceGroupQuery.DeviceGroupQueryMessage] { + context: ActorContext[DeviceGroupQuery.Command], + timers: TimerScheduler[DeviceGroupQuery.Command]) + extends AbstractBehavior[DeviceGroupQuery.Command] { import DeviceGroupQuery._ + import DeviceManager.DeviceNotAvailable + import DeviceManager.DeviceTimedOut + import DeviceManager.RespondAllTemperatures + import DeviceManager.Temperature + import DeviceManager.TemperatureNotAvailable + import DeviceManager.TemperatureReading + timers.startSingleTimer(CollectionTimeout, CollectionTimeout, timeout) private val respondTemperatureAdapter = context.messageAdapter(WrappedRespondTemperature.apply) @@ -72,19 +70,19 @@ class DeviceGroupQuery( deviceIdToActor.foreach { case (deviceId, device) => context.watchWith(device, DeviceTerminated(deviceId)) - device ! ReadTemperature(0, respondTemperatureAdapter) + device ! Device.ReadTemperature(0, respondTemperatureAdapter) } //#query-outline //#query-state - override def onMessage(msg: DeviceGroupQueryMessage): Behavior[DeviceGroupQueryMessage] = + override def onMessage(msg: Command): Behavior[Command] = msg match { case WrappedRespondTemperature(response) => onRespondTemperature(response) case DeviceTerminated(deviceId) => onDeviceTerminated(deviceId) case CollectionTimeout => onCollectionTimout() } - private def onRespondTemperature(response: RespondTemperature): Behavior[DeviceGroupQueryMessage] = { + private def onRespondTemperature(response: Device.RespondTemperature): Behavior[Command] = { val reading = response.value match { case Some(value) => Temperature(value) case None => TemperatureNotAvailable @@ -97,7 +95,7 @@ class DeviceGroupQuery( respondWhenAllCollected() } - private def onDeviceTerminated(deviceId: String): Behavior[DeviceGroupQueryMessage] = { + private def onDeviceTerminated(deviceId: String): Behavior[Command] = { if (stillWaiting(deviceId)) { repliesSoFar += (deviceId -> DeviceNotAvailable) stillWaiting -= deviceId @@ -105,7 +103,7 @@ class DeviceGroupQuery( respondWhenAllCollected() } - private def onCollectionTimout(): Behavior[DeviceGroupQueryMessage] = { + private def onCollectionTimout(): Behavior[Command] = { repliesSoFar ++= stillWaiting.map(deviceId => deviceId -> DeviceTimedOut) stillWaiting = Set.empty respondWhenAllCollected() @@ -113,7 +111,7 @@ class DeviceGroupQuery( //#query-state //#query-collect-reply - private def respondWhenAllCollected(): Behavior[DeviceGroupQueryMessage] = { + private def respondWhenAllCollected(): Behavior[Command] = { if (stillWaiting.isEmpty) { requester ! RespondAllTemperatures(requestId, repliesSoFar) Behaviors.stopped diff --git a/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuerySpec.scala b/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuerySpec.scala index 893fb73953..551fea32dd 100644 --- a/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuerySpec.scala +++ b/akka-docs/src/test/scala/typed/tutorial_5/DeviceGroupQuerySpec.scala @@ -8,7 +8,7 @@ import scala.concurrent.duration._ import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit import org.scalatest.WordSpecLike -import typed.tutorial_5.Device.DeviceMessage +import typed.tutorial_5.Device.Command import typed.tutorial_5.DeviceGroupQuery.WrappedRespondTemperature import typed.tutorial_5.DeviceManager.DeviceNotAvailable import typed.tutorial_5.DeviceManager.DeviceTimedOut @@ -24,8 +24,8 @@ class DeviceGroupQuerySpec extends ScalaTestWithActorTestKit with WordSpecLike { "return temperature value for working devices" in { val requester = createTestProbe[RespondAllTemperatures]() - val device1 = createTestProbe[DeviceMessage]() - val device2 = createTestProbe[DeviceMessage]() + val device1 = createTestProbe[Command]() + val device2 = createTestProbe[Command]() val deviceIdToActor = Map("device1" -> device1.ref, "device2" -> device2.ref) @@ -49,8 +49,8 @@ class DeviceGroupQuerySpec extends ScalaTestWithActorTestKit with WordSpecLike { "return TemperatureNotAvailable for devices with no readings" in { val requester = createTestProbe[RespondAllTemperatures]() - val device1 = createTestProbe[DeviceMessage]() - val device2 = createTestProbe[DeviceMessage]() + val device1 = createTestProbe[Command]() + val device2 = createTestProbe[Command]() val deviceIdToActor = Map("device1" -> device1.ref, "device2" -> device2.ref) @@ -74,8 +74,8 @@ class DeviceGroupQuerySpec extends ScalaTestWithActorTestKit with WordSpecLike { "return DeviceNotAvailable if device stops before answering" in { val requester = createTestProbe[RespondAllTemperatures]() - val device1 = createTestProbe[DeviceMessage]() - val device2 = createTestProbe[DeviceMessage]() + val device1 = createTestProbe[Command]() + val device2 = createTestProbe[Command]() val deviceIdToActor = Map("device1" -> device1.ref, "device2" -> device2.ref) @@ -100,8 +100,8 @@ class DeviceGroupQuerySpec extends ScalaTestWithActorTestKit with WordSpecLike { "return temperature reading even if device stops after answering" in { val requester = createTestProbe[RespondAllTemperatures]() - val device1 = createTestProbe[DeviceMessage]() - val device2 = createTestProbe[DeviceMessage]() + val device1 = createTestProbe[Command]() + val device2 = createTestProbe[Command]() val deviceIdToActor = Map("device1" -> device1.ref, "device2" -> device2.ref) @@ -127,8 +127,8 @@ class DeviceGroupQuerySpec extends ScalaTestWithActorTestKit with WordSpecLike { "return DeviceTimedOut if device does not answer in time" in { val requester = createTestProbe[RespondAllTemperatures]() - val device1 = createTestProbe[DeviceMessage]() - val device2 = createTestProbe[DeviceMessage]() + val device1 = createTestProbe[Command]() + val device2 = createTestProbe[Command]() val deviceIdToActor = Map("device1" -> device1.ref, "device2" -> device2.ref) diff --git a/akka-docs/src/test/scala/typed/tutorial_5/DeviceManager.scala b/akka-docs/src/test/scala/typed/tutorial_5/DeviceManager.scala index 6a11cf3445..3cbd2e2c9c 100644 --- a/akka-docs/src/test/scala/typed/tutorial_5/DeviceManager.scala +++ b/akka-docs/src/test/scala/typed/tutorial_5/DeviceManager.scala @@ -14,36 +14,34 @@ import akka.actor.typed.scaladsl.Behaviors //#device-manager-full object DeviceManager { - def apply(): Behavior[DeviceManagerMessage] = + def apply(): Behavior[Command] = Behaviors.setup(context => new DeviceManager(context)) //#device-manager-msgs - import DeviceGroup.DeviceGroupMessage - sealed trait DeviceManagerMessage + sealed trait Command final case class RequestTrackDevice(groupId: String, deviceId: String, replyTo: ActorRef[DeviceRegistered]) - extends DeviceManagerMessage - with DeviceGroupMessage + extends DeviceManager.Command + with DeviceGroup.Command - final case class DeviceRegistered(device: ActorRef[Device.DeviceMessage]) + final case class DeviceRegistered(device: ActorRef[Device.Command]) final case class RequestDeviceList(requestId: Long, groupId: String, replyTo: ActorRef[ReplyDeviceList]) - extends DeviceManagerMessage - with DeviceGroupMessage + extends DeviceManager.Command + with DeviceGroup.Command final case class ReplyDeviceList(requestId: Long, ids: Set[String]) - private final case class DeviceGroupTerminated(groupId: String) extends DeviceManagerMessage + private final case class DeviceGroupTerminated(groupId: String) extends DeviceManager.Command //#device-manager-msgs //#query-protocol - import DeviceGroupQuery.DeviceGroupQueryMessage final case class RequestAllTemperatures(requestId: Long, groupId: String, replyTo: ActorRef[RespondAllTemperatures]) - extends DeviceGroupQueryMessage - with DeviceGroupMessage - with DeviceManagerMessage + extends DeviceGroupQuery.Command + with DeviceGroup.Command + with DeviceManager.Command final case class RespondAllTemperatures(requestId: Long, temperatures: Map[String, TemperatureReading]) @@ -55,16 +53,14 @@ object DeviceManager { //#query-protocol } -class DeviceManager(context: ActorContext[DeviceManager.DeviceManagerMessage]) - extends AbstractBehavior[DeviceManager.DeviceManagerMessage] { +class DeviceManager(context: ActorContext[DeviceManager.Command]) extends AbstractBehavior[DeviceManager.Command] { import DeviceManager._ - import DeviceGroup.DeviceGroupMessage - var groupIdToActor = Map.empty[String, ActorRef[DeviceGroupMessage]] + var groupIdToActor = Map.empty[String, ActorRef[DeviceGroup.Command]] context.log.info("DeviceManager started") - override def onMessage(msg: DeviceManagerMessage): Behavior[DeviceManagerMessage] = + override def onMessage(msg: Command): Behavior[Command] = msg match { case trackMsg @ RequestTrackDevice(groupId, _, replyTo) => groupIdToActor.get(groupId) match { @@ -103,7 +99,7 @@ class DeviceManager(context: ActorContext[DeviceManager.DeviceManagerMessage]) this } - override def onSignal: PartialFunction[Signal, Behavior[DeviceManagerMessage]] = { + override def onSignal: PartialFunction[Signal, Behavior[Command]] = { case PostStop => context.log.info("DeviceManager stopped") this