使用 Spring 异步调用 SOAP 服务



我必须异步调用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");
                        }
                    });

我可以知道如何更改它以异步调用服务吗?还是我需要在春天使用其他东西来实现这一点?

不确定为什么在问题中使用spring-integration标签,但是如果我们在这里,请务必查看将Future<>作为返回类型的@MessagingGateway:https://docs.spring.io/spring-integration/docs/5.0.1.RELEASE/reference/html/messaging-endpoints-chapter.html#async-gateway

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

示例如下: https://github.com/spring-projects/spring-integration-samples

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

<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"/>

相关内容

  • 没有找到相关文章

最新更新