Over a million developers have joined DZone.

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

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

 

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

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.


The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:

Published at DZone with permission of Krishna Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}