How to set timeout in Spring WebServiceTemplate

2019-01-14 10:38发布

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

8条回答
爷、活的狠高调
3楼-- · 2019-01-14 11:00

The below code worked for me.

@Bean
public YourlassImpl YourClassImpl(Jaxb2Marshaller marshaller, HttpComponentsMessageSender  httpComponentsMessageSender) {
    YourClassImpl client = new YourClassImpl();
    client.setDefaultUri(PiiProperties.SOAP_ACTION.getValue());
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    client.setMessageSender(httpComponentsMessageSender);
    return client;
}

@Bean
public  HttpComponentsMessageSender  httpComponentsMessageSender() {       
    HttpComponentsMessageSender sender =  new HttpComponentsMessageSender();
    sender.setReadTimeout(1000);
    sender.setConnectionTimeout(1000);
    return sender;
}
查看更多
SAY GOODBYE
4楼-- · 2019-01-14 11:04

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>
查看更多
神经病院院长
5楼-- · 2019-01-14 11:07

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)

查看更多
看我几分像从前
6楼-- · 2019-01-14 11:09

That's how I did:

@Configuration
public class MunisServiceConfig {

    @Value("${service.uri}")
    private String soapUri;

    @Bean
    Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath(CheckStatePayment.class.getPackage().getName());

        return jaxb2Marshaller;
    }

    @Bean
    public WebServiceTemplate munisService() {
        WebServiceTemplate template = new WebServiceTemplate();

        template.setMarshaller(jaxb2Marshaller());
        template.setUnmarshaller(jaxb2Marshaller());

        template.setDefaultUri(soapUri);

        HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
        httpComponentsMessageSender.setReadTimeout(3000);
        httpComponentsMessageSender.setConnectionTimeout(5000);
        template.setMessageSender(httpComponentsMessageSender);

        return template;
    }

}
查看更多
一纸荒年 Trace。
7楼-- · 2019-01-14 11:15

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);
查看更多
登录 后发表回答