本文介绍了使用Google Guice将服务注入Scala Akka Actor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为akka演员注入一些服务。我正在使用三种不同类型的参与者,每种类型将使用不同的服务。目前,我只有一个模块,在actor内部实例化一个注射器,并在每个Crow内部进行绑定。问题在于,彼此之间会收到一个新的服务实例。

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.

我做了一些阅读,发现
,但是akka文档建议我们不要使用 IndirectActorProducer 。对我来说,将这些服务注入演员的最佳方法是什么? @Inject关键字看起来很有希望,但是我不确定如何使用它。

I did a little bit of reading and found http://www.typesafe.com/activator/template/activator-akka-scala-guicebut 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.

工作流程:

Main会创建指挥官,向其发送命令,Commander会创建三种不同类型的乌鸦,并向其发送消息以执行(正是这些乌鸦需要服务)。

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).

推荐答案

我最终使用了作为指南。我发现它运行良好,并且实际上并没有使用IndirectActorProducer,而是使用另一个类对其进行扩展,这也是他们的建议。

I ended up using http://www.typesafe.com/activator/template/activator-akka-scala-guice as a guide. I found that it works pretty well, and it doesn't actually use IndirectActorProducer but rather another class extending it, which is what they recommend as well.

这篇关于使用Google Guice将服务注入Scala Akka Actor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:13