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 f4fc56262f..41b152daa2 100644 --- a/akka-docs/src/main/paradox/typed/guide/tutorial_5.md +++ b/akka-docs/src/main/paradox/typed/guide/tutorial_5.md @@ -103,7 +103,24 @@ Scala Java : @@snip [DeviceGroupQuery.java](/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQuery.java) { #query-state } -For `RespondTemperature` and `DeviceTerminated` we keep track of the replies by updating `repliesSoFar` and remove the actor from `stillWaiting`, and then delegate to a method `respondWhenAllCollected`, which we will discuss soon. +For `RespondTemperature` and `DeviceTerminated` we keep track of the replies by updating `repliesSoFar` and remove the actor from `stillWaiting`. For this, we can use the actor's identifier already present in the `DeviceTerminated` message. For our `RespondTemperature` message we will need to add this information as follows: + +Scala +: @@snip [DeviceGroupQuery.scala](/akka-docs/src/test/scala/typed/tutorial_5/Device.scala) { #respond-declare } + +Java +: @@snip [DeviceGroupQuery.java](/akka-docs/src/test/java/jdocs/typed/tutorial_5/Device.java) { #respond-declare } + +And: + +Scala +: @@snip [DeviceGroupQuery.scala](/akka-docs/src/test/scala/typed/tutorial_5/Device.scala) { #respond-reply } + +Java +: @@snip [DeviceGroupQuery.java](/akka-docs/src/test/java/jdocs/typed/tutorial_5/Device.java) { #respond-reply } + + +After processing each message we delegate to a method `respondWhenAllCollected`, which we will discuss soon. In the case of timeout, we need to take all the actors that have not yet replied (the members of the set `stillWaiting`) and put a `DeviceTimedOut` as the status in the final reply. 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 975af55d48..e710079106 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 @@ -48,6 +48,7 @@ public class Device extends AbstractBehavior { } } + // #respond-declare public static final class RespondTemperature { final long requestId; final String deviceId; @@ -59,6 +60,7 @@ public class Device extends AbstractBehavior { this.value = value; } } + // #respond-declare static enum Passivate implements Command { INSTANCE @@ -98,10 +100,12 @@ public class Device extends AbstractBehavior { return this; } + // #respond-reply private Behavior onReadTemperature(ReadTemperature r) { r.replyTo.tell(new RespondTemperature(r.requestId, deviceId, lastTemperatureReading)); return this; } + // #respond-reply private Behavior onPostStop() { getContext().getLog().info("Device actor {}-{} stopped", groupId, deviceId); 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 a4e6bb6922..346690886a 100644 --- a/akka-docs/src/test/scala/typed/tutorial_5/Device.scala +++ b/akka-docs/src/test/scala/typed/tutorial_5/Device.scala @@ -21,8 +21,9 @@ object Device { sealed trait Command final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends Command + //#respond-declare final case class RespondTemperature(requestId: Long, deviceId: String, value: Option[Double]) - + //#respond-declare final case class RecordTemperature(requestId: Long, value: Double, replyTo: ActorRef[TemperatureRecorded]) extends Command final case class TemperatureRecorded(requestId: Long) @@ -45,11 +46,11 @@ class Device(context: ActorContext[Device.Command], groupId: String, deviceId: S lastTemperatureReading = Some(value) replyTo ! TemperatureRecorded(id) this - + //#respond-reply case ReadTemperature(id, replyTo) => replyTo ! RespondTemperature(id, deviceId, lastTemperatureReading) this - + //#respond-reply case Passivate => Behaviors.stopped }