I am using org.springframework.ws.client.core.WebServiceTemplate
for making Web Service calls. How can i configure timeout for the call.
One way we can implement a request timeout on database calls is to take advantage of Spring's @Transactional annotation. It has a timeout property that we can set. The default value for this property is -1, which is equivalent to not having any timeout at all.
The WebServiceTemplate is the core class for client-side Web service access in Spring-WS. It contains methods for sending Source objects, and receiving response messages as either Source or Result .
Your configuration of WebServiceTemplate , SaajSoapMessageFactory and Jaxb2Marshaller singletons is perfectly fine, and entirely thread-safe.
marshalSendAndReceive(String uri, Object requestPayload) Sends a web service message that contains the given payload, marshalled by the configured Marshaller . Object.
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>
Same as Sathish answer, but programmatically (Spring 4+):
@Component public class MyWebServiceGatewaySupport extends WebServiceGatewaySupport { @Value("${my.ws.readtimeout}") private String readTimeout; @Value("${my.ws.connectiontimeout}") private String connectionTimeout; Object marshalSendAndReceive(String endpoint, Object requestPayload) { WebServiceTemplate wsTemplate = this.getWebServiceTemplate(); WebServiceMessageSender[] senders = wsTemplate.getMessageSenders(); for (WebServiceMessageSender sender: senders) { try { int readTimeoutMsec = Integer.parseInt(readTimeout); int connTimeoutMsec = Integer.parseInt(connectionTimeout); HttpComponentsMessageSender httpSender = (HttpComponentsMessageSender) sender; httpSender.setReadTimeout(readTimeoutMsec); httpSender.setConnectionTimeout(connTimeoutMsec); } catch (ClassCastException|NumberFormatException cex) { logger.warn("Cannot set WS timeout: " + cex.getMessage()); } } return wsTemplate.marshalSendAndReceive(endpoint, requestPayload); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With