Favour Behaviors.receiveMessage in the docs (#25853)

... over Behaviors.receive, but not using the ActorContext binding.
This commit is contained in:
Dale Wijnand 2018-11-05 09:57:36 +00:00 committed by Christopher Batey
parent 6a04731971
commit cb9b35d8ee

View file

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