I have a WebSocket controller which creates per connection actor handler:
class WebSocketController #Inject()(cc: ControllerComponents)(implicit exc: ExecutionContext) {
def socket: WebSocket = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef { out => // Flow that is handled by an actor from 'out' ref
WebSocketActor.props(out) // Create an actor for new connected WebSocket
}
}
}
And inside the actor handler I need to work with ReactiveMongo:
trait ModelDAO extends MongoController with ReactiveMongoComponents {
val collectionName: String
...
}
class UsersCollection #Inject()(val cc: ControllerComponents,
val reactiveMongoApi: ReactiveMongoApi,
val executionContext: ExecutionContext,
val materializer: Materializer)
extends AbstractController(cc) with ModelDAO {
val collectionName: String = "users"
}
So, the usual way is to #Inject() UsersCollection in the target class. But I can't do something like:
class WebSocketActor #Inject()(out: ActorRef, users: UsersCollection) extends Actor { ... }
Because instances of actor creates inside WebSocketActor companion object:
object WebSocketActor {
def props(out: ActorRef) = Props(new WebSocketActor(out))
}
How do I use UsersCollection inside the WebSocketActor?
You can create actor who's dependencies will be injected automatically by Play. No problem. (https://www.playframework.com/documentation/2.6.x/ScalaAkka)
But in case of web sockets, Props of the actor is expected, but not Actor (or ActorRef) itself.
ActorFlow.actorRef { out => // Flow that is handled by an actor from 'out' ref
WebSocketActor.props(out) // <- ACTOR IS NOT CREATED HERE, WE RETURN PROPS
}
So there is no way to do it automatically in this case (At least I didn't find it).
What you can do is to pass UsersCollection manually.
class WebSocketController #Inject()(cc: ControllerComponents, usersCollection: UsersCollection)(implicit exc: ExecutionContext) {
def socket: WebSocket = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef { out => // Flow that is handled by an actor from 'out' ref
WebSocketActor.props(out, usersCollection) //<- ACTOR IS NOT CREATED HERE, WE RETURN PROPS
}
}
}
Noticed that I injected UsersCollection into WebSocketController and pass it to props.
Simple and I do not see any disadvantage.
I have an event handler module inside my play app that basically instantiates all actors upon application startup. event handlers are all actors so you could do it like this:
class EventHandlerBootstrap #Inject() (system: ActorSystem, app: Application) {
EventHandlerBootstrap.handlers.foreach {
case (h, n) => system.actorOf(Props(app.injector.instanceOf(h)), n)
}
}
//These Class[_ <: EventHandler] are classes of user defined actors each with their own
// dependencies which guice will take care of automattically.
object EventHandlerBootstrap {
val handlers: Map[Class[_ <: EventHandler], String] = Map(
classOf[UserRegisteredHandler] -> "user-registered-handler",
classOf[CustomerOrderCreatedHandler] -> "customer-order-created-handler",
classOf[DisputeClosedHandler] -> "dispute-closed-handler",
classOf[Throttle] -> "throttle"
)
}
and inside the module i run the bootstrapper like this:
class EventModule extends ScalaModule with AkkaGuiceSupport {
override def configure(): Unit = {
bind[EventHandlerBootstrap].asEagerSingleton()
}
}
if you're ofcourse not eager to blindly follow my recipe you can still take out the fact that injecting your actors and their dependencies is fully supported by guice as shown above. Hope It'll help
Related
I'm new to Akka and Guice, and just started to explore it.
I'm trying to make a father actor, that produce child actors from one specific type, to be more generic- so it'll produce many kind of child actors with different types.
Currently- I inject to the father-actor a Factory of one specific type of actor,
I don't want to add more cases to the father actor, I'd like to solve this problem in more elegant way.
so now I have 2 child actors, and I want to Inject their factory to the father-actor, to do so i thought that maybe I should create the father-actor twice, and each time to inject a different type of Factory.
what I would want to achieve is something like this code
(this code is not working, but this is the idea):
Base Factory trait:
trait BaseFactory {
apply(id: Int) : Actor
}
object FirstActor {
trait Factory extends BaseFactory
}
class FirstActor (#Assisted id: Int) extends Actor with InjectedActorSupport {
....
}
object SecondActor {
trait Factory extends BaseFactory
}
class SecondActor (#Assisted id: Int) extends Actor with InjectedActorSupport {
....
}
class Father #Inject()(factory: BaseFactory, name: String) extends Actor with InjectedActorSupport {
override def receive: Receive = {
case Command =>
...
val actor = context.child(id)
.getOrElse(injectedChild(factory(id), name, _.withMailbox("deque-mailbox")))
}
}
And then the module:
(this part is not compiling since I can't pass the Factories to props as a trait definition and not an instance)
class Module extends AkkaGuiceSupport {
def configure(): Unit = {
bindActor[ExecutorsOffice]("FirstFather", Props(new Father(FirstActor.Factory)))
bindActor[ExecutorsOffice]("SecondFather", Props(new Father(SecondActor.Factory)))
bindActorFactory[FirstActor, FirstActor.Factory]
bindActorFactory[SecondActor, SecondActor.Factory]
}
I'll be happy to hear your thoughts, and your solutions (other solutions will be great also!)
I'm not sure why you'd need Guice-injection.
type PropsCreator = String => Props
object ParentActor {
def props(childPropsCreator: PropsCreator) = Props(classOf[ParentActor], childPropsCreator)
}
class ParentActor(childPropsCreator: PropsCreator) extends Actor {
// yadda yadda yadda
def receive(): Receive = {
case x: Command =>
// yadda yadda yadda
val child = context.child(id)
.getOrElse(context.actorOf(childPropsCreator(id), id))
child.forward(x)
}
}
object DdvRenderProcessManager {
def props(id: String) = Props(classOf[DdvRenderProcessManager], id)
}
class DdvRenderProcessManager(id: String) extends Actor {
// definition omitted
}
object UpdateProcessManager {
def props(id: String) = Props(classOf[UpdateProcessManager], id)
}
class UpdateProcessManager(id: String) extends Actor {
// definition omitted
}
Then you'd create parents like
val ddvRenderParent = system.actorOf(ParentActor.props(DdvRenderProcessManager.props _), "ddvRenderParent")
val updateParent = system.actorOf(ParentActor.props(UpdateProcessManager.props _), "updateParent")
If you wanted to, for instance have all the DdvRenderProcessManagers have a certain dynamic value:
object DdvRenderProcessManager {
// could also express as
// def props(x: Int)(id: String): Props
// and curry, but this expression is clearer about intent
def propsFor(x: Int): String => Props = { (id: String) =>
Props(classOf[DdvRenderProcessManager], id, x)
}
}
class DdvRenderProcessManager(id: String, x: Int) extends Actor {
// definition omitted
}
And then
val ddvRenderParent = system.actorOf(ParentActor.props(DdvRenderProcessManager.propsFor(42)), "ddvRenderParent")
You could even make the dynamic value implicit to allow for something very-close to compile-time DI.
I am a little confused with the play documentation https://www.playframework.com/documentation/2.5.x/ScalaAkka
Looking at the examples, one can start an actor from a controller:
import play.api.mvc._
import akka.actor._
import javax.inject._
#Singleton
class Application #Inject() (system: ActorSystem) extends Controller {
val actor = system.actorOf(Props(classOf[AnActor], "anActor")
//...
}
Or one can rely on Guice to instantiate that actor
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
import actors.ConfiguredActor
class MyModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActor[AnActor]("anActor")
}
}
When the actor is instantiated by Guice, it is possible to inject a dependency into it
import akka.actor._
import javax.inject._
import play.api.Configuration
class AnActor #Inject() (configuration: Configuration) extends Actor {
//...
}
However, starting that actor from a controller raises an
[IllegalArgumentException: no matching constructor found on class AnActor for arguments []]
Is there a way to inject a service into a non-Guice-instantiated actor?
You could use bindActorFactory and then use a factory for creating actors in controllers.
Bind an actor factory. This is useful for when you want to have child actors injected, and want to pass parameters into them, as well as have Guice provide some of the parameters.
You nead a Factory in your Actor's companion object.
import akka.actor._
object AnActor {
trait Factory {
def apply(): Actor
}
}
class AnActor #Inject() (configuration: Configuration) extends Actor {
def receive = ???
}
You use bindActorFactory in your Module
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
import actors._
class MyModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActorFactory[AnActor, AnActor.Factory]
}
}
Then, you could instantiate your actors from your controller using the actor system (so they will be childs of /user actor) and guice will do its magic.
class LocationsController #Inject()(actorSystem: ActorSystem){
def injectedChild2(create: => Actor, name: String, props: Props => Props = identity)(implicit system: ActorSystem): ActorRef = {
system.actorOf(props(Props(create)), name)
}
val actor1: ActorRef = injectedChild2(childFactory(),"anActor1")(actorSystem)
val actor2: ActorRef = injectedChild2(childFactory(),"anActor2")(actorSystem)
}
You could add parameters to the Factory.apply if you like to pass parameters that aren't injected by guice, in this case you need to annotate this parameters with #Assisted
object AnActor {
trait Factory {
def apply(someValue: String): Actor
}
}
class AnActor #Inject() (configuration: Configuration,#Assisted someValue: String) extends Actor {
def receive = ???
}
class LocationsController #Inject()(actorSystem: ActorSystem){
def injectedChild2(create: => Actor, name: String, props: Props => Props = identity)(implicit system: ActorSystem): ActorRef = {
system.actorOf(props(Props(create)), name)
}
val actor1: ActorRef = injectedChild2(childFactory("value fom actor 1"),"anActor1")(actorSystem)
val actor2: ActorRef = injectedChild2(childFactory("value from actor 2"),"anActor2")(actorSystem)
}
The example and the method injectedChild2 are taken from the original Play example but modified to create actors from controllers.
UPDATE:
check this answer to know why you should avoid create actors from a controller, in the words of James Roper.
I'm not able to figure out how to inject a Service into an Actor.
I tried out several approaches and think this one should fit best.
I think the major problem is that I provide the websocket to the succeeding actor as an parameter. When I pull the Inject up to the class signature compilation doesn't perform because of signature conflict.
I tried adding a currying implicit class declaration to inject the service, but this didn't work out either.
What shall I do to get it right?
Running this code results in a NullPointerException.
package actors
import akka.actor._
import com.google.inject.Inject
import play.api.libs.json._
import models._
import services.WeatherService
/**
* Created by jbc on 28.12.16.
*/
class WeatherWSActor (out: ActorRef) extends Actor {
#Inject() val weatherService: WeatherService = null
def handleMessage(msg: JsValue): JsValue = {
val req = msg.as[Request]
req match {
case Request("daily") => Json.toJson(weatherService.getFourDays())
case Request("next") => Json.toJson(weatherService.getNextHour())
}
}
override def receive: Receive = {
case msg: JsValue => out ! handleMessage(msg)
case _ => out ! "Bad Request"
}
#scala.throws[Exception](classOf[Exception])
override def postStop(): Unit = {
}
}
object WeatherWSActor {
def props(out: ActorRef) = Props(new WeatherWSActor(out))
}
Controller Code:
class WeatherWSController #Inject() (implicit system: ActorSystem, materializer: Materializer) extends Controller {
def socket = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(out => WeatherWSActor.props(out));
}
}
The service is set up with this Module Code
class Module extends AbstractModule {
override def configure(): Unit = {
bind(classOf[WeatherService]).to(classOf[WeatherServiceImpl]).asEagerSingleton()
}
}
This is covered in the documentation, but here's a summary.
If you want to inject your actors, you need to change a couple of things. First off, the injection point is the constructor, so
class WeatherWSActor (out: ActorRef) extends Actor {
#Inject() val weatherService: WeatherService = null
// ...
}
should become
class WeatherWSActor #Inject() (out: ActorRef,
weatherService: WeatherService) extends Actor {
// ...
}
In fact, assigning null to a Scala val is also going to give you issues.
Secondly, declare a binding for the actor instead of creating it yourself.
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
import actors.WeatherWSActor
class MyModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActor[WeatherWSActor]("weather-actor")
}
}
On a side note, you'll also need to create a named binding for out and change the definition of WeatherWSActor to reflect this:
class WeatherWSActor #Inject() (#javax.inject.Named("out-actor") out: ActorRef,
weatherService: WeatherService) extends Actor {
// ...
}
out-actor is whichever class you have set up in the module, e.g.
class MyModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActor[WeatherWSActor]("weather-actor")
bindActor[SomeOutActor]("out-actor")
}
}
But...
Since you're getting out when a web socket is created, you're (presumably) going to have multiple WeatherWSActor instances. This is going to effect how you handle the dependency injection, so it make more sense to not inject outActor (and hence not create a binding for it), but rather have a singleton WeatherWSActor that broadcasts to all instances of OutActor addressed at a certain point in the actor system.
class HomeController #Inject (implicit actorSystem:ActorSystem, materializer :Materializer) extends Controller
{
case class sendMsg(msg:JsValue)
class MYWSACTOR(out:ActorRef,mainActor:ActorRef) extends Actor
{
def receive =
{
case msg : JsValue =>
mainActor ! sendMsg(msg)
case sendMsg(msg) =>
{
/* validation part */
out ! msg
}
}
}
}
val mainActor = actorSystem.actorOf(Props[MYWSACTOR],"mainActor")
def object MYWSACTOR
{
def props(out:ActorRef) = Props(new MYWSACTOR(out,mainActor))
}
def socket = WebSocket.accept[JsValue,JsValue]
{
request =>
ActorFlow.actorRef(out => MYWSACTOR.props(out))
}
I am new to Akka and Scala . I am trying to make a chat application using Akka in Scala and Play framework 2.5.3 .I used the above code from the official documentation. I just wanted to create another actor (mainActor in above code) so that message from the client is validated first and then be sent back. But the problem is mainActor is unable to send the msg to another class case sendMsg. Also , if i try to create the actor at any different point, it gives me compilation error Unexpected Exception: Provisional Exception
Here is hopefully what you probably wanted:
case class SendMsg(msg: JsValue)
case class ReceivedMessage(wsActor: ActorRef, msg: JsValue)
class MyWsActor(out: ActorRef, mainActor:ActorRef) extends Actor {
def receive = {
case msg : JsValue =>
mainActor ! ReceivedMessage(self, msg)
case SendMsg(msg) =>
out ! msg
}
}
object MyWsActor {
def props(out: ActorRef, mainActor: ActorRef) = Props(new MyWsActor(out, mainActor))
//or if you want to instantiate your Props with reflection
def props2(out: ActorRef, mainActor: ActorRef) = Props(classOf[MyWsActor], out, mainActor)
}
class MainActor() extends Actor {
def receive = {
case ReceivedMessage(wsActor, msg) => {
/*
Do sth with msg
*/
val result = msg
wsActor ! SendMsg(result) //here sender is your MyWsActor
}
}
}
class HomeController #Inject() (
actorSystem: ActorSystem,
materializer: Materializer
) extends Controller {
val mainActor = actorSystem.actorOf(Props[MainActor], "mainActor")
def socket = WebSocket.accept[JsValue,JsValue] {
request =>
ActorFlow.actorRef(out => MyWsActor.props(out, mainActor))
}
}
The key is that you can send, with actual message, also ActorRef (or by default you can get a sender ActorRef calling sender()) and then you can replay to this ActorRef.
I have a parent actor named "manager" which creates several child actors.
These child actors then send back their response via "sender tell", i.e directly back to "manager".
I want to create a unit test for this manager actor, and therefore need to inject a probe to forward the messages from the manager to its children.
I used the following post:
http://www.superloopy.io/articles/2013/injecting-akka-testprobe.html
However i'm still having some trouble getting this done correctly.
In order to simplify the situation, attached is code describing the actors and unit test i wrote for just one child.
Manager class:
trait ManagerChildProvider {
def createTimestampPointChild: Actor
}
trait ProductionManagerChildProvider extends ManagerChildProvider {
def createTimestampPointChild = new TimeDifferenceCalculationActor
}
object Manager {
def apply() = new Manager("cid1") with ProductionManagerChildProvider
}
class Manager(name: String) extends Actor with ActorLogging {
this: ManagerChildProvider =>
#Autowired private val modelParams = new ModelParams //list of parameters
val timeDifference = context.actorOf(Props(createTimestampPointChild))
def receive = {
case p#TimePoint(tPoint) =>
timeDifference ! p
case _ =>
log.error("Unknown message type")
}
}
Child class:
class TimeDifferenceCalculationActor extends Actor with ActorLogging {
var previousTimestamp: Long = -1
def receive = {
case tPoint(timestamp) =>
if (previousTimestamp != -1) {
sender ! Result(1)
}
case _ =>
log.error("Unknown message type")
}
}
Test class:
object BarSpec {
class Wrapper(target: ActorRef) extends Actor {
def receive = {
case x => target forward x
}
}
}
trait ChildrenProvider {
def newFoo: Actor
}
class BarSpec extends TestKitSpec("BarSpec") {
import Manager._
import BarSpec._
trait TestCase {
val probe = TestProbe()
trait TestChildrenProvider extends ManagerChildProvider {
def newBar = new Wrapper(probe.ref)
}
val actor = system.actorOf(Props(new Manager(componentId = "cid1") with TestChildrenProvider))
}
"Bar" should {
"involve child in doing something" in new TestCase {
actor ! tPoint(1)
actor ! tPoint(2)
probe.expectMsg(tPoint(1))
//probe.reply("ReplyFromChild")
//expectMsg("ReplyFromParent")
}
}
}
Additional test class:
abstract class TestKitSpec(name: String) extends TestKit(ActorSystem(name)) with MustMatchers with BeforeAndAfterAll with ImplicitSender with WordSpecLike{
override def afterAll() {
system.shutdown()
}
}
Currently i get the following errors:
Error:(36, 42) object creation impossible, since method > createTimestampPointChild in trait ManagerChildProvider of > type => akka.actor.Actor is not defined
val actor = system.actorOf(Props(new Manager(componentId = "cid1") with TestChildrenProvider))
Error:(11, 16) overriding method run in trait BeforeAndAfterAll of type > (testName: Option[String], args: > org.scalatest.Args)org.scalatest.Status;
method run in trait WordSpecLike of type (testName: Option[String], > args: org.scalatest.Args)org.scalatest.Status needs `abstract override' > modifiers
abstract class TestKitSpec(name: String) extends > TestKit(ActorSystem(name))
any help with these specific errors or with the task in general would be highly appreciated