2017-10-10 40 views
0

我尝试使系统,它接受请求,对它们进行一些操作(对相同请求进行分组等),然后以1-2秒的延迟发送响应。Java servlet异步上下文阻止其他请求

因此我尝试使用AsyncContext来防止立即发送响应并在需要时发送它们。

但是,当我使用AsyncContext它阻止下一个响应,而第一个没有完成。 对于我正在发送的测试3 ajax一次获取请求

因此,我的代码不会处理3个未完成的请求,而是等待第一个请求完成,然后与第二个请求一起工作,而第三个等待,直到第二个完成。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    System.out.println("GET " + new Date().toString()); 

    if(!request.getRequestURL().toString().contains("localhost")) { 
     try { 

      AsyncContext context = request.startAsync(); 

      context.start(new Runnable() { 
       @Override 
       public void run() { 

        try { 

         prepare(request, response); 
         addToPool(request, response, context); 

         System.out.println("fin GET " + new Date().toString()); 

        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

在日志中它看起来是这样的:

GET Tue Oct 10 10:16:25 
Try combine: false 
Req added to waitlist 
fin GET Tue Oct 10 10:16:25 
Completed 

GET Tue Oct 10 10:16:30 
Try combine: false 
Req added to waitlist 
fin GET Tue Oct 10 10:16:30 
Completed 

GET Tue Oct 10 10:16:35 
Try combine: false 
Req added to waitlist 
fin GET Tue Oct 10 10:16:35 
Completed 

AsyncContext 768,16像这还是我做错了什么?

P.S. 我尝试使用AsyncContext没有 “context.start(新的Runnable(){...”,我也得到相同的结果

UPDATE。 - JS代码,我就按一下按钮调用multiReq事件。

function multiReq() { 
    getCurrAcc(); 
    getCurrAcc(); 
    getCurrAcc(); 
} 

function getCurrAcc() { 

    $.ajax({ 
     type: "GET", 
     contentType: 'application/json', 
     url: "http://new59d2023b5d1a4.amocrm.ru.myhost.localarea.local/private/api/v2/json/accounts/current", 
     xhrFields: { 
      withCredentials: true 
     }, 
     success: function (data) { 
      alert(JSON.stringify(data)); 
     }, 
     error: function (request, status, error) { 

      alert(JSON.stringify(request)); 
     } 
    }); 
} 
+0

请显示相关部分的javascript –

+0

**可怕的袋熊**,完成 – ToNY

回答

0

的问题是不是在java代码或AsyncContext。 它是由Chrome浏览器的缓存造成的。铬摊位GET请求来获得分组REQ响应。 无缓存选项解析日是问题。