经过大量研究,我想在这里更新我的发现。虽然我能够解决我的具体问题如下,但这里还有很多话要说。
我的具体解决方案首先 -
而不是 DbController,我将我的服务包装如下,并在需要的地方注入它 -
trait Db {
def post
}
class InfluxDb @Inject() (ws: WSClient) extends Db {
val logger = LoggerFactory.getLogger(classOf[InfluxDb])
logger.info("InfluxDb: Initiatlized")
def post = {
val req = ws.url("http://localhost:9086/write")
.withQueryString("db" -> "db1")
.withHeaders("Content-Type" -> "application/json")
.post("job_id,command=PUT value=99")
logger.debug("InfluxDb: Post")
}
}
话虽如此,注入东西给我带来了很多问题。我终于意识到这里有一些不同的用例 -
- 使用 Akka & Guice 而不是使用 Playframework
- 使用 Playframework + Akka + Guice 并注入顶级actor
- 使用 Playframework + Akka + Guice 并注入子actor
- 使用 playframework + Akka + Guice 但创建不“注入”您的顶级演员和演员系统。
以下是解决上述每个问题的方法。
- 对于 (1) - 参考 guice akka 教程
- 对于 (2) 和 (3) - 请参阅 Playframework 文档
- 对于 (4) 这有点棘手
您将需要扩展“IndirectActorProducer”,然后使用它来创建您的 ActorRef。问题是“道具”不知道如何与 Guice 交互。这也是(1)中解决方案的一部分
下面的示例代码显示了所有 4 个用例,并进行了编译。在下面的代码中
ParentActor - 参考上述用例 (2),ChildActor 参考用例 (3),ParentActor_2 和 ChildActor_2 参考用例 (4)。
// play imports
import play.api.mvc._
import play.api.Logger
import play.api.mvc.Results
// actor imports
import akka.actor.{Actor, ActorSystem, ActorRef, Props, IndirectActorProducer}
// DI imports
import com.google.inject.{Injector, AbstractModule, Key, Provides}
import javax.inject._
import com.google.inject.assistedinject.Assisted
import play.libs.akka.AkkaGuiceSupport
import play.api.libs.concurrent.InjectedActorSupport
class MainCntrlr @Inject() (injector : Injector,
@Named("PActor") pa: ActorRef,
cfP: ParentActor_2.Factory)
extends Controller {
Logger.debug("MainCntrlr: created")
val pa_2 = ActorSystem("test")
.actorOf(Props(classOf[GuiceActorProducer], injector, "PActor_2"), "PA_2")
pa ! 12
pa_2 ! 100
def index = Action { Ok (views.html.index.render()) }
}
class ParentActor @Inject() (cf: ChildActor.Factory) extends Actor with InjectedActorSupport {
Logger.debug("ParentActor: created")
val cactor = injectedChild(cf(2),"childActor")
cactor ! 10
def receive = { case _ => Logger.debug("ParentActor received msg") }
}
object ChildActor { trait Factory { def apply(i: Int) : Actor } }
class ChildActor @Inject()( i: Injector, @Assisted v: Int) extends Actor {
Logger.debug("ChildActor: created with value " + v.toString)
def receive = { case _ => Logger.debug("ChildActor received msg") }
}
class ParentModule extends AbstractModule with AkkaGuiceSupport {
def configure () = {
bindActor(classOf[ParentActor],"PActor")
bindActorFactory(classOf[ChildActor], classOf[ChildActor.Factory])
bindActorFactory(classOf[ParentActor_2], classOf[ParentActor_2.Factory])
bindActorFactory(classOf[ChildActor_2], classOf[ChildActor_2.Factory])
}
}
object ParentActor_2 { trait Factory { def apply() : Actor } }
class ParentActor_2 @Inject() (cf: ChildActor_2.Factory) extends Actor with InjectedActorSupport {
Logger.debug("ParentActor_2: created")
val cactor = injectedChild(cf(4),"childActor_2")
cactor ! 10
def receive = { case _ => Logger.debug("ParentActor_2 received msg") }
}
object ChildActor_2 { trait Factory { def apply(i: Int) : Actor } }
class ChildActor_2 @Inject() ( i: Injector, @Assisted v: Int) extends Actor {
Logger.debug("ChildActor_2: created with value " + v.toString)
def receive = { case _ => Logger.debug("ChildActor_2 received msg") }
}
class GuiceActorProducer(val injector: Injector, val actorName: String)
extends IndirectActorProducer {
override def actorClass = classOf[ParentActor_2]
override def produce() =
injector.getBinding(Key.get(classOf[ParentActor_2])).getProvider.get()
}
然后在我的 application.conf
play.modules.enabled += "package.ParentModule"