问题描述
我试图用未类型化的actor工厂创建一个actor,编译进行得很好。但是在运行应用程序时,出现以下错误。我是否在配置中缺少任何内容?
I am trying to create an actor with untyped actor factory, compilation happens fine. But while running the application, I get the following error. Am I missing anything in configuration?
Java代码:
MyActor myactor = new MyActor(); //MyActor extends UnTypedActor
ActorSystem system = ActorSystem.create("mynamespace");
ActorRef actor = system.actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return myactor;
}
}));
运行时错误:
推荐答案
这是因为您要在 ActorSystem
之外创建 MyActor
的实例。在工厂内部创建Actor(这就是;-)的意思)。
That's because you are creating the instance of MyActor
outside the ActorSystem
. Create the Actor inside of your factory (that's what it is for ;-) ) and it should be fine.
ActorSystem system = ActorSystem.create("mynamespace");
ActorRef actor = system.actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new MyActor();
}
}));
在这种情况下,您甚至不需要工厂,因为您有默认的构造函数。只需将类作为参数传递给 Props
:
In this case you don't even need a factory, because you have a default constructor. Just pass the class as parameter to the Props
:
system.actorOf(new Props(MyActor.class));
这篇关于无法使用Akka Java API的UnTypedActorFactory创建演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!