Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of async-supported in web.xml?

<servlet>         <description>xxx</description>         <servlet-name>xxx</servlet-name>         <servlet-class>com.xxx.yyy</servlet-class>         <async-supported>true</async-supported> </servlet> 

What's the purpose of async-supported in the servlet's web.xml configuration file? What case I can use it in?

like image 419
jiafu Avatar asked Mar 29 '12 05:03

jiafu


People also ask

How does Async servlet work?

In short, an asynchronous servlet enables an application to process incoming requests in an asynchronous fashion: A given HTTP worker thread handles an incoming request and then passes the request to another background thread which in turn will be responsible for processing the request and send the response back to the ...

Which class is used to process the request asynchronously within the servlet?

servlet. AsyncContext class provides the functionality that you need to perform asynchronous processing inside service methods.


2 Answers

Ironically, I was looking for the syntax of how to write this property in tomcat's web.xml and this is the first search item I opened from google - it's written correctly too (it works), so thanks.

To answer your question though, this allows the servlet to store incoming requests for later response. It frees up the thread used to handle the request so it can be used elsewhere until the server is ready to send the response.

For practical purposes, with this configuration you can set-up a servlet that will (in effect) push data to the client (after the client sends the initial request to the server).

This technique replaces the need for unnecessary timed requests from a client to get data that can change at uncertain intervals. And it does it in a scalable manner by not hanging onto the thread.


Some example use-cases include:

Chat applications, when one client types a message you want it to appear instantly to the other client.

Email apps, to allow clients to view e-mails as soon as they are received by the e-mail server.

I've also used it to send input change updates to a browser from a Programming Logic Controller for automation tasks.

Here's a good tutorial on it. This also covers some nut and bolts in java.

like image 94
egerardus Avatar answered Oct 04 '22 17:10

egerardus


The main purpose is to enable XHR streaming as a fallback mechanism to Websockets. If is not explicitly configured to true your application cannot fallback to XHR streaming which will lead to java.lang.IllegalArgumentException: Async support must be enabled on a servlet....

For more in depth information check here (Servlet 3 Async Requests):
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

In order to verify that your configuration is applied correctly limit the number of wesocket connections in your browser to 1 and open your application in more than 1 tab. If the configuration is applied you will have websocket connection established in tab 1 and hxr streaming in tab 2.

like image 41
magiccrafter Avatar answered Oct 04 '22 18:10

magiccrafter