Injecting services into Scala Akka Actors with Google Guice

时间:2015-07-31 20:54:12

标签: scala dependency-injection akka guice

I have a couple of services that I want to inject into akka actors. There are three different types of actors I am working with, and each type will use different services. Currently I just have a module, instantiate an injector inside of the actor, and do the binding inside of each Crow. The issue is that for each other, they receive a new instance of the service.

I did a little bit of reading and found http://www.typesafe.com/activator/template/activator-akka-scala-guice but the documentation for akka recommends we not use IndirectActorProducer. What is the best way for me to inject these services into my actors? The @Inject keyword looks promising but I'm not exactly sure how to use this.

Workflow:

Main creates commander, sends it a command, commander creates the three different types of crows, and sends them messages to execute (it is these crows that require the services).

2 个答案:

答案 0 :(得分:1)

在您的模块中使用@Provides方法接受服务作为参数并返回ActorRefs。在这些提供程序方法中,您从注入的服务中实例化Props,但让akka实际创建actor,因此您不需要子注入器。

请参阅我对https://stackoverflow.com/a/30901808/1341546的回答。

答案 1 :(得分:1)

我最终使用http://www.typesafe.com/activator/template/activator-akka-scala-guice作为指南。我发现它工作得很好,它实际上并没有使用IndirectActorProducer,而是另一个扩展它的类,这也是他们推荐的。