2012-06-06 41 views
0

我是websevices的新手。我的要求是調用不同供應商提供的不同的web服務,並在java類中輸入相同的輸入。例如:天氣信息由不同供應商提供,所有供應商都將輸入視爲城市名稱。我想調用一個java類中的方法,它調用由不同供應商提供的並行的所有Web服務。然後,我必須以供應商(所有使用axis2的供應商)的jsp顯示結果。我怎樣才能調用不同供應商提供的多種web服務在java中輸入相同

回答

0

這應該不難,如果你有相同的輸入。

假設你正在使用的Axis2的Java API,你可以這樣做:

public class Test { 

    public static void main(String[] args) throws AxisFault,Exception { 

     RPCServiceClient serviceClient = new RPCServiceClient(); 
     Options options = serviceClient.getOptions(); 

     String [] vendors = {"http://example.com/vendor1/services/GETWeatherHttpSoap12Endpoint", "http://example.com/vendor2/services/GETWeatherHttpSoap12Endpoint"}; // Array of all vendors 

     for (String vendor : vendors) { 
      EndpointReference targetEPR = new EndpointReference(vendor); 
      options.setTo(targetEPR); 
      QName opGetExchange = new QName("http://ws.apache.org/axis2", "getWeather"); 

      // preparing the parameters 
      String country = "USA"; 
      Object[] opGetExchangeArgs = new Object[] {country}; 

      // preparing the return type 
      Class[] returnTypes = new Class[] { String.class }; 

      // invoking the service passing in the arguments and getting the response 
      Object[] response = serviceClient.invokeBlocking(opGetExchange, opGetExchangeArgs, returnTypes); 
      // obtaining the data from the response 
      String result = (String) response[0]; 

      System.out.println("Vendor: " + result); 
     } 
    } 
} 

這將打印出結果,從每個供應商在另一條線路。

+0

嗨,如果一個web服務失敗是否有任何影響的結果?我的意思是餘下的結果將顯示或總的請求將失敗?我們可以在調用不同供應商提供的不同方法時使用超時。如果需要時間,那麼我們可以顯示其他結果? –

+0

是的。如果一個web服務失敗,它會拋出一個異常。您將必須專門爲供應商捕捉異常,然後可能會顯示一條錯誤消息。如果你想提高速度,你可以考慮使用線程並行運行它們,然後將結果提交到一個共同的端點 –

+0

Thanks..can請詳細說明使用線程的例子。 –

相關問題