james123
james123

Reputation: 137

How is each connection handled in play framework 2?

I'm getting mixed information on how play 2 is architectured. It says on the website that it is built on akka which most likely means that it uses an actor per connection? It also says not to write blocking code because it prevents other events from firing in different connections (Kind of like how it blocks the event loop in node.js). How can that be since blocking code in 1 actor doesn't blocking code in another actor? Isn't that the point of using actors vs callbacks like node.js?

Upvotes: 4

Views: 388

Answers (1)

TizianoPiccardi
TizianoPiccardi

Reputation: 497

When they say Play is built on top of Akka they mean the framework not the webserver. The connections are handled by JBoss Netty (http://netty.io/) that is the embedded webserver.

The connections can be bounded (or not) to a Akka actor to provide an asynchronous response as described here: http://www.playframework.com/documentation/2.1.0/JavaAkka

The communication between actors is non-blocking because they send messages (any Object) and don't wait a response (they don't call methods to communicate with a different actor).

The logic is similar to this:

//somewhere in the code
ActorB.tell(new Request(...));
-----------------------------------------
ActorB:

    public void onReceive(Object msg) throws Exception {
        if (msg instanceof Request) {
            //process the request
            ActorA.tell(new Response(...))
        }
    }
----------------------------------------------------------------
ActorA:

    //ActorA is notified
    public void onReceive(Object msg) throws Exception {
        if (msg instanceof Response) {
            System.out.println("Response: " + msg)
        }
    }

The method tell() send the message and doesn't wait a response.

Upvotes: 2

Related Questions