3

Is there any way to set programmatically the timeout of Spring's WebServiceTemplate? I've seen old articles about setting it via Message Senders in Application Context config file. But in our project, these objects have been created by code, not by injection.

I need to lower the default timeout as sometimes the customer's endpoint takes too long, and queues up other requests, so I need to force it to fail faster.

PS: Using spring-ws-core-2.2.2.RELEASE.jar

gvasquez
  • 1,919
  • 5
  • 27
  • 41
  • Possible duplicate of [How to set timeout in Spring WebServiceTemplate](https://stackoverflow.com/questions/6733744/how-to-set-timeout-in-spring-webservicetemplate) – Dherik Jan 22 '19 at 13:02

1 Answers1

4

Since Spring Webservices 2.2, you can 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));
    }

    (...)
}
Sundararaj Govindasamy
  • 8,180
  • 5
  • 44
  • 77
  • Excellent, I actually had custom marshallers set so adding the sender was straightforward! Now I'll just have to test it – gvasquez Oct 06 '16 at 15:28
  • Is this implementation ThreadSafe or do I have yo switch to a different kind of sender? I need to be sure because my WS client is stored in a private static final variable and used by many threads – gvasquez Oct 06 '16 at 15:31