使用WebServiceGatewaySupport处理对多个Web服务的请求

时间:2014-09-25 16:24:11

标签: soap spring-ws

我使用spring-ws-core构建SOAP客户端。为此,我正在扩展WebServiceGatewaySupport以进行服务调用。

public class WeatherClient extends WebServiceGatewaySupport {
...
    public WeatherResponse getCityForecastByZip(String zipCode) {
        GetCityForecastByZIP request = new GetCityForecastByZIP();
        request.setZIP(zipCode);

        GetCityForecastByZIPResponse response = (GetCityForecastByZIPResponse) this.getWebServiceTemplate().marshalSendAndReceive(request,
                new SoapActionCallback("http://ws.cdyne.com/WeatherWS/GetCityForecastByZIP"));

        return response;
    }
...
}

Spring配置非常简单

@Configuration
public class WebServicesConfiguration {

    private static final String WEATHER_SERVICE_DEFAULT_URI = "...";


    @Bean(name = "servicesMarshaller")
    public Jaxb2Marshaller servicesMarshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("some.package");
        return marshaller;
    }

    @Bean
    public WeatherClient weatherService(@Qualifier("servicesMarshaller") Jaxb2Marshaller marshaller) {
        WeatherClient client = new WeatherClient(WEATHER_SERVICE_DEFAULT_URI);
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

}

这适用于单个Web服务。现在,假设我有许多类似的Web服务,但每个服务都有自己的.wsdl规范和URI。我知道我可以通过Spring WebServiceTemplate进行服务调用并指定要使用的URI。所以我的想法是使用单个WebServiceGatewaySupport来处理对不同服务的所有调用。在每次调用中,我都会传递soap操作,相应的请求(如果有)和Web服务URL。我的应用程序假设在多线程环境中运行。

使用单个WebServiceGatewaySupport来处理对不同URI的并发调用是一种好习惯吗?

1 个答案:

答案 0 :(得分:1)

查看WebServiceGatewaySupport源代码,简短的asnwer:是的,可以将它用于不同的URL,以及底层的WebServiceTemplate是线程安全的。

如果您不在请求之间保存某些状态,那么您的实现也将是线程安全的。