【问题标题】:No implementation for akka.actor.ActorRef was bound没有绑定 akka.actor.ActorRef 的实现
【发布时间】:2016-12-10 13:59:46
【问题描述】:

我正在尝试在 Java Play Framework (2.5.10) 中创建一个演员来运行定期任务。但是,当我的应用程序运行时,我收到错误 No implementation for akka.actor.ActorRef was bound(本文后面提供了详细的错误消息)。我确信这个错误是非常基本的,但我对整个演员这件事还是新手,我很难弄清楚。

这是绑定调度程序类和参与者的类(根级Module.java):

public class Module extends AbstractModule implements AkkaGuiceSupport {

    @Override
    public void configure() {
        // Use the system clock as the default implementation of Clock
        bind(Clock.class).toInstance(Clock.systemDefaultZone());
        // Ask Guice to create an instance of ApplicationTimer when the
        // application starts.
        bind(ApplicationTimer.class).asEagerSingleton();
        // Set AtomicCounter as the implementation for Counter.
        bind(Counter.class).to(AtomicCounter.class);

        // bind the ECWID data importer
        bind(ImportScheduler.class).asEagerSingleton();
        bindActor(UserImportActor.class, UserImportActor.ACTOR_NAME);
    }
}

调度器类:

@Singleton
public class ImportScheduler {

    @Inject
    public ImportScheduler(final ActorSystem actorSystem, final ActorRef UserImportActor) {
        actorSystem.scheduler().schedule(
                Duration.create(1, TimeUnit.SECONDS),
                Duration.create(1, TimeUnit.SECONDS),
                UserImportActor,
              0,
                actorSystem.dispatcher(),
                UserImportActor
            );
    }
}

最后是actor类:

public class UserImportActor extends UntypedActor  {
    public static final String ACTOR_NAME = "user_import_actor";

    @Override
    public void onReceive(Object message){
        Logger.info("The user import actor was called!");
    }
}

当应用程序运行时,这是我看到的错误(完整的错误太长 - 我认为前几行就足够了):

! @72bagdfd4 - Internal server error, for (GET) [/] ->

play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) No implementation for akka.actor.ActorRef was bound.
  while locating akka.actor.ActorRef
    for parameter 1 at services.ecwid.db.ImportScheduler.<init>(ImportScheduler.java:12)
  at Module.configure(Module.java:34) (via modules: com.google.inject.util.Modules$OverrideModule -> Module)

知道我错过了什么吗?

【问题讨论】:

    标签: java playframework akka


    【解决方案1】:

    bindActor 方法使用名称注释您的 ActorRef - actorRef 本身的名称。

    您可以尝试使用@Named 注释吗?

        @Inject
        public ImportScheduler(final ActorSystem actorSystem, @Named("user_import_actor") ActorRef UserImportActor) {
            ...
        }
    

    【讨论】:

    • 我仍然不明白为什么这个名字很重要以及@Named 的作用,但非常感谢!它解决了现在的一个主要问题。 :-)
    • 基本上你不是在注入Actor本身,而是在注入actor实例句柄——即ActorRef。在任何重要的应用程序中,您都会有多个演员四处飞行,他们都是ActorRefs。注入你想要的ActorRef 的唯一方法是通过名称——Guice 提供了一种使用@Named 注释的方法。无论如何,所有ActorRefs 都会有一个名称,因此将其用作其 Guice 名称也是一个合理的选择。
    • 嗯。 . .所以看起来演员只能通过他们的名字而不是他们的班级来识别。
    • 请注意,在将 Akka 安装到 DI 框架的上下文中,该名称特别重要。无论您在没有 DI 框架的情况下使用 Akka,名称都会变得不那么重要(但仍然)。进一步阅读:doc.akka.io/docs/akka/current/java/untyped-actors.htmlletitcrash.com/post/55958814293/akka-dependency-injection
    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    相关资源
    最近更新 更多