Servlet3.0之异步支持_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Servlet3.0之异步支持

Servlet3.0之异步支持

 2012/2/17 9:26:20  alex.Lin  程序员俱乐部  我要评论(0)
  • 摘要:将该部分单独拧出来,方便查看3.3异步的支持3.3.1AsyncContext接口ServletRequest执行startAsync()或者startAsync(ServletRequest,ServletResponse)进入异步模式,将会创建一个AsyncContext,将其需要异步的操作放在此AsyncContext中addListener(AsyncListenerlistener)//可以增加监听器3.3.2ServletRequest接口startAsync(
  • 标签:Servlet

将该部分单独拧出来,方便查看

3.3???? 异步的支持

3.3.1? AsyncContext接口

ServletRequest执行startAsync()或者startAsync(ServletRequest, ServletResponse) 进入异步模式,将会创建一个AsyncContext,将其需要异步的操作放在此AsyncContext

?

addListener(AsyncListener?listener)//可以增加监听

3.3.2? ServletRequest接口

startAsync()

startAsync(ServletRequest, ServletResponse)

将当前请求/响应 或指定请求/响应 放到异步模式中

3.3.3? AsyncListener异步监听器

void

onComplete(AsyncEvent?event)
??????????Notifies this AsyncListener that an asynchronous operation has been completed.

?void

onError(AsyncEvent?event)
??????????Notifies this AsyncListener that an asynchronous operation has failed to complete.

?void

onStartAsync(AsyncEvent?event)
??????????Notifies this AsyncListener that a new asynchronous cycle is being initiated via a call to one of the
ServletRequest#startAsync methods.

?void

onTimeout(AsyncEvent?event)

Notifies this AsyncListener that an asynchronous operation has timed out.

?

可以给AsyncContext实例增加一个AsyncListener监听器,当AsyncContext调用complete()完成后即可触发onComplete(e)方法

3.3.4? 例子???

asyncSupported要定义为true

?

3.3.4.1 简单例子

@WebServlet(name = "syncServlet", value = { "/syncServlet"},asyncSupported=true)

public class SyncTestServlet extends HttpServlet {

?????? private static final long serialVersionUID = -126107068129496624L;

?????? public void doGet(HttpServletRequest request, HttpServletResponse response)

???????????????????? throws ServletException, IOException {

?

????????????? response.setContentType("text/html");

????????????? PrintWriter out = response.getWriter();

????????????? out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

????????????? out.println("<HTML>");

????????????? out.println("? <HEAD><TITLE>A Servlet</TITLE></HEAD>");

????????????? out.println("? <BODY>");

????????????? out.print("??? Servlet Start:::<br>This is ");

????????????? out.print(this.getClass());

????????????? out.println("<br>using the GET method<br>");

????????????? out.flush();

????????????? //-------------增加一个异步线程

????????????? System.out.println("response对象:"+response);

????????????? AsyncContext sc = request.startAsync(request,response);

????????????? Thread t = new Thread(new MyThread(sc));

????????????? t.start();

?????????????

?????????????

????????????? //-------------继续主线程

//注:主线程不需要等待子线程完毕,可以先执行并打印出来【异步进行】

????????????? out.println(" &nbsp;-------Servlet end.");???????

????????????? out.flush();

?????????????

?????? }

}

?

class MyThread extends Thread{

?????? private AsyncContext syncContext;

??????

?????? public MyThread(AsyncContext syncContext){

????????????? this.syncContext = syncContext;

?????? }

??????

?????? public void run(){

????????????? System.out.println("MyThread start2....");

????????????? HttpServletResponse resp = (HttpServletResponse) syncContext.getResponse();

????????????? System.out.println("MyThread end2....");

????????????? try {

???????????????????? PrintWriter out = resp.getWriter();

???????????????????? out.println("<br><br>-----MyThread start----");

???????????????????? out.println("<br>-----wait running.....");

???????????????????? out.flush();//flush即可先输出,不需要等全部完成

???????????????????? System.out.println("子线程里的response对象(和主线程是同一个):"+resp);

???????????????????? Thread.sleep(3000);

???????????????????? out.println("<br>-----MyThread end----");

???????????????????? out.println("? </BODY>");

???????????????????? out.println("</HTML>");

???????????????????? out.flush();

???????????????????? //out.close();

????????????? } catch (IOException e) {

???????????????????? // TODO Auto-generated catch block

???????????????????? e.printStackTrace();

????????????? } catch (InterruptedException e) {

???????????????????? // TODO Auto-generated catch block

???????????????????? e.printStackTrace();

????????????? }

????????????? syncContext.complete();//完成异步操作

?????? }

}

发表评论
用户名: 匿名