REF:
- [1] - play - Integrating with Akka
- [2] - akka - Remoting
- [3] - google group - Actor system does not listen on public IP, just on localhost
- [4] - alvin alexander - A simple Akka (actors) remote example
Add depencency
In build.sbt
for both your Play API and akka actor program, add akka’s remoting lib, because this is not included in akka’s main lib.
```
(Re)Config akka
For API
For Play API, change the default akka settings in your conf/application.conf
like following:
If there is no akka object in your config file, you could just add above to the config file.
For Actor
Almost do the same thing as you did in API in your src/main/resources/application.conf
.
Then apply the settings in your code:
Note The provider shown above changes over versions of akka, check your version carefully and choose the right provider.
Program
API
I use Play in Java for this example. For Scala, see this.
In your controller(action), connect your remote actor using:
The main point here is that if you use ask pattern in your code, you have to wrap your result into Promise.
Actor
Actor code:
Launch code:
Now, if the controller askYourActorSomething is called, it will send a message to your actor, which is specified by your path. Then the actor receives this message and send a String back to the API controller, which consequently cause API return “worked!”.
There is a one more thing
If you are gonna use remote actor in Play application in Production, especially in distributed environment, things are going to be a little bit tough.
Firewall
This will cause it impossible to API and Actor program access to each other.
If you are using EC2, this could be solved by setting security groups. You must make sure the API and Actor program is in each other’s group’s inbound.
Pass path to the API
It seems very easy in the first sight, you can just put the actor path in the a database table by overriding actor’s preStart method. Programatically, the API will never know the remote actor it is asking for is still working or already dead. Even if you change the record in your table when the actor is not accessible any more by overriding the postStop of Actor, this method can hardly be called in real situation.