GeeCON Prague 2014*
The moose likes Web Services and the fly likes SoapConnection timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "SoapConnection timeout" Watch "SoapConnection timeout" New topic
Author

SoapConnection timeout

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

I wrote a SOAP client and I am using the SoapConnection object to make a call to the web service. I need this client to timeout in 15 minutes if I dont get a SOAP response. There is nothing in the API that will allow me to do this. So here are the things I have done so far to work around this problem

1. I tried to write a URLStreamHandler and I constructed a URL object with the handler as a parameter hoping that I can set the timeout in the URLConnection. That didnt work because only the toString() method is invoked on the URL object by the SOAP API.

2. I downcasted the SoapConnection object to a SoapConnectionImpl object. The SoapConnectionImpl object is provided by the axis API. I used the timeout method on this object and that works fine.

Even though option 2 works I feel awkward to downcast the implementation class and then use a timeout method on that class. It just does not feel right. Does anyone know a method that will handle timeouts more gracefully ? I am against starting new threads and then interrupting them if the thread is still alive after X minutes. That is not graceful either and that adds more code complexity.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Amir Alagic
Ranch Hand

Joined: Mar 21, 2006
Posts: 65
Hi

I also use SOAPConnectionImpl from AXIS and couldn't find other way to solve this problem... Did you find solution ?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Nope. Still using the one I mentioned. If I find something I ll post it here
Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 22
Hi Deepak,
I have implemented a basic soap client (javax.xml.soap.*).




The input message is JBossEsb message, which I convert to soap message and then send it using SOAPConnection, which works fine.

The problem with SOAPConnection class is that it does not provide setTimeout(..) method. I read some where that there is another class SOAPConnectionImpl (by axis), which extends SOAPConnection and has this setTimeout method.

I have tried to convert my existing implementation to SOAPConnectionImpl but i always get a classcast exceptions.

Could somebody help me in suggesting a solution to this problem? My main concern is to include timeout feature that is my WS consumer/client should timeout after a certain period if the server does not reply.

Any other soap message sending implementation with timeout feature are also good.

I have included


maven dependencies for axis.

Awaiting replies.

Thanks.
bhupesh ravish
Greenhorn

Joined: Sep 12, 2006
Posts: 11
Hi guys
I also faced the same situation, But I was using the stub created while compiling the WSDL file to call the web service.
In that case we can set the time out as under :

stub._getServiceClient().getOptions.setTimeOutInMilliSeconds(100000);

stub is the instance of my stub class.

You can give this you respective time in this. ;)
Tom Davis Sr
Greenhorn

Joined: May 24, 2009
Posts: 16
I am using SAAJ for sending a SOAP Message. I need to set a timeout on the client if there is no reply within certain time.



After going through some references, it seems downcasting to SoapConnectionImpl works? How would this be accomplised . Got the AXIS jars and cast - but gives a ClassCastException? Also is this safe to do - any known issues?
kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Hi,

I am using Axis 1.4 and JDK 1.4. I am also facing the same issue where I have to set the timeout. Please find code snippet below.


MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();

//Custom
SOAPHeaderElement authElement;
SOAPElement authTokenElement;


// Create and populate the body

Name ns = envelope.createName("Authentication", "","urn:test.com");

Name ns1 = envelope.createName("AuthenticationToken", "","urn:test.com");

//Name ns = new
SOAPHeader soaph = envelope.getHeader();
authElement = soaph.addHeaderElement(ns);
authTokenElement = authElement.addChildElement("AuthenticationToken");
authTokenElement.addTextNode( authenticationToken );

SOAPBody body = envelope.getBody();




SOAPElement bodyElement = body.addChildElement(
envelope.createName("GetValue", "", "urn:WS.service.test.com"));
SOAPElement bodyElementMat = bodyElement.addChildElement("First"); // bodyElement.add;
SOAPElement testing = bodyElementMat.addChildElement("second");
testing.addTextNode("TestData");
envelope = soapPart.getEnvelope();

MimeHeaders headers = message.getMimeHeaders();
headers.addHeader("SOAPAction", "urn:WS.service.test.com/GetValue");


// Save the message
message.saveChanges();


SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();

--In this thread it is mentioned that the soapconnection was downcasted to soapconnectionImpl, to achieve the timeout

URL url = new URL("http://localhost:8001/ws/services/TestService");


SOAPMessage msg = message;
msg.writeTo(System.out);

SOAPMessage reply = connection.call(msg,url );
//reply.writeTo(System.out); // this is the acutal output of the webservice.

//Getting the values from response
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
if (reply !=null){
Source src = reply.getSOAPPart().getContent();

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();

transformer.transform(src, result);
}


System.out.println("finally"+writer.toString());
}

In this thread it is mentioned that the soapconnection was downcasted to soapconnectionImpl, to achieve the timeout. Could you please provide your suggestions. Any suggestion is highly appreciated.

Thanks in Advance.


Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Wow. So the thread is still useful

Anywho, the SoapConnection object was indeed downcasted to a SoapConnectionImpl when I used AXIS. But it appears that this solution does not work for the others in the thread. This could be because of the following reasons

1. The SOAP provider does not have the SoapConnectionImpl set as the default implementation
2. Since this is not a method to be honored in an interface it is no longer available (if you can downcast it successfully that is)

An alternate solution suggested was to tinker with the HTTP timeout that the SOAP messages will be transported over. There was another method where you do not need a work around at all. The interface had a timeout method. I cannot remember where I found this client code. I will update this thread if I find the solution. I will experiment with it tonight
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

ok, I gave the code a try with JDK 1.6

The implementation class is now com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection. The point I am making is, you cannot depend on the implementation class specific details to set the timeout, like I mentioned in my original post a few months back. To find out which implementation you have, simple do an 'inspect element' on the SOAPConnection on your favorite IDE. That will / should tell you the type of the underlying class. If it supports timeout thats great.

I ll try to hunt down the other method which can handle timeouts gracefully. You should be using that instead

[EDIT]

An alternate solution:

Axis supposedly supports timeouts by passing these system properties. Source



kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Thanks Deepak. The webservice call is done within another application that uses only JDK 1.4. I cannot use higher versions. So, this is another restriction.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Inability to use jdk 1.6 should not matter. Let us know if the alternate solution works
kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Deepak,

I did try the option. Please find the below
For the version that I am using the implementation class is SOAPConnectionImpl, but this does not have any method to set the timeout.

Thanks.


Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

hmmm well you cannot depend on the method offered by an implementation class.

How about starting a new thread and then stopping it after X seconds if there is no response ? It is extremely ugly but will work. I still cannot remember the alternate call that allowed one to set the timeout.
Kasi Viswan
Ranch Hand

Joined: Sep 27, 2008
Posts: 42
I have similar requirement to sent timeout at client, couldn't do.

Tried Casting SOAPConnection to SOAPConnectionImpl of Axis 1.5, getting classcastexception.

Anyone figured out, how do set timeout?
alex ho kwong yiu
Greenhorn

Joined: Jun 06, 2006
Posts: 3
Kasi Viswan wrote:I have similar requirement to sent timeout at client, couldn't do.

Tried Casting SOAPConnection to SOAPConnectionImpl of Axis 1.5, getting classcastexception.

Anyone figured out, how do set timeout?


interesting, i try casting to SOAPConnectionImpl and then settimeout , it works fine.

i am using axis 1.4 and jdk1.4 ,
Josef Schlessinger
Greenhorn

Joined: Dec 05, 2013
Posts: 1

It's an old thread, but I just finished coding this and wanted to let users of SAAJ and SOAPConnection, not Axis, not Axis2, just what is built in to Java, know they can indeed use URLStreamConnection to set timeouts.

 
GeeCON Prague 2014
 
subject: SoapConnection timeout