Servlet 3.0笔记之异步请求相关方法和AsyncContext转发

在ServletRequest中增加的有关异步相关方法分为:
  1. startAsync(servletRequest, servletResponse) 传入指定的request和response对象,便于在AsyncContext中重复使用(这样被Filter、Servlet包装过的请求、相应对象才会在异步的环境下继续生效)。
  2. startAsync() 若request或者response被包装,将失去预期的效果。
  3. isAsyncSupported()isAsyncStarted()
    辅助性的方法,用于判断当前请求是否支持异步或者异步已经开始。
  4. getAsyncContext()
    需要在异步启动之后才能够访问,否则会报异常。
在AsyncContext中分发的方法有三个,不太容易区分:
  1. AsyncContext.dispatch()
    若当前AsyncContext由ServletRequest.startAsync(ServletRequest, ServletResponse)方法启动,返回的地址可以通过HttpServletRequest.getRequestURI()得到。
    否则,分发的地址则是当前URL request对象最后一次分发的地址。
    虽有些拗口,两者分发的地址大部分情况下一致;但尽量使用带有参数的异步上下文启动器。
    如本例中请求/asyncDispatch2Async?disUrl=self,执行dispatch()方法之后,自身会再次分发到自身,包括传递的参数。
  2. AsyncContext.dispatch(String path)
    等同于ServletRequest.getRequestDispatcher(String)算是一个快捷方法。
    可以转向一个同步或异步的servlet,或者JSP,或其它资源地址等。
  3. AsyncContext.dispatch(ServletContext context, String path)
    请求的地址将在给定的上下文里面(ServletContext),有有可能传入的上下文与当前站带你应用的上下文有所区别。
展示一个较为有趣、但没有多少实际意义的小示范:
/**
 * 异步上下文的转向分发
 * 
 * @author yongboy
 * @date 2011-1-14
 * @version 1.0
 */
@WebServlet(urlPatterns = { "/asyncDispatch2Async" }, asyncSupported = true)
public class AsyncContextDispatch2AsyncServlet extends HttpServlet {
 private static final long serialVersionUID = 46172233331022236L;
 private static final Log log = LogFactory
   .getLog(AsyncContextDispatch2AsyncServlet.class);

 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  response.setHeader("Cache-Control", "private");
  response.setHeader("Pragma", "no-cache");
  response.setHeader("Connection", "Keep-Alive");
  response.setHeader("Proxy-Connection", "Keep-Alive");
  response.setContentType("text/html;charset=UTF-8");

  PrintWriter out = response.getWriter();
  out.println("
Start ...
"); out.flush(); if (!request.isAsyncSupported()) { log.info("the servlet is not supported Async"); return; } request.startAsync(request, response); if (request.isAsyncStarted()) { AsyncContext asyncContext = request.getAsyncContext(); asyncContext.setTimeout(1L * 60L * 1000L);// 60sec new CounterThread(asyncContext).start(); } else { log.error("the ruquest is not AsyncStarted !"); } } private static class CounterThread extends Thread { private AsyncContext asyncContext; public CounterThread(AsyncContext asyncContext) { this.asyncContext = asyncContext; } @Override public void run() { int interval = 1000 * 20; // 20sec try { log.info("now sleep 20s, just as do some big task ..."); Thread.sleep(interval); log.info("now dispatch to another Async Servlet"); ServletRequest request = asyncContext.getRequest(); String disUrl = request.getParameter("disUrl"); if (StringUtils.isBlank(disUrl)) { disUrl = "/demoAsyncLink"; } if (disUrl.endsWith(".jsp")) { request.setAttribute("dateStr", DateFormatUtils.format( System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } log.info("disUrl is : " + disUrl); // 将当前异步上下文所持有的request, response分发给Servlet容器 if (StringUtils.equals("self", disUrl)) { // 将分发到自身,即当前异步请求地址 asyncContext.dispatch(); } else { // 将分发到指定的路径 asyncContext.dispatch(disUrl); } } catch (InterruptedException e) { e.printStackTrace(); } } } }
当前请求完成之后,将分发到下一个请求上面,若都是异步的Servlet,则很好的组成了异步Servlet请求链。
有趣的地方在于,异步上下文环境可以分发到下一个异步或同步的servlet、jsp、html等资源。若访问类似于如下地址,当前URL永远不会断开,又一个永动机,除非网络链接出错或者服务器关闭。
http://localhost/servlet3/asyncDispatch2Async?disUrl=self
一个视图:
image
上面的异步Servlet总是在不断的请求自我,成为了一个永动机;为disUrl传入要转发到的异步或同步的资源地址,组成一个链的模式,相当的简单轻松。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值