DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Kotlin Code Style: Best Practices for Former Java Developers
  • Best Date Conversion Approaches in Java 8+
  • Designing Java Web Services That Recover From Failure Instead of Breaking Under Load
  • Oracle NoSQL Database: A Comprehensive Guide for Developers

Trending

  • Docker Hardened Images Are Free Now — Here's What You Still Need to Build
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • The Agentic Agile Office: Streamlining Enterprise Agile With Autonomous AI Agents
  1. DZone
  2. Coding
  3. Java
  4. Harnessing a New Java Web Dev Stack: Play 2.0, Akka, Comet

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

By 
Krishna Prasad user avatar
Krishna Prasad
·
Updated Oct. 11, 22 · Interview
Likes (2)
Comment
Save
Tweet
Share
11.6K Views

Join the DZone community and get the full member experience.

Join For Free


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 server  ,  akka  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  .


Akka (toolkit) Comet (pinball) Web application Java (programming language) dev

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

Opinions expressed by DZone contributors are their own.

Related

  • Kotlin Code Style: Best Practices for Former Java Developers
  • Best Date Conversion Approaches in Java 8+
  • Designing Java Web Services That Recover From Failure Instead of Breaking Under Load
  • Oracle NoSQL Database: A Comprehensive Guide for Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook