使用Spring异步调用SOAP服务

时间:2018-02-02 05:25:24

标签: spring web-services spring-integration webservicetemplate

我要异步调用SOAP Web服务。目前,我正在使用Spring webservicetemplate以同步方式调用它。

当前配置如下:

    <bean id="interceptedHttpClientBuilder" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="httpClientBuilder" />
        <property name="targetMethod" value="addInterceptorFirst"> </property>
        <property name="arguments">
            <list>
                <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender.RemoveSoapHeadersInterceptor"/>
            </list>
        </property>
    </bean>

    <bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig" factory-method="custom">
        <property name="socketTimeout" value="120000" />
    </bean>

    <bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />
    <bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />

    <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create">
        <property name="defaultRequestConfig" ref="requestConfig" />
    </bean>

    <bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
        <constructor-arg ref="httpClient"></constructor-arg>
    </bean>

    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="packagesToScan">
            <list><value>...</value></list>
        </property>
    </bean>

    <bean id="wsClientSecurityInterceptor"  
        class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">  
        <property name="securementActions" value="UsernameToken" />  
        <property name="securementUsername"><value>${username}</value></property>  
        <property name="securementPassword"><value>${password}</value></property>
        <property name="securementPasswordType" value="PasswordText" />
    </bean>

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <property name="marshaller" ref="jaxb2Marshaller"></property>
        <property name="unmarshaller" ref="jaxb2Marshaller"></property>
        <property name="defaultUri"><value>${ws.url}</value></property>
        <property name="interceptors">  
            <list>  
                <ref local="wsClientSecurityInterceptor"/>  
            </list>  
        </property>
        <property name="messageSender" ref="messageSender"></property>
    </bean>

Java调用看起来像:

MyResponse response = (MyResponse) webServiceTemplate.marshalSendAndReceive(req, new WebServiceMessageCallback() {
                        public void doWithMessage(WebServiceMessage message) {
                        ((SoapMessage) message).setSoapAction("test");
                        }
                    });

我可以知道如何更改它以异步方式调用服务?或者我是否需要在春季使用其他东西来实现这一目标?

1 个答案:

答案 0 :(得分:0)

不确定为什么在您的问题中使用spring-integration标记,但如果我们在此,请务必查看@MessagingGateway并将Future<>作为返回类型:{ {3}}

可以通过Spring Integration <int-ws:outbound-gateway>调用SOAP WebService:https://docs.spring.io/spring-integration/docs/5.0.1.RELEASE/reference/html/messaging-endpoints-chapter.html#async-gateway

样本在这里:https://docs.spring.io/spring-integration/docs/5.0.1.RELEASE/reference/html/ws.html

为了更清楚,代码可能如下所示:

<int:gateway id="mathService" 
             service-interface="org.springframework.integration.samples.async.gateway.MathServiceGateway"
             default-request-channel="requestChannel"
             async-executor="executor"/>

MathServiceGateway的含义如下:

public interface MathServiceGateway {

    Future<Integer> multiplyByTwo(int i);
}

WS调用也很简单:

<int-ws:outbound-gateway request-channel="requestChannel" uri="http://www.w3schools.com/xml/tempconvert.asmx"/>