【发布时间】:2013-12-16 00:08:00
【问题描述】:
我有一个演员可以创造另一个演员:
class MyActor1 extends Actor {
val a2 = system actorOf Props(new MyActor(123))
}
第二个参与者必须在创建后自行初始化(引导),然后才能执行其他工作。
class MyActor2(a: Int) extends Actor {
//initialized (bootstrapped) itself, potentially a long operation
//how?
val initValue = // get from a server
//handle incoming messages
def receive = {
case "job1" => // do some job but after it's initialized (bootstrapped) itself
}
}
所以MyActor2 必须做的第一件事就是做一些初始化工作。这可能需要一些时间,因为它是对服务器的请求。只有在它成功完成后,它才必须能够处理通过receive 传入的消息。在此之前 - 它不能那样做。
当然,对服务器的请求必须是异步的(最好使用Future,而不是async、await 或其他高级别的东西,如AsyncHttpClient)。我知道如何使用 Future,不过这不是问题。
我如何确保这一点?
p.s.我的猜测是它必须先向自己发送消息。
【问题讨论】:
标签: scala asynchronous akka future