Implementing long polling in an asynchronous fashion

前端 未结 2 835
迷失自我
迷失自我 2020-12-09 06:42

Is it possible to take an HTTPServletRequest away from its thread, dissolve this thread (i.e. bring it back to the pool), but keep the underlying connection with the browser

相关标签:
2条回答
  • 2020-12-09 07:14

    Yes, it's possible using Servlet spec ver. 3.0. Implementation I can recommend is Jetty server. See here.

    0 讨论(0)
  • 2020-12-09 07:27

    Yes, you can do this with Servlet 3.0

    Below is the sample to write the alert every 30 secs(not tested).

    @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);
    }
    }
    

    Below is the sample to write based on an event. The alertManager has to be implemented which notifies AlertNotificationHandler when client has to be alerted.

    @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
                             }
                       }
            });
      }
    }
    
    0 讨论(0)
提交回复
热议问题