Announcement Announcement Module
Collapse
No announcement yet.
WebServiceTemplate time out Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • WebServiceTemplate time out

    Hello,
    I am trying to set connection time out using CommonsHttpMessageSender and org.apache.commons.httpclient.HttpClient. I am setting the connectionManagerTimeout to 10 secondes but it doesn't work. Can anyone help?
    I would like to make sure the connection will time out after 10 seconds if the target server is down.

    I am able to see the following in the logs:
    Code:
    2010-07-27 12:15:04,254 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.connection-manager.timeout = 10000
    2010-07-27 12:15:04,254 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.socket.timeout = 30000


    Code:
    applkication-context.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:ws="http://www.springframework.org/schema/web-services"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd
    		http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-1.5.xsd">
    
    	<bean id="requestSender" class="com.destiny.soap.RequestSender"
    		p:messageFactory-ref="messageFactory" p:messageSender-ref="httpSender"
    		p:webServiceTemplate-ref="webServiceTemplate" />
    
    	<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />
    
    	<bean id="faultMessageResolver"
    		class="org.springframework.ws.soap.client.core.SoapFaultMessageResolver" />
    
    	<bean id="httpParams" class="org.apache.commons.httpclient.params.HttpClientParams"
    		p:connectionManagerTimeout="10000" p:soTimeout="30000" />
    
    	<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient"
    		p:params-ref="httpParams" />
    
    	<bean id="httpSender"
    		class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
    		<constructor-arg>
    			<ref bean="httpClient" />
    		</constructor-arg>
    	</bean>
    
    	<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"
    		p:faultMessageResolver-ref="faultMessageResolver"
    		p:checkConnectionForError="true" p:checkConnectionForFault="true" />
    
    </beans>
    
    
    RequestSender class
    
    package com.destiny.soap;
    
    import java.io.IOException;
    
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.TransformerException;
    
    import org.springframework.ws.WebServiceMessage;
    import org.springframework.ws.client.core.WebServiceMessageCallback;
    import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
    import org.springframework.ws.soap.SoapMessage;
    
    public class RequestSender
    		extends WebServiceGatewaySupport {
    
    	public boolean send(String uri, Source source, final String soapActionUri,
    			Result result) {
    		return getWebServiceTemplate().sendSourceAndReceiveToResult(uri,
    			source, new WebServiceMessageCallback() {
    
    				public void doWithMessage(WebServiceMessage message)
    						throws IOException, TransformerException {
    					((SoapMessage) message).setSoapAction(soapActionUri);
    				}
    			}, result);
    	}
    }
    
    RequestSenderTest class
    
    
    package com.destiny.soap;
    
    import static org.junit.Assert.*;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.StringReader;
    
    import javax.xml.transform.Source;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    
    import org.apache.commons.io.IOUtils;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @ContextConfiguration(locations = {"/application-context.xml"})
    @RunWith(SpringJUnit4ClassRunner.class)
    public class RequestSenderTest {
    
    	@Test
    	public void testSend() {
    		String path = this.getClass().getResource(".").getPath();
    		path = path.replace("%20", " ");
    		File newPath = new File(path).getParentFile().getParentFile()
    			.getParentFile();
    		File file = new File(newPath, "test.xml");
    
    		byte[] bytes = null;
    		FileInputStream is = null;
    
    		try {
    			is = new FileInputStream(file);
    			bytes = org.apache.commons.codec.binary.Base64.encodeBase64(IOUtils
    				.toByteArray(is));
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (is != null) {
    				try {
    					is.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    
    		if (bytes != null && bytes.length != 0) {
    			RequestSender requestSender = new RequestSender();
    			String nameSpace = "http://test.co.uk/";
    			String message = "<ImportDestinyForm xmlns=\"${NAME_SPACE}\"><FileIn>${FileIn}</FileIn><FileInName>${FileInName}</FileInName></ImportDestinyForm>";
    			message = message.replace("${NAME_SPACE}", nameSpace);
    			message = message.replace("${FileIn}", new String(bytes));
    			message = message.replace("${FileInName}", file.getName());
    			Source source = new StreamSource(new StringReader(message));
    			StreamResult result = new StreamResult(new ByteArrayOutputStream());
    			String uri = "http://localhost:8080/";
    			String soapActionUri = "";
    			boolean response = false;
    
    			response = requestSender.send(uri, source,
    				soapActionUri, result);
    
    			assertTrue(response == true);
    			String receivedMessage = ((ByteArrayOutputStream) result
    				.getOutputStream()).toString();
    			System.out.println(receivedMessage);
    		}
    	}
    }
    Last edited by armandlek16; Aug 6th, 2010, 05:31 AM.
Working...
X