无法使用Akka Java API的UnTypedActorFactory创建演员 [英] Unable to create an actor using UnTypedActorFactory of akka java api

查看:142
本文介绍了无法使用Akka Java API的UnTypedActorFactory创建演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用未类型化的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;
      }
  }));

运行时错误:


原因:akka.actor.ActorInitializationException:您不能使用构造函数
(新)显式创建
[com.practice.MyActor]实例。您必须使用一种工厂方法来创建新的
actor。可以使用:'val actor = context.actorOf(Props [MyActor])'

(从一个actor中创建一个受监督的子actor),或者'val
actor = system.actorOf( Props(new MyActor(..)))'(从ActorSystem创建顶级
演员)

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(这就是;-)的意思)。

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创建演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆