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));
}
});
}
請顯示相關部分的javascript –
**可怕的袋熊**,完成 – ToNY