?
Solved

Setting Connection timeout for SOAPConnection

Posted on 2008-09-29
11
Medium Priority
?
6,372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 1500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

Question has a verified solution.

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

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 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