我可以创建演员,actorOf并与他们一起看actorFor.我现在想要一些演员id:String,如果它不存在,我希望它被创建.像这样的东西:
def getRCActor(id: String):ActorRef = {
Logger.info("getting actor %s".format(id))
var a = system.actorFor(id)
if(a.isTerminated){
Logger.info("actor is terminated, creating new one")
return system.actorOf(Props[RC], id:String)
}else{
return a
}
}
Run Code Online (Sandbox Code Playgroud)
但这并不isTerminated总是如此,我得到actor name 1 is not unique!了第二次调用的异常.我想我在这里使用了错误的模式.有人可以帮助如何实现这一目标吗?我需要
我应该使用Dispatcher或Router吗?
解决方案 如我所建议的,我使用一个具体的Supervisor来保存地图中可用的actor.可以要求他提供他的一个孩子.
class RCSupervisor extends Actor {
implicit val timeout = Timeout(1 second)
var as = Map.empty[String, ActorRef]
def getRCActor(id: String) = as get id getOrElse {
val c = context actorOf …Run Code Online (Sandbox Code Playgroud)