我正在尝试在Java Play Framework(2.5.10)中创建一个actor以运行定期任务。但是,当我的应用程序运行时,出现错误No implementation for akka.actor.ActorRef was bound(本文稍后提供的详细错误消息)。我敢肯定这个错误是很基本的,但是我对整个演员都是陌生的,很难弄清楚。

这是绑定调度程序类和actor的类(根级别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
            );
    }
}


最后,演员课:

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)


知道我缺少什么吗?

最佳答案

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

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

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

10-06 12:23