WebSocket.acceptWithActor
instantiates a new Akka actor without making use of Guice.
With Play 2.4, using the injector for my actor was still possible by importing play.api.Play.current
.
Snippet from ReactiveMongo documentation:
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))
}
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?
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)
}
}
I don't think this is possible. Quoting James Roper:
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) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With