Enterprise Integration Zone is brought to you in partnership with:

Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

Harnessing a New Java Web Dev Stack: Play 2.0, Akka, Comet

11.13.2012
| 5681 views |
  • submit to reddit

 

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

 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);
}
}
}

f 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.


Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)