我正在关注akka tutorial。基本上,有一个listener
,一个master
和一些worker
。
class Listener extends Actor {
def receive = {
case Report => // message from Master, print result, shutdown system
}
}
class Master extends Actor {
def receive = {
case Calculate => // ask Workers to calculate
case Result => // message from Workers, aggregate results.
// if all Workers have reported back, stop self and children,
// and send Report message to Listener
}
class Worker extends Actor {
def receive = {
case Calculate => // work, send result back to Master
}
}
我的问题是如何从外部世界Listener
获得结果?假设Report
消息为case class Report(results: Int)
。
答案 0 :(得分:0)
我已经弄清楚了。只需在Listener
收到Report
消息时,请致电外部设置者。
object DataHolder {
private var _data: Int = _
def data = _data // getter
def data_= (i: Int): Unit = _data = i // setter
}
class Listener extends Actor {
def receive = {
case Report(results) = {
DataHolder.data = results // call external setter here
}
}
}
答案 1 :(得分:0)
import scala.concurrent.Await
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._
implicit val timeout = Timeout(5 seconds)
val future = actor ? msg // enabled by the “ask” import
val result = Await.result(future, timeout.duration).asInstanceOf[String]