Category Archives: Play

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.

Java Web Development Frameworks: Play 2.0 and Spring MVC

Recently I was exploring alternative Java Web Development frameworks, I stumbled upon Play 2.0, I wanted to compare this against Spring MVC which has a good enterprise development capabilities. In this blog I will discuss few of those points. Though I am biased towards Spring MVC, I still liked Play 2.0 as a potential candidate. Also note, there are some major differences between Play 2.0 and Play 1.0.

Taking advantage of existing knowledge

Play 2.0 support MVC design pattern similar to Spring MVC. Java is the primary language in Play 2.0, it also support for Scala. Lift is another popular framework that support Scala. There is a good comparison of Play 2.0 and Lift here. Java developers can be productive immediately, I was up and running a web application in 15mins.

There is a good article in Stackoverflow comparing Play 2.0 with other non java frameworks like, Grails and  Django on What is pro and contra of using Play Framework?

SBT: Ease of build

Play 2.0 uses SBT (Simple build tool) for build process and Maven for repository management. Maven and Gradle are popular among Java and Spring MVC world for build and repository management.

SBT Support for continuous Integration

As mentioned earlier, Play 2.0 is built on top of SBT. SBT has plugins for Hudson for continuous integration

Netty based Ease of deploy

Play 2.0 is built on top and bundled with Netty web server which is an asynchronous event-driven network application framework. Netty is similar to Node.js, which is popular among people who are looking for high performance web application development. It also supports websocket protocol.  Ideally for deployment, you can use commands like

play dist
play publish

The above commands will zip the whole Play 2.0 application into a zip file and publish the application to Maven repository. In the Production environment, based on the version, you can download and unzip and run the Play 2.0 environment. There are few good articles around this in Stackoverflow, Play Framework Deployment.

The other and not so preferred way is to bundle the play application in a war file and deploy it in a Servlet container like vFabric tc Server. When you do this, you will not exploit the full potential of Play framework like Netty. Play development team will support Servlet containers like vFabric tc Server in their future version, till than there is a article in Stackoverflow which shows how to deploy in Tomcat.

Ease of Unit test

There is a good article on how to do JUnit test in Play 2.0. This will demonstrate testing of all the layers of Play 2.0 like Routes, Controllers and Views. Play 2.0 definitely encourages Test first development. Sonar is a good software quality measuring tool, there are also some configuration for integrating SBT with Sonar.

Support for ORM: Ebean and JPA

Play 2.0 primarily supports EBean and JPA. There are hacks to support iBatis and other lightweight ORMs but it is not officially supported.