Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 42 web apps Long polling has several variants The simplest version is achieved with the client polling the server in a specified interval Upon receipt of such a request the server responds immediately It either sends the latest data to the client or informs the client that it has no new data to share This simple polling technique works for some applications for which updates are infrequent and displaying stale data is not problematic Another version of long polling the one I discuss in this article is used when the server holds onto the client request and does not respond until the data that the client has asked for is available The principal alternatives to long polling are WebSocket and Server Sent Events SSE WebSocket is currently a widely used alternative It is a standard protocol providing a full duplex communication channel over a single TCP connection One of the biggest advantages of WebSocket is that it can drastically reduce the amount of network trafic between the server and the clients The disadvantages are that not all browsers support it and the older network routers that are optimized for the HTTP protocol can cache or close your WebSocket connection This is why some connection libraries upgrade to the WebSocket protocol if its available and fall back to the longpolling solution if its not An article about using WebSocket for a project similar to the one in this article can be found on page 47 of this issue Ed SSE is another standard technology in which a browser receives automatic updates from a server via an HTTP connection It is designed to be eficient while pushing data to clients The protocol has automatic reconnection and other useful features built in such as automatic tracking of the last seen message Still not all browsers support it and in such cases the web solutions are usually programmed to fall back to using long polling methods As such long polling remains a significant player and a reliable solution It is useful to know how it works and how to implement it eficiently Long Polling Before Servlet 30 Before the Servlet 30 specification there were two serverthreading models thread per connection and thread per request In the thread per connection model a thread is associated with every TCP IP connection and the server can scale to a very high number of requests per second when these requests come from the same set of clients However this model exhibits scalability issues The reason is that for most websites the users initiate an action and then the connection stays mostly idle while users read the pages and decide what to do next Hence the threads tied to a connection are sitting idle To improve scalability the web servers can use a threadper request model In this model after servicing the request the thread can be reused to service a request from a diferent client This model allows for much greater scaling of the user base at the minor expense of increased time servicing each request This expense is due to the thread scheduling that takes place All major web servers today use the thread perrequest model Yet with long polling the diference in scalability between thread per connection and thread per request is blurred This is because each request must wait for data to be available on the server before generating the response Waiting in the servlet is inefficient because the server thread that could be used to service another request is blocked Prior to Servlet 30 this resulted in poor scalability as users were added to the application Servlet 30 Changes Long Polling Servlet 30 introduced asynchronous processing which is a way for servers to process requests particularly those in which a long running operation such as a remote call or an application event must happen before a response can be generated Prior to the Servlet 30 specification a servlet would block a response thread and hold on to other limited resources while it waited for a response to be generated With asynchronous Nowadays web applications are expected to be interactive have a stellarlooking UI and do almost as much as their desktop counterparts
You must have JavaScript enabled to view digital editions.