Solved

Setting Connection timeout for SOAPConnection

Posted on 2008-09-29
11
5,435 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

21 Experts available now in Live!

Get 1:1 Help Now