Solved

Setting Connection timeout for SOAPConnection

Posted on 2008-09-29
11
5,637 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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
groupSum6 challenge 6 86
Java Server Faces parameter pass? 6 48
eclipse argument 14 60
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 24
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

786 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