2015-12-02 104 views
2

我在我的請求中使用@Suspended AsyncResponse response,並在處理請求時啓動線程。當進程結束時,我試圖恢復響應,但RestEasy將請求標記爲已完成,因爲請求線程已完成,並且未在響應中設置超時。如果我設置超時,它可以正常工作,但我需要在每個希望實現的異步請求中設置超時。無論如何,水平設置超時到所有暫停的AsyncRequests?在所有異步請求中設置默認超時

回答

0

不幸的是,JAX-RS 2.0 specificationRESTEasy documentationJersey documentation沒有提及爲AsyncResponse設置默認超時值。

Jersey documentation提到下列:

缺省情況下,沒有在懸掛AsyncResponse實例定義超時。自定義超時和超時事件處理程序可以使用setTimeoutHandler(TimeoutHandler)setTimeout(long, TimeUnit)方法來定義。 setTimeoutHandler(TimeoutHandler)方法定義了在超時達到時將被調用的處理程序。處理程序以響應代碼503(來自Response.Status.SERVICE_UNAVAILABLE)恢復響應。還可以定義超時間隔而不指定自定義超時處理程序(僅使用setTimeout(long, TimeUnit)方法)。

因此,該解決方案將無法從您正在使用的解決方案不同:

@GET 
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) { 

    // Register a timeout handler 
    asyncResponse.setTimeoutHandler(new TimeoutHandler() { 

     @Override 
     public void handleTimeout(AsyncResponse asyncResponse) { 
      asyncResponse.resume(Response.status(SERVICE_UNAVAILABLE) 
       .entity("Operation timed out. Please try again.").build()); 
     } 
    }); 

    // Set timeout 
    asyncResponse.setTimeout(15, SECONDS); 

    // Execute long running operation in new thread 
    executor.execute(new Runnable() { 

     @Override 
     public void run() { 
      executeLongRunningOp(); 
      asyncResponse.resume("Hello async world!"); 
     } 
    }); 
}