初學者在這裏。如何在單獨的線程中執行大量WS調用並增加單個結果變量?
我需要在Struts動作中通過每次調用webservice多次使用不同的請求來檢索一些數據,並等待每個請求完成,以便我可以顯示結果。
所以基本上我在做這樣的:
// The list of the region codes used for the requests
List<String> codesRegions;
// Init of a variable containing the total amount of data
Integer totalAmount = 0;
// For every region
for(String codeRegion : codesRegions)
{
MyRegionStub stub = createRegionStub();
// Call of the WS with the code region
MyRegionRequest request = new MyRegionRequest();
request.setCodeRegion(codeRegion);
// Getting the number associated to the region and incrementing totalAmount
MyRegionResponse response = stub.getRegionStats(request);
totalAmount += response.getRegionStats();
}
// Once all the calls are done, I display the result
request.setAttribute("totalAmount", totalAmount);
mapping.findForward("success");
我的問題是,我經常這樣做,因爲我調用這個WS很多次得到超時錯誤。所以我想知道如何在一個單獨的線程中完成每個調用,並且不加任何錯誤地增加相同的結果變量。
我想知道的另一件事,我需要等待所有的呼叫完成顯示數據。只有完成此操作後,我需要做什麼才能撥打mapping.findForward("success");
?
非常感謝您的回答。不幸的是,我正在使用舊版本的Java(Java 6)開發一箇舊項目,所以我不能使用這個解決方案......但我會在後面的目的中記住它! – Someday