Favour Behaviors.receiveMessage in the docs (#25853)
... over Behaviors.receive, but not using the ActorContext binding.
This commit is contained in:
parent
6a04731971
commit
cb9b35d8ee
1 changed files with 70 additions and 88 deletions
|
|
@ -55,14 +55,12 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
// #request-response-protocol
|
||||
|
||||
// #request-response-respond
|
||||
val otherBehavior = Behaviors.receive[Request] { (ctx, msg) ⇒
|
||||
msg match {
|
||||
val otherBehavior = Behaviors.receiveMessage[Request] {
|
||||
case Request(query, respondTo) ⇒
|
||||
// ... process query ...
|
||||
respondTo ! Response("Here's your cookies!")
|
||||
Behaviors.same
|
||||
}
|
||||
}
|
||||
// #request-response-respond
|
||||
|
||||
val otherActor: ActorRef[Request] = spawn(otherBehavior)
|
||||
|
|
@ -110,8 +108,7 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
def active(
|
||||
inProgress: Map[Int, ActorRef[URI]],
|
||||
count: Int): Behavior[Command] = {
|
||||
Behaviors.receive[Command] { (_, msg) ⇒
|
||||
msg match {
|
||||
Behaviors.receiveMessage[Command] {
|
||||
case Translate(site, replyTo) ⇒
|
||||
val taskId = count + 1
|
||||
backend ! Backend.StartTranslationJob(taskId, site, backendResponseMapper)
|
||||
|
|
@ -131,15 +128,13 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
active(inProgress = Map.empty, count = 0)
|
||||
}
|
||||
}
|
||||
// #adapted-response
|
||||
|
||||
val backend = spawn(Behaviors.receive[Backend.Request] { (_, msg) ⇒
|
||||
msg match {
|
||||
val backend = spawn(Behaviors.receiveMessage[Backend.Request] {
|
||||
case Backend.StartTranslationJob(taskId, site, replyTo) ⇒
|
||||
replyTo ! Backend.JobStarted(taskId)
|
||||
replyTo ! Backend.JobProgress(taskId, 0.25)
|
||||
|
|
@ -147,7 +142,6 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
replyTo ! Backend.JobProgress(taskId, 0.75)
|
||||
replyTo ! Backend.JobCompleted(taskId, new URI("https://akka.io/docs/sv/"))
|
||||
Behaviors.same
|
||||
}
|
||||
})
|
||||
|
||||
val frontend = spawn(Frontend.translator(backend))
|
||||
|
|
@ -174,7 +168,7 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
|
||||
def idle(timers: TimerScheduler[Msg], target: ActorRef[Batch],
|
||||
after: FiniteDuration, maxSize: Int): Behavior[Msg] = {
|
||||
Behaviors.receive[Msg] { (ctx, msg) ⇒
|
||||
Behaviors.receiveMessage[Msg] { msg ⇒
|
||||
timers.startSingleTimer(TimerKey, Timeout, after)
|
||||
active(Vector(msg), timers, target, after, maxSize)
|
||||
}
|
||||
|
|
@ -182,8 +176,7 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
|
||||
def active(buffer: Vector[Msg], timers: TimerScheduler[Msg],
|
||||
target: ActorRef[Batch], after: FiniteDuration, maxSize: Int): Behavior[Msg] = {
|
||||
Behaviors.receive[Msg] { (_, msg) ⇒
|
||||
msg match {
|
||||
Behaviors.receiveMessage[Msg] {
|
||||
case Timeout ⇒
|
||||
target ! Batch(buffer)
|
||||
idle(timers, target, after, maxSize)
|
||||
|
|
@ -197,7 +190,6 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
active(newBuffer, timers, target, after, maxSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
//#timer
|
||||
|
||||
val probe: TestProbe[Batch] = TestProbe[Batch]()
|
||||
|
|
@ -214,13 +206,11 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
case class OpenThePodBayDoorsPlease(respondTo: ActorRef[HalResponse]) extends HalCommand
|
||||
case class HalResponse(message: String)
|
||||
|
||||
val halBehavior = Behaviors.receive[HalCommand] { (ctx, msg) ⇒
|
||||
msg match {
|
||||
val halBehavior = Behaviors.receiveMessage[HalCommand] {
|
||||
case OpenThePodBayDoorsPlease(respondTo) ⇒
|
||||
respondTo ! HalResponse("I'm sorry, Dave. I'm afraid I can't do that.")
|
||||
Behaviors.same
|
||||
}
|
||||
}
|
||||
|
||||
sealed trait DaveMessage
|
||||
// this is a part of the protocol that is internal to the actor itself
|
||||
|
|
@ -251,8 +241,7 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
case Failure(ex) ⇒ AdaptedResponse(s"$requestId: Request failed")
|
||||
}
|
||||
|
||||
Behaviors.receive { (ctx, msg) ⇒
|
||||
msg match {
|
||||
Behaviors.receiveMessage {
|
||||
// the adapted message ends up being processed like any other
|
||||
// message sent to the actor
|
||||
case AdaptedResponse(msg) ⇒
|
||||
|
|
@ -260,7 +249,6 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
Behaviors.same
|
||||
}
|
||||
}
|
||||
}
|
||||
// #actor-ask
|
||||
|
||||
// somewhat modified behavior to let us know we saw the two requests
|
||||
|
|
@ -279,20 +267,16 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
|
||||
// #per-session-child
|
||||
|
||||
val keyCabinetBehavior: Behavior[GetKeys] = Behaviors.receive { (ctx, msg) ⇒
|
||||
msg match {
|
||||
val keyCabinetBehavior: Behavior[GetKeys] = Behaviors.receiveMessage {
|
||||
case GetKeys(_, respondTo) ⇒
|
||||
respondTo ! Keys()
|
||||
Behaviors.same
|
||||
}
|
||||
}
|
||||
val drawerBehavior: Behavior[GetWallet] = Behaviors.receive { (ctx, msg) ⇒
|
||||
msg match {
|
||||
val drawerBehavior: Behavior[GetWallet] = Behaviors.receiveMessage {
|
||||
case GetWallet(_, respondTo) ⇒
|
||||
respondTo ! Wallet()
|
||||
Behaviors.same
|
||||
}
|
||||
}
|
||||
|
||||
// #per-session-child
|
||||
// messages for the two services we interact with
|
||||
|
|
@ -342,8 +326,7 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
Behavior.same
|
||||
}
|
||||
|
||||
Behaviors.receive((ctx, msg) ⇒ {
|
||||
msg match {
|
||||
Behaviors.receiveMessage {
|
||||
case w: Wallet ⇒
|
||||
wallet = Some(w)
|
||||
nextBehavior
|
||||
|
|
@ -353,7 +336,6 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
case _ ⇒
|
||||
Behaviors.unhandled
|
||||
}
|
||||
})
|
||||
}.narrow[NotUsed] // we don't let anyone else know we accept anything
|
||||
// #per-session-child
|
||||
|
||||
|
|
@ -372,7 +354,7 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
|||
// #standalone-ask
|
||||
|
||||
// keep this out of the sample as it uses the testkit spawn
|
||||
val cookieActorRef = spawn(Behaviors.receive[GiveMeCookies] { (ctx, msg) ⇒
|
||||
val cookieActorRef = spawn(Behaviors.receiveMessage[GiveMeCookies] { msg ⇒
|
||||
msg.replyTo ! Cookies(5)
|
||||
Behaviors.same
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue