Welcome!

Java Authors: Pat Romanski, Elizabeth White, Yeshim Deniz, Brian Lavallée, Liz McMillan

Related Topics: Java, Open Source, AJAX & REA

Java: Blog Post

Long Polling Explained

How long polling could be used in Spring MVC web applications

This article defines long polling and presents a few techniques on how long polling could be used in Spring MVC web applications. Discussion covers both the AJAX and the server part of the solution as well as outlines a few possible options for implementation. The article compares performance of the two server side approaches and describes a practical example of long polling application.

Introduction

On daily basis we are using third party web applications, such as Gmail, or Google docs, which update web page content in real time. How can this be done in our own applications? There are a few techniques that can show real live updates in the web application: server push, long polling, web events, and web sockets to name a few. This article will specifically focus on long polling as a potential solution for real time web updates.

Implementations of long polling is also known as Comet.  Long polling emulates server push mechanism. It is a technique to update the page content by continuously polling the server for new information. The page first initiates the request to the server, and the server then holds on to the request until the new information is available, or until the request expires. As server responds to the request, the information is displayed on the web page. As request is processed, the page issues another request to the server. This article will discuss a few examples of long polling implementation and will explore both sides of the processing logic AJAX on front end and server side code.

News Polling App, pre Servlet 3.0

The first example explores a long polling solution on pre-Servlet 3.0 web server, a server where asynchronous servlets are not supported. In this example the server to broadcast news, and a web page that continuously updates as the news becomes available.  The solution implemented as a web application that consists of 3 components: news publishing service that generates news; long wait controller -- a Spring MVC controller that handles the web requests; and a web page that displays the news and has AJAX code that continuously polls for news. To emulate the real time updates without inundating the web server with requests, the web server needs to hold on to the request and not respond to the user until new information is available. To do this, the thread handling the request needs to be paused. It's not the ideal situation, since each web server only has so many threads available to process request, but that's the only way long polling is done without asynchronous servlets.

polling image

 

More Stories By Henry Naftulin

Henry Naftulin is seasoned, result-oriented enterprise senior software developer with 10 years of experience in enterprise scale software applications. He is highly skilled in Java (J2EE), Web and database enterprise application development. Recognized as strong team player, technical leader and mentor for junior team members. Latest interests include e-Logistics/Supply Chain, financial applications and performance tuning.