I have built a web service using Play & Akka and now need to integrate another Webservice, where my web service is a client.
My default controller (with an associated routes file) looks like
class myController @Inject() (implicit val messagesApi: MessagesApi,
config: play.api.Configuration) extends Controller with I18nSupport {
// Actions
}
This spins up a large actor system and everything is good.
One of the actors is defined as below -
class ActorMgr ( jobId: Long,
config: Config) extends Actor with ActorLogging {
// Actor specific stuff
}
My problem is that I now need to call a new web service from this actor. This web service is a database that will log the results from this actor.
I have seen and followed instructions from (among others)
- https://playframework.com/documentation/2.5.x/ScalaWS
- Dependency injection with abstract class and object in Play Framework 2.5
As per the instructions above, i am supposed to inject WSClient into a class where i need to access it.
I am able to solve the dependency injection into a second controller as below
class DbController @Inject() (ws: WSClient) extends Controller {
def post = Action {
// access webservice
}
}
This works, and i can execute the "post" action, by accessing the URL that it is mapped to in the routes file, and therefore access the web service. I also now have two controllers.
My problem is to access the web service controller "post" method from ActorMgr (an Akka Actor). How do i enable that ?
After a lot of research, i wanted to update my findings here. Though i was able to solve my specific problem as below, there is a lot more to be said here.
My specific solution first -
Instead of DbController, I wraped my service as below and injected it where required -
Having said that, injecting stuff gave me a ton of problems. I finally realized that there are a few distinct use cases here -
Here is how you would solve each of the above.
You will need to extend "IndirectActorProducer" and then use that for creating your ActorRef. The problem is "Props" does not know how to interface with Guice. This is also part of the solution in (1)
The below sample code shows all 4 use cases, and compiles. In the code below
ParentActor - Refers to use case (2) above, ChildActor to use case (3), and ParentActor_2 & ChildActor_2 to use case (4).
And then in my application.conf