是的,CXF 是线程安全的,您可以对 Weather 和 WeatherSoap 使用单实例/单例,您可以将 cxf 视为类似于 servlet 引擎,它为您处理所有基础设施,例如传输、数据绑定。我有类似的用例,其中有一个前端表示层和一定数量的网络服务器,为了在这些服务器之间进行交互,我有一个用于表示和 SOAP 的休息,它实现业务逻辑并与服务器交互。因此我在休息层实现了一个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,你可以让事情变得更简单,你可以通过使用如下所示的配置文件来消除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();
}
}