更新:此问题的源代码可以在here中找到。由danluu提供。
现在已经不推荐使用替换actorFor
的标准方法吗?
我发现的所有内容都涉及actorSelection
-但通过选择获得3个依赖关系是一个真正的皮塔饼。在线上大多数讨论只是说不使用actorFor
,但是akka上的所有书籍仍然使用它(因为它们的目标是2.2 akka之前的版本)。
例如,假设我有两个演员Pilot
和Copilot
,都是Plane
演员的两个孩子。以下是他们目前如何相互参考(Akka Concurrency,Derek Wyatt,2013年):
// Plane.scala
...
override def preStart(){
// Get our children going.
//... create pilot/copilot (in a separate function)
startPeople()
// Tell them system is ready to go
context.actorFor("Pilots/" + pilotName) ! ReadyToGo
context.actorFor("Pilots/" + copilotName) ! ReadyToGo
// Pilot.scala
def receive = {
case ReadyToGo =>
val copilotName = context.system.settings.config.getString("zzz.akka.avionics.flightcrew.copilotName")
copilot = context.actorFor("../" + copilotName)
// Copilot.scala
def receive = {
case ReadyToGo =>
pilot = context.actorFor("../" + pilotName)
// watch out for pilot dying - take over control when he does
context.watch(pilot)
case Controls(ctr) =>
controls = ctr
case Terminated(_) =>
// Pilot died!
plane ! GiveMeControl
没有actorFor怎么办?哦,DI构造器喷射在这里行不通-因为飞行员和副驾驶都需要彼此了解。
谢谢。
附言:这是
startPeople()
方法的内容,不确定是否重要: def startPeople() {
val plane = self
val controls: ActorRef = actorForControls("ControlSurfaces")
val autopilot: ActorRef = actorForControls("Autopilot")
val altimeter: ActorRef = actorForControls("Altimeter")
val people = context.actorOf(Props(new IsolatedStopSupervisor with OneForOneStrategyFactory
{
override def childStarter() {
// These children get implicitly added to the hierarchy
context.actorOf(Props(newCopilot(plane, autopilot, altimeter)), copilotName)
context.actorOf(Props(newPilot(plane, autopilot, controls, altimeter)), pilotName)
}
}), "Pilots")
// Use the default strategy here, which restarts indefinitely
context.actorOf(Props(newLeadFlightAttendant), attendantName)
Await.result(people ? WaitForStart, 1.second)
}
更新:已解决
感谢Derek Wyatt(作者)的出色回答。
例如,这是我如何在一个
map
actor中使用带有Copilot
表示法的Future来获取对Pilot
actor的依赖关系:package zzz.akka.avionics
import akka.actor._
import zzz.akka.avionics.Pilots.ReadyToGo
import zzz.akka.avionics.Plane.GiveMeControl
import akka.actor.Terminated
import zzz.akka.avionics.Plane.Controls
import scala.concurrent.Future
import akka.util.Timeout
import scala.concurrent.duration._
import akka.pattern.pipe
/** Copilot is a fail-over for the pilot.
* Created by Andriy Drozdyuk on 05-Apr-14.
*/
class Copilot(plane: ActorRef, autopilot: ActorRef, altimeter: ActorRef) extends Actor with Stash {
// Implicit execution context for futures
implicit val ec = context.dispatcher
// Implicit timeout for getting dependencies
implicit val timeout = Timeout(1.second)
val conf = context.system.settings.config
var pilotName = conf.getString("zzz.akka.avionics.flightCrew.pilotName")
var controls: ActorRef = context.system.deadLetters
// Helps us get pilot dependency
trait PilotAcquisition
case class PilotAcquired(pilot: ActorRef) extends PilotAcquisition
case class PilotNotAcquired(t: Throwable) extends PilotAcquisition
// Acquire the pilot
// Send either PilotAcquired or PilotNotAcquired message to self
acquirePilot pipeTo self
def acquirePilot: Future[PilotAcquisition] = {
context.actorSelection("../" + pilotName).resolveOne() map {
pilot => PilotAcquired(pilot)
} recover {
case t:Throwable => PilotNotAcquired(t)
}
}
def receive: Receive = waitingForDependencies
def waitingForDependencies: Receive = {
case PilotAcquired(pilot) =>
// Get all the messages we stashed and receive them
unstashAll()
// pass all our acquired dependencies in
context.become(operational(pilot))
case PilotNotAcquired(t) => throw new IllegalStateException(s"Failed to instantiate: $t")
// Any other message save for later
case _ => stash()
}
// All our dependencies have been acquired
def operational(pilot: ActorRef) : Receive = {
case ReadyToGo =>
// Start watch on the pilot in case he kicks it
context.watch(pilot)
case Controls(ctr) =>
controls = ctr
case Terminated(_) =>
// Pilot died!
plane ! GiveMeControl
}
}
最佳答案
ActorSelection
确实是替换actorFor
的方法。但是,正如我在书中已经提到的那样,使用Actor路径或选择可能会使您的应用程序非常脆弱。
context.actorSelection("../someactor") ! ReadyToGo
context.actorSelection(self / "someChild" / "someGrandchild") ! ReadyToGo
更改参与者的层次结构,您的应用程序开始失败。但是,至少使用
ActorSelection
会出现一些超时错误,而不仅仅是将事情发送到死信办公室。现在,如果您要开始获取引用,那就是另一回事了,我建议您使用
Future
来实现。import akka.actor._
import akka.pattern.pipe
class MyActor extends Actor with Stash {
val actors = for {
actorA <- ActorSelection(someRootActor / List("child1", "grandchild", "greatGrandchild")).resolveOne()
actorB <- ActorSelection(someRootActor / List("child2")).resolveOne()
actorC <- ActorSelection(someOtherRootActor / List("childC")).resolveOne()
} yield ActorsLocated(actorA, actorB, actorC)
actors recover {
case t: Throwable =>
ActorLocatingFailed(t)
} pipeTo self
val uninitialized: Receive = {
case ActorsLocated(a, b, c) =>
unstashAll()
context.become(initialized(a, b, c))
case ActorLocatingFailed(reason) =>
throw new IllegalStateException(s"Failed to initialize: $reason")
case _ =>
stash()
}
def initalized(a: ActorRef, b: ActorRef, c: ActorRef): Receive = {
// do your stuff here
}
def receive = uninitialized
}
现在,您的Actor有了一个完全异步的启动,可以正确地捕获其所有依赖项。重新启动后,您就可以开始了。
关于scala - 如何替换actorFor?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22951549/