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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Oracle NoSQL Database: A Comprehensive Guide for Developers
  • Microsoft Teams for Developers: Enhancing Communication With Call Initiating and Recording
  • Mastering Thread-Local Variables in Java: Explanation and Issues
  • Migrating From Lombok to Records in Java

Trending

  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • How to Convert XLS to XLSX in Java
  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.5K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Oracle NoSQL Database: A Comprehensive Guide for Developers
  • Microsoft Teams for Developers: Enhancing Communication With Call Initiating and Recording
  • Mastering Thread-Local Variables in Java: Explanation and Issues
  • Migrating From Lombok to Records in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!