2011-02-12 48 views
2

我有一種情況,其中我稱這反過來觸發異步HTTP REST調用(後發送狀態到另一個端點)它進一步前進之前的方法。我想的方法,等到我得到響應返回給端點,檢查我得到了狀態,並進一步進行。我正在尋找一種可行的Java解決方案。任何僞代碼或實施將有助於等待異步調用完成第一,然後繼續在Java中

看到了類似的情況@差不多Lightweight way of waiting for a group of asynchronous Java calls但沒有太多的想法是否很容易實現。

實現細節

我有JAX-RS端點來處理異步響應如下

@POST 
@Path("/status") 
@Consumes("application/xml") 
public Response processConfigStatus(@Context UriInfo uriInfo, ConfigStatus configStatus) 
{ 
    // process Status got from the async REST call 
    return ResponseHelper.createNoContentResponse(); 
} 

類,其處理和從處理

Class ProcessData{ 

    public void updateData() 
    checktStatus(uri); 
    update();// should wait untill the processConfigStatus() endpoint gives status 
    } 

    private checktStatus(String uri){ 
    // makes a REST call to a URI using a JAX-RS webclient or a httpclient this returns HTTP 200 or 204 code immediatley. And then the Server process the request asynchronously and gives back the status to JAX-RS endpoint(/status). 
    post(uri); 
    } 

} 

方法調用另一類

ProcessData pd = new ProcessData() 
pd.updateData(); 
+0

和一些代碼也歡迎您 – Bozho 2011-02-12 18:24:55

回答

0

正如你所提供的鏈接,你將不得不實施辦法簡單地跟蹤有多少異步調用消力等待的一個響應,並等待,直到計數爲零。


count = numAsyncCalls 
..calling all of the RESTful services here (each call must have some sort of callback to decrement 'count' variable above.. 
while (count > 0) 
    wait around 

使用在您的鏈接的CountDownLatch看起來幾乎一樣的我的僞代碼

+0

@Bozho,加入了我計劃實施的代碼。 @jerluc,我還沒有在回調中的Java工作,爲實施以上礦井,我們怎麼能有這樣或從processConfigStatus()REST處理程序,我可能需要調用一個方法的過程數據類給狀態。 – ravi 2011-02-12 19:21:09

4

如何使用CountDownLatch

同步協助,允許一個或多個線程等待,直到在其他線程中執行一組操作完成。

相關問題