2016-12-10 20 views
0

我試圖在Java Play Framework(2.5.10)中創建一個actor來運行定期任務。但是,當我的應用程序運行時,出現錯誤No implementation for akka.actor.ActorRef was bound(稍後在此帖子中提供了詳細的錯誤消息)。我相信這個錯誤是非常基本的,但我對整個演員都很陌生,並且很難找出它。沒有實現akka.actor.ActorRef被綁定

下面是結合調度類和演員類(根級別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) 

任何想法我失蹤了?

回答

1

bindActor方法使用名稱(即actorRef本身的名稱)註釋您的ActorRef

您可以嘗試使用@Named註釋嗎?

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

我還是不明白爲什麼這個名字很重要,'@ Named'做了什麼,但是非常感謝!它現在解決了一個主要的頭痛。 :-) – dotslash

+1

基本上你並沒有注入Actor,本身就是注入actor實例 - 即ActorRef。在任何非平凡的應用程序中,您將有多個演員飛來飛去,他們都將是'ActorRef's。注入你想要的'ActorRef'的唯一方法是名字 - Guice提供了一種使用'@ Named'註解來實現這一點的方法。所有'ActorRef's都會有一個名字,所以它也是一個合理的選擇,用於它的Guice名字。 –

+0

嗯。 。 。所以演員似乎只能通過他們的名字而不是他們的班級來識別。 – dotslash

相關問題