繁体   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