Solved

Setting Connection timeout for SOAPConnection

Posted on 2008-09-29
11
5,542 Views
Last Modified: 2010-10-05
I have the below method to call the web service method. Some times when it is not able to connect to the web service as the server may be down, it is just hanging for (atleast more than an hour). I just want to set the time outs, so that if it will not get the response in that specified time I have to cancel the waiting.


protected SOAPMessage sendSOAPRequest(SOAPMessage message, String recipient) throws Exception {

URL urlEndpoint = new URL(recipient)

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();           

        SOAPConnection con = scf.createConnection();

        SOAPMessage replyMessage = con.call(message, urlEndpoint);

return replyMessage 

}

Open in new window

0
Comment
Question by:manyam537
  • 5
  • 5
11 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 22603292
Since, SOAP uses jave.net.URLConnection underneath, you can set the system parameters for connection timeout with
is going to be valid for SOAP as well. You can set the following parameters

-Dsun.net.client.defaultConnectTimeout=

-Dsun.rmi.transport.proxy.connectTimeout=
0
 

Author Comment

by:manyam537
ID: 22604941
Ajay,

If I want to set timeout of 5 minutes  , will this be Ok.

System.setProperty("sun.net.client.defaultConnectTimeout", "5 * 60 * 1000");
  System.setProperty("sun.rmi.transport.proxy.connectTimeout",  "5 * 60 * 1000");

Thanks
TirupatiReddy
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22605090
Sure, it sounds good.
0
 

Author Comment

by:manyam537
ID: 22605162
Ajay,

  I will try that. Before that I implemented in a different way, I don't know why it is not working.

When I run through debug mode, it should come to openConnection method of GGHttpStreamHandler class below when it calls (HttpSOAPConnection) con.call . But it's not coming. Can you please tell me why ?

Thanks
TirupatiReddy
protected SOAPMessage sendSOAPRequest(SOAPMessage message, String recipient) throws Exception {
 

        URL urlEndpoint = null;
 

        GGHttpStreamHandler handler = new GGHttpStreamHandler();
 

        urlEndpoint = new URL((URL) null, recipient, handler);

    
 

        SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();

       

        SOAPConnection con = scf.createConnection();
 

        SOAPMessage replyMessage = con.call(message, urlEndpoint);

return replyMessage;

}
 
 

public class GGHttpStreamHandler extends URLStreamHandler {
 

    protected URLConnection openConnection(URL url) throws IOException {

        HttpURLConnection conn = new GGHttpURLConnection(url);

        conn.setConnectTimeout(10 * 60 * 1000);

        conn.setReadTimeout(10 * 60 * 1000);

        return conn;

    }

}

Open in new window

0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22605484
> SOAPMessage replyMessage = con.call(message, urlEndpoint);

The method call takes, object as second argument. I believe its not making use of the stream handler you are setting.
You can set StreamHandlerFactory (that would create StreamHandler), by calling
  java.net.URL.setURLStreamHandlerFactory(URLStreamHandlerFactory)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:manyam537
ID: 22606170
Ajay,

  Will the system properties that were set will work for Https connection also.
0
 

Author Comment

by:manyam537
ID: 22606365
Ajay,

Will both are same or the first one will not work.

System.setProperty("sun.net.client.defaultConnectTimeout", "5 * 60 * 1000")

OR

System.setProperty("sun.net.client.defaultConnectTimeout", "600000")

0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22611384
> Will the system properties that were set will work for Https connection also.
Yes it should.
0
 

Author Comment

by:manyam537
ID: 22626928
Ajay,

Last question before accepting and closing this question.

How should I create HttpsURLConnection object with URL object.

0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22630776
I have modified your code a little to handle https protocol. But you need implement GGHttpsURLConnection class
public class GGHttpStreamHandler extends URLStreamHandler {

 

    protected URLConnection openConnection(URL url) throws IOException {

        if("http".equalsIgnoreCase(url.getProtocol())) {

          HttpURLConnection conn = new GGHttpURLConnection(url);

          conn.setConnectTimeout(10 * 60 * 1000);

          conn.setReadTimeout(10 * 60 * 1000);

          return conn;

        }

        else if("https".equalsIgnoreCase(url.getProtocol())) {

          HttpsURLConnection conn = new GGHttpsURLConnection(url); //You may have to change this class as well.

          conn.setConnectTimeout(10 * 60 * 1000);

          conn.setReadTimeout(10 * 60 * 1000);

          return conn;

        }

    }

}

Open in new window

0
 
LVL 4

Expert Comment

by:Tuno85
ID: 23361292
I was wondering how do you implement the two timeout methods in GGHttpsURLConnection?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 73
Image decoding from Camera 3 73
more than one jdk and one jre 1 34
oracle 11g 23 51
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now