简体   繁体   中英

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?

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.

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.

If you happen to be using GlassFish 4.1, try this in the src/main/webapp/web.xml:

<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM