Actor中的WebSocket.acceptWithActor和@Inject()(播放2.5) [英] WebSocket.acceptWithActor and @Inject() in the Actor (Play 2.5)

查看:115
本文介绍了Actor中的WebSocket.acceptWithActor和@Inject()(播放2.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebSocket.acceptWithActor实例化一个新的Akka演员,而无需使用Guice.

WebSocket.acceptWithActor instantiates a new Akka actor without making use of Guice.

在Play 2.4中,仍然可以通过导入play.api.Play.current为演员使用喷射器.

With Play 2.4, using the injector for my actor was still possible by importing play.api.Play.current.

ReactiveMongo文档中的摘录:

import scala.concurrent.Future

import play.api.Play.current // should be deprecated in favor of DI
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json.collection.JSONCollection

object Foo {
  lazy val reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]

  def collection(name: String): Future[JSONCollection] =
    reactiveMongoApi.database.map(_.collection[JSONCollection](name))
}

但是在Play 2.5中,不推荐使用play.api.Play.current.如何仍然将ReactiveMongoApi注入演员中?在演员中使用ReactiveMongoApi实例的推荐方式是什么?

But in Play 2.5, play.api.Play.current is deprecated. How can I still inject ReactiveMongoApi in my actor? What is the recommended way of using an instance of ReactiveMongoApi in my actor?

这是我的与Play 2.4兼容的代码,因为我的自定义演员类ClientActor可以通过current.injector.instanceOf[ReactiveMongoApi]访问ReactiveMongoApi:

Here is my code which works with Play 2.4 because my custom actor class ClientActor has access to ReactiveMongoApi through current.injector.instanceOf[ReactiveMongoApi]:

@Singleton
class Application @Inject() (system: ActorSystem) extends Controller {

  val midiDiscoveryActor = system.actorOf(MidiDiscoveryActor.props, "midi-discovery-actor")
  val midiActor = system.actorOf(MidiActor.props(midiDiscoveryActor), "midi-actor")

  def index(page: String) = Action {
    Ok(views.html.index(page))
  }

  def bidirectional = WebSocket.acceptWithActor[JsValue, JsValue] { request => out =>
    ClientActor.props(out, midiActor, midiDiscoveryActor)
  }

}

推荐答案

我认为这是不可能的.引用 James Roper :

I don't think this is possible. Quoting James Roper:

Play为依赖项注入参与者提供的帮助程序仅适用于有限的用例.但是,对于某些常见需求,助手实际上只是很薄的包装器-根本不需要它们.在Play的WebSocket actor支持的情况下,通常是要手动实例化actor,因为您必须以某种方式将其传递出ActorRef.因此,您可以使用Guice辅助注入进行此操作,并定义一个取出out actor ref(以及您想要传递给它的任何其他参数)的factor接口,或者简单地手动实例化它,将依赖关系从控制器传递给actor ,例如:

The helpers that Play provides for dependency injecting actors are suited for a limited number of use cases. Though, the helpers are really just very thin wrappers over some common requirements - they're not needed at all. In the case Play's WebSocket actor support, the thing is, generally you want to manually instantiate the actor since you have to somehow pass it the out ActorRef. So, you can either do this using Guice assisted inject, and define a factor interface that takes the out actor ref (and whatever other arguments you want to pass to it), or simply instantiate it manually, passing dependencies from the controller to the actor, for example:

class MyController @Inject() (myDep: MyDep) extends Controller {
  def socket = WebSocket.acceptWithActor[String, String] { request => out =>
    MyWebSocketActor.props(out, myDep)
  }
}

这篇关于Actor中的WebSocket.acceptWithActor和@Inject()(播放2.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆