1

I'm using a HttpClient to send a SOAP request to an webservice to query some data. For some webservice parameters the execution of the webservice takes longer than 5 minutes and after 5 minutes I get an java.net.SocketException: Connection reset.

I think that the error occures because the connection idles for more than 5 minutes and then a firewall caps the connection.

Is there a way to send a keep-alive package for a http post request or something to keep the connection alive? (I need a client-side solution if possible)

If you google for HttpClient keep-alive you find a lot of topics regarding reusing a connection. In my case I only want to keep the connection alive until I get a response.

Method to execute the SOAP request:

def executeSOAPRequest(String url, String content, String soapAction, Integer timeout) {
    def retVal = new SoapResponse();

    PostMethod post = new PostMethod(url);

    RequestEntity entity = new StringRequestEntity(content,"ISO-8859-1","ISO-8859-1");

    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", soapAction);

    HttpClient httpclient = new HttpClient()
    httpclient.setTimeout(timeout)

    try {
        retVal.httpResponse = httpclient.executeMethod(post);
        retVal.httpResponseBody = post.getResponseBodyAsString();
    } catch(Exception e){
        ... exception handling ...
    } finally {
        ... finally stuff ...
    }

    return retVal;
}

Currently the HttpClient v3.1 is used.

das Keks
  • 3,723
  • 5
  • 35
  • 57
  • http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e412 – tim_yates Jun 11 '14 at 12:36
  • @tim_yates It seems that the keep alive strategy is based on a `HttpResponse` (`public long getKeepAliveDuration(HttpResponse response, HttpContext context`) but what if I haven't got a response yet? – das Keks Jun 12 '14 at 07:52

1 Answers1

-5

5 minutes are an eternity in telecommunications, this days one giga can be transferred in less time, and keeping a connection idle consumes resources not only in the ending machines but in the intermediate nodes such as routers and firewalls.

So IMHO you shouldn't try to keep the connection alive for so long, especially if you don't manage the networks you are using (ie, firewalls can have their own timeouts and kill your connection), you should reduce the time the server needs to respond, or use other asynchronous communication mechanism.

yoyoooyoy
  • 63
  • 7
  • The problem is that I can't change the system design. So my only option is to keep the connection alive. – das Keks Jun 11 '14 at 13:40
  • If a man walked on the moon, probably you'll be able to change the system design :)Your root problem is that your system is using the wrong tool: a bigger knife won't help eating soup. You can try to optimize the respond time in the server (again, 5 minutes is an eternity), but in the end your system will need to use asynchronous calls. – yoyoooyoy Jun 11 '14 at 14:24
  • If it were my own system - yes. But in a big company you don't just go ahead and change things by yourself. A change would cost money and if it works otherwise there is no money for that. Beside that: A lot of systems depend on that design. It would be a huge effort to change them all. And if someone is willing to change them, then there are a lot of other things which have to be changed too and have higher priority. – das Keks Jun 11 '14 at 14:58