簡體   English   中英

如何以線程安全的方式使用CXF客戶端

[英]How to use CXF client in thread safe way

我使用apache-cxf的wsdl2java命令為以下服務創建了客戶端存根。 http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL


然后我調用getWeatherInformation()方法,如下所示。

Weather weatherService = new Weather();
WeatherSoap weatherSoap = weatherService.getWeatherSoap();
ArrayOfWeatherDescription result = weatherSoap.getWeatherInformation();

我已經讀過cxf客戶端是線程安全的。 但我懷疑在多個線程中使用相同的WeatherSoap實例是否安全? 或者我應該/可以使用Weather類的實例,跨越多個線程嗎? 謝謝。

編輯:


我所做的是我向公眾公開了RESTful API,如果有人稱之為休息服務,我會調用另一個SOAP服務。 上面的代碼用於調用SOAP服務。 我想知道的是我應該為每個休息請求執行以上所有行,還是可以重復使用WeatherWeatherSoap的實例來處理所有REST請求。

是的CXF是線程安全的,你可以使用單個實例/單獨的Weather和WeatherSoap,你可以認為cxf類似於servlet引擎,它為你處理所有基礎設施,如傳輸,數據綁定。 我有類似的用例,我有一個前端表示層和多個網絡服務器,在這些之間進行交互我有一個休息的演示文稿和SOAP實現業務邏輯以及與服務器交互。 因此我在rest層中實現了一個soap客戶端。 我有要求,我需要拆分休息請求,並調用時間延遲800毫秒的並行肥皂調用。 我的性能測試了整個設置,沒有遇到任何線程問題。

所以進入客戶端實現

純Java

public class MySoapClient{

  private static WeatherSoap weatherSoap;

  private MySoapClient(){
  }

  public static WeatherSoap getClient(){

    if(weatherSoap==null){
        Weather weatherService = new Weather();
        weatherSoap= weatherService.getWeatherSoap();
    }
    return weatherSoap;
  }

}

我會修改Weather類以從屬性文件中獲取SOAP URL。

@WebServiceClient(name = "Weather", 
                  wsdlLocation = "classpath:weather.wsdl",
                  targetNamespace = "http://ws.cdyne.com/WeatherWS/") 
public class Weather extends Service {

    private static final Logger LOG = LoggerFactory.getLogger(Weather.class);
    public final static URL WSDL_LOCATION;
    public final static QName SERVICE = new QName("http://ws.cdyne.com/WeatherWS/", "Weather");
    public final static QName WeatherHttpPost = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherHttpPost");
    public final static QName WeatherHttpGet = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherHttpGet");
    public final static QName WeatherSoap12 = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherSoap12");
    public final static QName WeatherSoap = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherSoap");
    static {
        URL url = null;
        try {
            url = new URL(MyPropertiesUtil.getProperty("app.weather.url"));
        } catch (MalformedURLException e) {
            LOG.error(e.getMessage(), e);
        }
        if (url == null) {
            LOG.error("an issue with your url");
        }       
        WSDL_LOCATION = url;   
    }

    public Weather(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public Weather(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public Weather() {
        super(WSDL_LOCATION, SERVICE);
    }


    //All the other interface methods

}

使用Spring

如果你使用spring你可以使事情變得更簡單,你可以使用配置文件消除Weather.java類,如下所示,讓cxf為你生成代理。

<jaxws:client id="weatherSoap" serviceClass="com.cdyne.ws.weatherws.WeatherSoap" address="${app.weather.url}" />

商務艙看起來如下。

@Component
MyBusinessLogic{

  @Autowired
  private WeatherSoap weatherSoap;

  public ArrayOfWeatherDescription getOutput(){
    return weatherSoap.getWeatherInformation();
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM