以异步方式实现长轮询

时间:2011-11-10 15:24:53

标签: java multithreading spring servlets long-polling

是否可以从其线程中取出HTTPServletRequest,解散此线程(即将其带回池中),但保持与浏览器的底层连接正常工作,直到我从耗时的操作中获得结果(说,处理图像)?处理返回数据时,应异步调用另一个方法,并将请求和数据作为参数给出。

通常,长池功能以非常阻塞的方式运行,当前线程不会被解散,这会降低服务器端应用程序在并发连接方面的可伸缩性。

2 个答案:

答案 0 :(得分:5)

是的,您可以使用Servlet 3.0

执行此操作

以下是每隔30秒(未测试)编写警报的示例。

@WebServlet(async =“true”)
public class AsyncServlet extends HttpServlet {

Timer timer = new Timer("ClientNotifier");

public void doGet(HttpServletRequest req, HttpServletResponse res) {

    AsyncContext aCtx = request.startAsync(req, res);
    // Suspend request for 30 Secs
    timer.schedule(new TimerTask(aCtx) {

        public void run() {
            try{
                  //read unread alerts count
                 int unreadAlertCount = alertManager.getUnreadAlerts(username); 
                  // write unread alerts count
    response.write(unreadAlertCount); 
             }
             catch(Exception e){
                 aCtx.complete();
             }
        }
    }, 30000);
}
}

以下是基于事件编写的示例。必须实施alertManager,以便在必须提醒客户端时通知AlertNotificationHandler。

@WebServlet(async=“true”)
public class AsyncServlet extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) {
        final AsyncContext asyncCtx = request.startAsync(req, res);
        alertManager.register(new AlertNotificationHandler() {
                   public void onNewAlert() { // Notified on new alerts
                         try {
                               int unreadAlertCount =
                                      alertManager.getUnreadAlerts();
                               ServletResponse response = asyncCtx.getResponse();
                               writeResponse(response, unreadAlertCount);
                               // Write unread alerts count
                         } catch (Exception ex) {
                               asyncCtx.complete();
                               // Closes the response
                         }
                   }
        });
  }
}

答案 1 :(得分:2)

是的,可以使用Servlet规范。 3.0。我可以推荐的实现是Jetty服务器。请参阅here

相关问题