scala - 无法使用akka java api的UnTypedActorFactory创建 Actor

标签 scala actor akka

我正在尝试使用无类型转换工厂创建一个 Actor ,编译进展顺利。但是在运行应用程序时,我收到以下错误。我在配置中遗漏了什么吗?

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;
      }
  }));

运行时出错:

Caused by: akka.actor.ActorInitializationException: You cannot create an instance of [com.practice.MyActor] explicitly using the constructor (new). You have to use one of the factory methods to create a new actor. Either use: 'val actor = context.actorOf(Props[MyActor])'
(to create a supervised child actor from within an actor), or 'val actor = system.actorOf(Props(new MyActor(..)))' (to create a top level actor from the ActorSystem)

最佳答案

这是因为您正在 ActorSystem 外部创建 MyActor 实例。在工厂内创建 Actor(这就是它的用途;-)),应该没问题。

ActorSystem system = ActorSystem.create("mynamespace");
ActorRef actor = system.actorOf(new Props(new UntypedActorFactory() {
  public UntypedActor create() {
    return new MyActor();
  }
}));

在这种情况下,您甚至不需要工厂,因为您有一个默认构造函数。只需将类作为参数传递给 Props:

system.actorOf(new Props(MyActor.class));

关于scala - 无法使用akka java api的UnTypedActorFactory创建 Actor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10735716/

相关文章:

akka - 如何更改普罗米修斯抓取指标的端口号

string - 将多行字符串转换为单行

斯卡拉 2.10 : why there is a type mismatch?

scala - Scala 2.9 和 Actors 中的并行集合

scala - 垃圾收集 scala 参与者

scala - Akka 的 Actors 与 Erlang 的 Actors 中消息顺序语义的结果

scala - 抽象类型的 MyClass.this.T 没有可用的 ClassTag

scala - 函数参数: upper bound vs parent class as argument?

scala - 配置 Akka 路由器时出错

c# - 传递上下文信息