如何以线程安全的方式使用CXF客户端

pra*_*eth 6 java wsdl web-services cxf wsdl2java

我使用apache-cxf的wsdl2java命令为以下服务创建了客户端存根. http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL


然后我调用如下getWeatherInformation()方法.

Weather weatherService = new Weather();
WeatherSoap weatherSoap = weatherService.getWeatherSoap();
ArrayOfWeatherDescription result = weatherSoap.getWeatherInformation();
Run Code Online (Sandbox Code Playgroud)

我已经读过cxf客户端是线程安全的.但我怀疑WeatherSoap在多个线程中使用相同的实例是否安全?或者我应该/可以使用Weather多个线程的类实例吗?谢谢.

编辑:


我所做的是我向公众公开了RESTful API,如果有人称之为休息服务,我会调用另一个SOAP服务.上面的代码用于调用SOAP服务.我想知道的是我应该为每个休息请求执行以上所有行,还是可以重用所有REST请求的实例WeatherWeatherSoap服务所有REST请求.

Kar*_*sad 5

是的,CXF是线程安全的,您可以对Weather和WeatherSoap使用单个实例/单个实例,可以将cxf视为类似于servlet引擎,该servlet引擎为您处理所有基础结构,例如传输,数据绑定。我有类似的用例,其中有一个前端表示层和许多网络服务器,可以在它们之间进行交互,其余部分用于表示和实现业务逻辑以及与服务器交互的SOAP。因此,我在休息层实现了一个肥皂客户端。我的要求是我需要分割休息请求并调用具有800ms时间延迟的并行soap调用。我对整个设置进行了性能测试,并且没有遇到任何线程问题。

因此,进入客户实施

纯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;
  }

}
Run Code Online (Sandbox Code Playgroud)

然后,我将修改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

}
Run Code Online (Sandbox Code Playgroud)

使用Spring

如果使用spring,则可以使事情变得更简单,可以使用如下所示的配置文件消除Weather.java类,并让cxf为您生成代理。

<jaxws:client id="weatherSoap" serviceClass="com.cdyne.ws.weatherws.WeatherSoap" address="${app.weather.url}" />
Run Code Online (Sandbox Code Playgroud)

和商务舱如下所示。

@Component
MyBusinessLogic{

  @Autowired
  private WeatherSoap weatherSoap;

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

}
Run Code Online (Sandbox Code Playgroud)