java servlet 3.0,Java - Servlet 3.0中的异步与Servlet 3.1中的NIO

Until now, as it applies to serving http requests, I thought the terms - asynchronous and non-blocking i/o meant the same thing. But apparently, they have been implemented separately in servlet 3.0 and 3.1 respectively. I am struggling to understand the difference here...

Can someone shed more light on this topic, please? Specifically, I am looking for an example of how a servlet 3.0 implementation of a server can be async, yet block on a thread? I think may be if I understand this, it may be easier to understand the exact problem that the non-blocking i/o in servlet 3.1 is trying to solve.

解决方案

I will try to summarize what I learned. To understand the problem that Servlet 3.0 and Servlet 3.1 solve, let's look at it this way:

Prior to Servlet 3.0:

The problem with synchronous processing of requests is that it resulted in threads (doing heavy-lifting) running for a long time before the response goes out. If this happens at scale, the servlet container eventually runs out of threads - long running threads lead to thread starvation.

Prior to Servlet 3.0, there were container specific solutions for these long running threads where we can spawn a separate worker thread to do the heavy task and then return the response to client. The servlet thread returns to the servlet pool after starting the worker thread. Tomcat’s Comet, WebLogic’s FutureResponseServlet and WebSphere’s Asynchronous Request Dispatcher are some of the example of implementation of asynchronous processing.

(See link 1 for more info.)

Servlet 3.0 Async:

The actual work could be delegated to a thread pool implementation (independent of the container specific solutions). The Runnable implementation will perform the actual processing and will use the AsyncContext to either dispatch the request to another resource or write the response. We can also add AsyncListener implementation to the AsyncContext object to implement callback methods.

(See link 1 for more info.)

Servlet 3.1 NIO:

As described above, Servlet 3.0 allowed asynchronous request processing but only traditional I/O (as opposed to NIO) was permitted. Why is traditional I/O a problem?

In traditional I/O, there are two scenarios to consider:

If the data coming into the server (I/O) is blocking or streamed slower than the server can read, then the server thread that is trying to read this data has to wait for that data.

On the other hand, if the response data from the server written to ServletOutputStream is slow, the client thread has to wait. In both cases, the server thread doing the traditional I/O (for requests/responses) blocks.

In other words, with Servlet 3.0, only the request processing part became async, but not the I/O for serving the requests and responses. If enough threads block, this results in thread starvation and affects performance.

With Servlet 3.1 NIO, this problem is solved by ReadListener and WriteListener interfaces. These are registered in ServletInputStream and ServletOutputStream. The listeners have callback methods that are invoked when the content is available to be read or can be written without the servlet container blocking on the I/O threads. So these I/O threads are freed up and can now serve other request increasing performance.

(See link 2 for more info.)

Credits

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.org.servlet3; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; /** * @project servlet3.0 * servlet3.0的文件上传 * @date:2012-5-21 *在创建项目的时候首先添加Tomcat7.x的支持,然后把apache-tomcat-7.0.27\conf\web.xml拷贝到项目WEB-INF目录下 *把之前的web.xml覆盖.. 配置留下 <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> */ @SuppressWarnings("all") @WebServlet(name="fileUploadServlet",urlPatterns="/fileUploadServlet") @MultipartConfig(maxRequestSize=222222)//设置文件上传大小 public class FileUploadServlet extends HttpServlet { /** *访问 *http://localhost:8080/servlet3.0/ */ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); Part part=req.getPart("file"); //获取页面的name //System.out.println(part.getName()); System.out.println(System.getProperty("user.dir"));//输出当前的项目存放的路径 String uploadPath=req.getSession().getServletContext().getRealPath("/upload"); System.out.println(uploadPath);//输出上传的文件路径 String value=part.getHeader("content-disposition");//设置头信息 System.out.println(value); String sub=value.substring(value.lastIndexOf("=")+2,value.length()-1);//截取文件 System.out.println("file size: \t"+part.getSize());//文件的大小 part.write(uploadPath+sub);//写入文件 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值