Saad Attieh
Saad Attieh

Reputation: 1486

Proper use of actors in the play framework (java)

Got a question on actors within the play framework. Disclaimer - I am still new to actors/AKKA and have been spending quite a while now reading through documentation. I apologise if the answers to any of the below is already documented somewhere that I have missed.

What I would like to verify is that I am implementing a correct/idiomatic solution to the below scenario:

Case: Using play framework, I need to execute code that may block (sql query) in such a way that it does not hinder the rest of my web server.

Below is my current solution and some questions:

    static ActorRef actorTest = Akka.system().actorOf(
       Props.create(ActorTest.class));
     public static Promise<Result> runQuery() {
             Promise<Result>r = Promise.wrap(
     Patterns.ask(actorTest, query, 600000)).map(
                     new Function<Object, Result>() {
                         public Result apply(Object response) {
                             return ok(response.toString());
                         }
                     });
             return r;
         }
  1. Now if I get many requests will they simpley enter an unbounded queue as they are dealt with by the actor? or,
  2. I have read some docs on actor routing. Would I have to take care of this i.e. make a router actor instead which will use some kind of routing logic to send queries to child actors? Or is the above all taken care of in the play framework?
  3. How can I configure the number of threds deadicated to the above actor (read something on this referring to the application.conf file).

Any clarification on the above will be greatly appreciated.

Upvotes: 0

Views: 563

Answers (1)

almendar
almendar

Reputation: 1813

I'm using mostly Scala with Akka and Play so I may be misguiding you but let's give it a try.

First of all you can ditch actors for the task you want. I would just run the computation in the Future.

User actors when you need to have some state. Running query by async mean will do just fine with Future.

Futures and Actora are run on ExecutionContext that the default reincarnation is available in Scala by importing and using by reference. This may be different in Java but probably not much. That default ExecutionContext is configured in application.conf just like you've said.

Upvotes: 1

Related Questions