How to set timeout in Spring WebServiceTemplate

前端 未结 9 1031
一整个雨季
一整个雨季 2020-12-05 06:45

I am using org.springframework.ws.client.core.WebServiceTemplate for making Web Service calls. How can i configure timeout for the call.

相关标签:
9条回答
  • 2020-12-05 07:22

    If you are using Spring Webservices 2.1.0 version, You can set timeout using HttpComponentsMessageSender.

    CommonsHttpMessageSender are deprecated and not recommended by Spring anymore.

    The way I have it implemented, I define my WebServiceTemplate to use HttpComponentsMessageSender.

    Values are in Milliseconds

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <property name="defaultUri" value="${endpoint.url}" />
        <property name="marshaller" ref="marshaller" />
        <property name="unmarshaller" ref="unmarshaller" />
        <property name="messageSender">
            <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
                <property name="connectionTimeout" value="1200000" />
                <property name="readTimeout" value="1200000" />
            </bean>
        </property>
    </bean>
    

    Just Make sure you have in your pom file, you added the following

    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.1</version>
            <scope>compile</scope>
        </dependency>
    
    0 讨论(0)
  • 2020-12-05 07:23

    Since Spring Webservices 2.2, you can also use Spring's ClientHttpRequestMessageSender:

    @Service
    public class CustomWebServiceImpl extends WebServiceGatewaySupport implements CustomWebService {
        private static final int CONNECTION_TIMEOUT = (10 * 1000);
        private static final int READ_TIMEOUT = (10 * 1000);
    
        public CustomWebServiceImpl() {
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(CONNECTION_TIMEOUT);
            requestFactory.setReadTimeout(READ_TIMEOUT);
            setMessageSender(new ClientHttpRequestMessageSender(requestFactory));
        }
    
        (...)
    }
    

    (no dependency to Apache HTTP Components required)

    0 讨论(0)
  • 2020-12-05 07:29

    This article will probably sort you out: http://onebyteatatime.wordpress.com/2009/03/19/how-to-set-socket-timeout-using-spring-webservicetemplate/

    The way I have it implemented, I define my WebServiceTemplate to use CommonsHttpMessageSender:

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
            <constructor-arg ref="messageFactory" />
            <property name="messageSender">
                <bean
                    class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
                </bean>
            </property>
    </bean>
    

    Then, in code, I get the messageSender and set the timeout on it. You could equally do this in your xml.

    CommonsHttpMessageSender messageSender = (CommonsHttpMessageSender)webServiceTemplate.getMessageSenders()[0];
    messageSender.getHttpClient().getParams().setSoTimeout(timeoutMillis);
    
    0 讨论(0)
提交回复
热议问题