嘿伙计们对scala和akka很新,所以我只是在尝试一下...... 但我不明白为什么我的代码提前终止。它工作但直到最后。 最后一位演员迈克在1000之前终止了。
import akka.actor.{Actor, ActorLogging, Props, ActorSystem}
class Person extends Actor with ActorLogging {
def receive = {
case FullPint(number) =>
log.info(s"Comsuming pint $number")
log.info(s"Consumed pint $number")
sender ! EmptyPint(number)
}
}
class BarTender extends Actor with ActorLogging {
var totalOrders = 0
var totalEmpty = 0
def receive = {
case Ticket(quantity) =>
totalOrders += quantity
log.info(s"Serving up $quantity pints to [${sender.path}]")
for(number <- 1 to quantity) {
log.info(s"Preparing pint $number for [${sender.path}]")
log.info(s"Sending pint $number to [${sender.path}]")
log.info(s"$totalOrders ----- $totalEmpty")
sender ! FullPint(number)
}
case EmptyPint(number) =>
totalEmpty = totalEmpty + 1
if(totalEmpty == totalOrders) {
log.info("Bar is shutting down")
context.system.shutdown()
}
}
}
case class Ticket(quantity:Int)
case class FullPint(number:Int)
case class EmptyPint(number:Int)
object HowdyAkka extends App {
val system = ActorSystem("howdy")
val zed = system.actorOf(Props(new BarTender), "zed")
val alice = system.actorOf(Props(new Person), "alice")
val bob = system.actorOf(Props(new Person), "bob")
val mike = system.actorOf(Props(new Person), "mike")
zed.tell(Ticket(3), alice)
zed.tell(Ticket(2), bob)
zed.tell(Ticket(1000), mike)
system.awaitTermination()
}
答案 0 :(得分:0)
考虑以下可能的顺序:
......不久之后,但在迈克处理所有1000品脱之前......
无法保证从不同来源发送的消息将以任何特定顺序到达演员的邮箱,只有单个给定来源的消息才会按顺序到达。因此,Ticket
将按顺序到达BarTender
的邮箱,但来自EmptyPint
的{{1}}邮件可能会进入alice
在下一个BarTender
之前的邮箱,启用上面的序列并混淆您的期望。