[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Setting Connection timeout for SOAPConnection

Posted on 2008-09-29
11
Medium Priority
?
6,669 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 

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

Automating Your MSP Business

The road to profitability.
Delivering superior services is key to ensuring customer satisfaction and the consequent long-term relationships that enable MSPs to lock in predictable, recurring revenue. What's the best way to deliver superior service? One word: automation.

Question has a verified solution.

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

For some reason Google Apps has decided to reserve 'www' for your new domain which results in an error when trying to map the web address 'www' to a certain site in your Google Sites. The error is 'this address is already being used' and after some …
A solution for Fortify Path Manipulation.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

656 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