[英]How to execute async calculation and handle other http requests at the same time?
我想同時處理幾個http請求。 這意味着我希望獲取http請求的線程將請求的句柄移動到另一個線程,並且可用於獲取新的請求,直到處理程序提供結果為止。 如果有人可以告訴我如何正確操作,我將不勝感激。 謝謝!
我嘗試使用CompletableFuture,但是顯然我做錯了,因為獲取請求的線程被阻止獲取新請求,直到處理程序完成為止。
如您所見-僅在處理程序完成后(睡眠10秒),請求線程才能獲得新請求,因此處理程序是在另一個線程中執行的,我沒有任何優勢。
@GET
@Path("/{phoneNumber}")
@Produces(MediaType.APPLICATION_JSON)
public Response query() throws InterruptedException, ExecutionException
{
log.debug("get a request");
String message = calculateAsync().get();
return Response.ok(message).build();
}
public Future<String> calculateAsync() throws InterruptedException
{
CompletableFuture<String> completableFuture = new CompletableFuture<String>();
completableFuture = CompletableFuture.supplyAsync(() ->
{
try {
Thread.sleep(10000);
log.debug("finish waiting");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "hello";
});
return completableFuture;
}
2019-06-21 06:38:48,080調試[grizzly-http-server-0] [com.xconnect.np.test.superquery.suppliers.http.QueryResource]-獲取請求2019-06-21 06:38: 58,081 DEBUG [ForkJoinPool.commonPool-worker-1] [com.xconnect.np.test.superquery.suppliers.http.QueryResource]-完成等待2019-06-21 06:38:58,116 DEBUG [grizzly-http-server-0 ] [com.xconnect.np.test.superquery.suppliers.http.QueryResource]-獲取請求2019-06-21 06:39:08,113調試[ForkJoinPool.commonPool-worker-1] [com.xconnect.np.test .superquery.suppliers.http.QueryResource]-完成等待
我找到了一種不使用completableFuture的方法(我仍然不明白它的用處,如果最后您需要調用get()
方法並在其結束之前被阻塞),而是使用@Suspended AsyncResponse response
,如建議的agpt。
這是代碼:
@GET
@Path("/{phoneNumber}")
@Consumes("application/json")
public void submit(@PathParam("phoneNumber") String phoneNumber, final @Suspended AsyncResponse response1) {
log.debug("get a request " +phoneNumber);
new Thread() {
public void run() {
String confirmation = process(phoneNumber);
Response response = Response.ok(confirmation,
MediaType.APPLICATION_XML_TYPE)
.build();
response1.resume(response);
}
}.start();
log.debug("the submit finish " + phoneNumber);
}
public String process(String phoneNumber)
{
try {
Thread.sleep(10000);
log.debug("finish waiting "+phoneNumber);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String res = "hello" + phoneNumber;
return res;
}
2019-06-23 09:54:20,157調試[grizzly-http-server-0] [com.xconnect.np.test.superquery.suppliers.http.QueryResource]-獲取請求1 2019-06-23 09:54 :20,158 DEBUG [grizzly-http-server-0] [com.xconnect.np.test.superquery.suppliers.http.QueryResource]-提交完成1 2019-06-23 09:54:22,026 DEBUG [grizzly-http-服務器0] [com.xconnect.np.test.superquery.suppliers.http.QueryResource]-獲取請求2 2019-06-23 09:54:22,026調試[grizzly-http-server-0] [com.xconnect .np.test.superquery.suppliers.http.QueryResource]-提交完成2 2019-06-23 09:54:30,158調試[Thread-2] [com.xconnect.np.test.superquery.suppliers.http.QueryResource ]-完成等待1 2019-06-23 09:54:32,027調試[Thread-3] [com.xconnect.np.test.superquery.suppliers.http.QueryResource]-完成等待2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.