问题描述
我有这种情况:
- ActorA每30-40秒发送一次ActorB开始/停止讯息
- ActorA发送要打印的ActorB字符串(总是)
- ActorB必须打印他收到的字符串,但前提是ActorA只发送一个开始消息
- ActorA sends ActorB start/stop messages every 30-40 seconds
- ActorA sends ActorB strings to print (always)
- ActorB must print the strings he receive, but only if ActorA sent just a start message
- ActorB可以从ActorA读取一个开始/停止消息,然后才能在ActorB之前读取一个开始/停止消息其他字符串消息?我想有这种情况:ActorA发送开始消息给ActorB,ActorB开始打印他在开始消息之前收到的字符串(并且仍然在接收),然后一收到停止消息就停止?
- Can ActorB read messages only under a certain condition (if a boolean is set as true) without losing the messages he receives while that boolean is set as false?
- Can ActorB read a start/stop message from ActorA before the other string messages? I'd like to have this situation: ActorA sends a start message to ActorB, ActorB start printing the strings he received before the start messages (and that is still receiving) and then stop as soon as it receives a stop messages?
-
我的意思是,如果我有Start-M1-M2-Stop-M3-M4-M5-Start-M6-M7-Stop,打印顺序是M1-M2,然后是M3-M4-M5-M6 -M7,或者可以在M3,M4和M5之前读取M6(如果在成为之后收到M6)?
Does the become mantain the order of the messages? I mean, if i have "Start-M1-M2-Stop-M3-M4-M5-Start-M6-M7-Stop", will the printing order be "M1-M2" and then "M3-M4-M5-M6-M7" or could M6 be read before M3, M4 and M5 (if M6 is received just after the become)?
现在我想知道我是否可以做以下事情:
我不知道我是否解释得很好。
,答案非常好,但我仍然有一些疑问。
I don't know if I explained it well.
Thank you, the answers are great, but I still have some doubts.
开始/停止消息?如果ActorB收到M1-M2-M3,然后它在打印M1时收到一个停止消息,我想ActorB再次保存M2和M3。
推荐答案
您可以使用 Stash
trait和成为/ unbecome
Akka的功能。这个想法如下:
You can exactly solve your problem with the Stash
trait and the become/unbecome
functionality of Akka. The idea is the following:
当你收到一个停止
的消息,然后你切换到一个行为,不是开始
的消息。当您收到开始
消息时,您将切换到打印所有收到的邮件的行为,此外您还会清除在此期间到达的所有邮件。
When you receive a Stop
message then you switch to a behaviour where you stash all messages which are not Start
. When you receive a Start
message, then you switch to a behaviour where you print all received messages and additionally you unstash all messages which have arrived in the meantime.
case object Start
case object Stop
case object TriggerStateChange
case object SendMessage
class ActorB extends Actor with Stash {
override def receive: Receive = {
case Start =>
context.become(printingBehavior, false)
unstashAll()
case x => stash()
}
def printingBehavior: Receive = {
case msg: String => println(msg)
case Stop => context.unbecome()
}
}
class ActorA(val actorB: ActorRef) extends Actor {
var counter = 0
var started = false
override def preStart: Unit = {
import context.dispatcher
this.context.system.scheduler.schedule(0 seconds, 5 seconds, self, TriggerStateChange)
this.context.system.scheduler.schedule(0 seconds, 1 seconds, self, SendMessage)
}
override def receive: Actor.Receive = {
case SendMessage =>
actorB ! "Message: " + counter
counter += 1
case TriggerStateChange =>
actorB ! (if (started) {
started = false
Stop
} else {
started = true
Start
})
}
}
object Akka {
def main(args: Array[String]) = {
val system = ActorSystem.create("TestActorSystem")
val actorB = system.actorOf(Props(classOf[ActorB]), "ActorB")
val actorA = system.actorOf(Props(classOf[ActorA], actorB), "ActorA")
system.awaitTermination()
}
}
这篇关于演员可以在一定条件下阅读邮件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!