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,13 +55,11 @@ 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
|
||||||
|
|
||||||
|
|
@ -110,25 +108,23 @@ 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)
|
active(inProgress.updated(taskId, replyTo), taskId)
|
||||||
active(inProgress.updated(taskId, replyTo), taskId)
|
|
||||||
|
|
||||||
case wrapped: WrappedBackendResponse ⇒ wrapped.response match {
|
case wrapped: WrappedBackendResponse ⇒ wrapped.response match {
|
||||||
case Backend.JobStarted(taskId) ⇒
|
case Backend.JobStarted(taskId) ⇒
|
||||||
ctx.log.info("Started {}", taskId)
|
ctx.log.info("Started {}", taskId)
|
||||||
Behaviors.same
|
Behaviors.same
|
||||||
case Backend.JobProgress(taskId, progress) ⇒
|
case Backend.JobProgress(taskId, progress) ⇒
|
||||||
ctx.log.info("Progress {}: {}", taskId, progress)
|
ctx.log.info("Progress {}: {}", taskId, progress)
|
||||||
Behaviors.same
|
Behaviors.same
|
||||||
case Backend.JobCompleted(taskId, result) ⇒
|
case Backend.JobCompleted(taskId, result) ⇒
|
||||||
ctx.log.info("Completed {}: {}", taskId, result)
|
ctx.log.info("Completed {}: {}", taskId, result)
|
||||||
inProgress(taskId) ! result
|
inProgress(taskId) ! result
|
||||||
active(inProgress - taskId, count)
|
active(inProgress - taskId, count)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -138,16 +134,14 @@ class InteractionPatternsSpec extends ScalaTestWithActorTestKit with WordSpecLik
|
||||||
}
|
}
|
||||||
// #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)
|
replyTo ! Backend.JobProgress(taskId, 0.50)
|
||||||
replyTo ! Backend.JobProgress(taskId, 0.50)
|
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,20 +176,18 @@ 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)
|
||||||
|
case m ⇒
|
||||||
|
val newBuffer = buffer :+ m
|
||||||
|
if (newBuffer.size == maxSize) {
|
||||||
|
timers.cancel(TimerKey)
|
||||||
|
target ! Batch(newBuffer)
|
||||||
idle(timers, target, after, maxSize)
|
idle(timers, target, after, maxSize)
|
||||||
case m ⇒
|
} else
|
||||||
val newBuffer = buffer :+ m
|
active(newBuffer, timers, target, after, maxSize)
|
||||||
if (newBuffer.size == maxSize) {
|
|
||||||
timers.cancel(TimerKey)
|
|
||||||
target ! Batch(newBuffer)
|
|
||||||
idle(timers, target, after, maxSize)
|
|
||||||
} else
|
|
||||||
active(newBuffer, timers, target, after, maxSize)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#timer
|
//#timer
|
||||||
|
|
@ -214,12 +206,10 @@ 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
|
||||||
|
|
@ -251,14 +241,12 @@ 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) ⇒
|
ctx.log.info("Got response from hal: {}", msg)
|
||||||
ctx.log.info("Got response from hal: {}", msg)
|
Behaviors.same
|
||||||
Behaviors.same
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// #actor-ask
|
// #actor-ask
|
||||||
|
|
@ -279,19 +267,15 @@ 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.receive { (ctx, msg) ⇒
|
val drawerBehavior: Behavior[GetWallet] = Behaviors.receiveMessage {
|
||||||
msg match {
|
case GetWallet(_, respondTo) ⇒
|
||||||
case GetWallet(_, respondTo) ⇒
|
respondTo ! Wallet()
|
||||||
respondTo ! Wallet()
|
Behaviors.same
|
||||||
Behaviors.same
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// #per-session-child
|
// #per-session-child
|
||||||
|
|
@ -342,18 +326,16 @@ 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
|
case k: Keys ⇒
|
||||||
case k: Keys ⇒
|
keys = Some(k)
|
||||||
keys = Some(k)
|
nextBehavior
|
||||||
nextBehavior
|
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
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue