Fixed small error in Akka Typed Getting Started Tutorial 5 (#28041)
This commit is contained in:
parent
3c9097c7b0
commit
ab3f63899c
3 changed files with 26 additions and 4 deletions
|
|
@ -103,7 +103,24 @@ Scala
|
||||||
Java
|
Java
|
||||||
: @@snip [DeviceGroupQuery.java](/akka-docs/src/test/java/jdocs/typed/tutorial_5/DeviceGroupQuery.java) { #query-state }
|
: @@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.
|
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.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ public class Device extends AbstractBehavior<Device.Command> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #respond-declare
|
||||||
public static final class RespondTemperature {
|
public static final class RespondTemperature {
|
||||||
final long requestId;
|
final long requestId;
|
||||||
final String deviceId;
|
final String deviceId;
|
||||||
|
|
@ -59,6 +60,7 @@ public class Device extends AbstractBehavior<Device.Command> {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// #respond-declare
|
||||||
|
|
||||||
static enum Passivate implements Command {
|
static enum Passivate implements Command {
|
||||||
INSTANCE
|
INSTANCE
|
||||||
|
|
@ -98,10 +100,12 @@ public class Device extends AbstractBehavior<Device.Command> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #respond-reply
|
||||||
private Behavior<Command> onReadTemperature(ReadTemperature r) {
|
private Behavior<Command> onReadTemperature(ReadTemperature r) {
|
||||||
r.replyTo.tell(new RespondTemperature(r.requestId, deviceId, lastTemperatureReading));
|
r.replyTo.tell(new RespondTemperature(r.requestId, deviceId, lastTemperatureReading));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
// #respond-reply
|
||||||
|
|
||||||
private Behavior<Command> onPostStop() {
|
private Behavior<Command> onPostStop() {
|
||||||
getContext().getLog().info("Device actor {}-{} stopped", groupId, deviceId);
|
getContext().getLog().info("Device actor {}-{} stopped", groupId, deviceId);
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,9 @@ object Device {
|
||||||
sealed trait Command
|
sealed trait Command
|
||||||
|
|
||||||
final case class ReadTemperature(requestId: Long, replyTo: ActorRef[RespondTemperature]) extends 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])
|
final case class RespondTemperature(requestId: Long, deviceId: String, value: Option[Double])
|
||||||
|
//#respond-declare
|
||||||
final case class RecordTemperature(requestId: Long, value: Double, replyTo: ActorRef[TemperatureRecorded])
|
final case class RecordTemperature(requestId: Long, value: Double, replyTo: ActorRef[TemperatureRecorded])
|
||||||
extends Command
|
extends Command
|
||||||
final case class TemperatureRecorded(requestId: Long)
|
final case class TemperatureRecorded(requestId: Long)
|
||||||
|
|
@ -45,11 +46,11 @@ class Device(context: ActorContext[Device.Command], groupId: String, deviceId: S
|
||||||
lastTemperatureReading = Some(value)
|
lastTemperatureReading = Some(value)
|
||||||
replyTo ! TemperatureRecorded(id)
|
replyTo ! TemperatureRecorded(id)
|
||||||
this
|
this
|
||||||
|
//#respond-reply
|
||||||
case ReadTemperature(id, replyTo) =>
|
case ReadTemperature(id, replyTo) =>
|
||||||
replyTo ! RespondTemperature(id, deviceId, lastTemperatureReading)
|
replyTo ! RespondTemperature(id, deviceId, lastTemperatureReading)
|
||||||
this
|
this
|
||||||
|
//#respond-reply
|
||||||
case Passivate =>
|
case Passivate =>
|
||||||
Behaviors.stopped
|
Behaviors.stopped
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue