Harnessing New Java Web Development stack: Play 2.0, Akka, Comet

For people in hurry, here is the code and some steps to run few demo samples.

Disclaimer: I am still learning Play 2.0, please point to me if something is incorrect.

Play 2.0 is a web application stack that bundled with Netty for HTTP ServerAkka for loosely coupled backend processing and Comet / Websocket for asynchronous browser rendering. Play 2.0 itself does not do any session state management, but uses cookies to manage User Sessions and Flash data. Play 2.0 advocates Reactive Model based on Iteratee IO. Please also see my blog on how Play 2.0 pits against Spring MVC.

In this blog, I will discuss some of these points and also discuss how Akka and Comet complement Play 2.0. The more I understand Play 2.0 stack the more I realize that Scala is better suited to take advantages of capabilities of Play 2.0 compared to Java. There is a blog on how Web developers view of Play 2.0. You can understand how Akka’s Actor pits against JMS refer this Stackoverflow writeup. A good documentation on Akka’s actor is here.

Play 2.0, Netty, Akka, Commet: how it fits

Play 2.0, Netty, Akka, Comet: How it fits

Play 2.0, Netty, Akka, Comet: How it fits

Servlet container like Tomcat blocks each request until the backend processing is complete. Play 2.0 stack will help in achieving the usecase like, you need to web crawl and get all the product listing from various sources in a non-blocking and asynchronous way using loosely coupled message oriented architecture.

For example, the below code will not be scalable in Play 2.0 stack, because Play has only 1 main thread and the code blocks other requests to be processed. In Play 2.0/Netty the application registers with callback on a long running process using frameworks like Akka when it is completed, in a reactive pattern.

public static Result index() {
//Here is where you can put your long running blocking code like getting
// the product feed from various sources
return ok("Hello world");
}

The controller code to use Akka to work in a non-blocking way with async callback is as below,

public static Result index() {
return async(
future(new Callable<Integer>() {
public Integer call() {
//Here is where you can put your long running blocking code like getting
//the product feed from various sources

return 4;
}
}).map(new Function<Integer,Result>() {
public Result apply(Integer i) {

ObjectNode result = Json.newObject();

result.put("id", i);
return ok(result);
}
})
);
}

And more cleaner and preferred way is Akka’s Actor model is as below,

public static Result sayHello(String data) {

Logger.debug("Got the request: {}" + data);

ActorSystem system = ActorSystem.create("MySystem");
ActorRef myActor = system.actorOf(new Props(MyUntypedActor.class), "myactor");

return async(
Akka.asPromise(ask(myActor, data, 1000)).map(
new Function<Object,Result>() {
public Result apply(Object response) {
ObjectNode result = Json.newObject();

result.put("message", response.toString());
return ok(result);
}
}
)
);
}

static public class MyUntypedActor extends UntypedActor {

public void onReceive(Object message) throws Exception {
if (message instanceof String){
Logger.debug("Received String message: {}" + message);

//Here is where you can put your long running blocking code like getting
//the product feed from various sources

getSender().tell("Hello world");
}
else {
unhandled(message);
}
}
}

If you want to understand how we can use Comet for asynchronously render data to the browser using Play, Akka and Comet refer the code in Github. Here is some good writeup comparing Comet and Websocket in Stackoverflow.

I hope this blog helped you.

About these ads

4 thoughts on “Harnessing New Java Web Development stack: Play 2.0, Akka, Comet

  1. Pingback: Play 2.0: Web Development Framework using Scala | Spring under the hood

  2. Renato

    Hi Krishna,
    I’m starting study Java Web Development and I want to know if you recommend start with Play Framework or not. Is it robust enough? Will it leave me in a bad position with a big project in the future?

    Reply
    1. Krishna Post author

      Renato,

      Play is a good framework for webdevelopment for someone who comes from Java background.. it is straight forward to understand if you know MVC..

      Plus, right now, there are few enterprise deployments of Play. Go ahead and learn it.. I encourage to learn Play using Scala, another great programming language to learn.

      I hope it helps.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s