我正在尝试使用ActorFor获取现有的ActorRef,或者如果它不存在则创建一个新的ActorRef.我有以下代码,但它似乎没有按预期工作..isTerminated()始终为true.
ActorSystem system = ActorSystem.create("System");
ActorRef subscriberCandidate = system.actorFor("akka://System/user/"+name);
if (subscriberCandidate.isTerminated())
{
ActorRef subscriber = system.actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new Sub(name,link);
}
}), name);
System.out.println(subscriber.path().toString() + " created");
}
else
System.out.println("already exists");
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?提前致谢.
Rol*_*uhn 25
Get或create只能由指定actor 的父级执行,因为只有父级才能创建该actor,并且只有父级可以一致地执行(即没有竞争条件).你可以做一个演员
// assuming a String name like "fred" or "barney", i.e. without "/"
final Option<ActorRef> child = child(name);
if (child.isDefined())
return child.get();
else
return getContext().actorOf(..., name);
Run Code Online (Sandbox Code Playgroud)
不要在顶层(即使用system.actorOf)执行此操作,因为那时您无法确定谁在请求创建中"获胜"并且依赖于用户监护人并不是一个好的监督策略.
| 归档时间: |
|
| 查看次数: |
16067 次 |
| 最近记录: |