欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet3.0 异步servlet

程序员文章站 2022-07-13 13:54:16
...
假设一种情况,你的一个servlet会处理比较长的时间,而且这个servlet请求数量也挺多的,那么久会造成tomcat线程池被占用满了,造成其他用户的请求等待队列过多,时间过长。

用例子实现
首先需要开启
<servlet>
    <servlet-name>webProxyMain</servlet-name>
    <servlet-class>com.lsiding.nat.server.web.WebProxyMain</servlet-class>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>


或者


@WebServlet(value = "/asyncTest", asyncSupported=true)



package com.lsiding.admin4j.act;

import java.io.IOException;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AsyncTest
 */
@WebServlet(value = "/asyncTest.html", asyncSupported = true)
public class AsyncTest extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AsyncTest() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 必须
		// 否则可能会 A filter or servlet of the current chain does not support
		// asynchronous operations.
		request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
		AsyncContext asyncCtx = request.startAsync();
		asyncCtx.addListener(new AppAsyncListener());
		asyncCtx.setTimeout(9000);
		asyncCtx.start(
		new Thread() {
			public void run() {
				try {
					try {
						asyncCtx.getResponse().getOutputStream()
								.println("thread");
						asyncCtx.getResponse().getOutputStream().flush();
					} catch (IOException e) {
					}
					Thread.sleep(8000);
					asyncCtx.complete();
				} catch (InterruptedException e) {
				}
				;
			}
		});
		response.getOutputStream()
				.write("请求方法返回了,但是response是否结束了呢?".getBytes());
		response.getOutputStream().flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}




package com.lsiding.admin4j.act;

import java.io.IOException;

import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;

public class AppAsyncListener implements AsyncListener {
	@Override
	public void onComplete(AsyncEvent asyncEvent) throws IOException {
		asyncEvent.getAsyncContext().getResponse().getOutputStream().println("onComplete");
		asyncEvent.getAsyncContext().getResponse().getOutputStream().flush();
	}

	@Override
	public void onError(AsyncEvent asyncEvent) throws IOException {
		asyncEvent.getAsyncContext().getResponse().getOutputStream().println("onError");
		asyncEvent.getAsyncContext().getResponse().getOutputStream().flush();
	}

	@Override
	public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
		asyncEvent.getAsyncContext().getResponse().getOutputStream().println("onStartAsync");
		asyncEvent.getAsyncContext().getResponse().getOutputStream().flush();
	}

	@Override
	public void onTimeout(AsyncEvent asyncEvent) throws IOException {
		asyncEvent.getAsyncContext().getResponse().getOutputStream().println("asyncEvent");
		asyncEvent.getAsyncContext().getResponse().getOutputStream().flush();
	}
}