views:

3674

answers:

3

I'm working with a bit of a legacy component in which we interact with a SOAP web service (a technology which I absolutely, positively abhor) using some client code built using the JAXRPC-RI (reference implementation) library.

I'm interested in being able to set a timeout with the stubs so that in case the web services server does not reply within X seconds, the application isn't setting there forever waiting for a response.

I'm using to working with clients/stubs generated by Apache Axis, in which you can simply use org.apache.axis.client.Stub.setTimeout() to set a timeout.

For the life of me I can't figure out how to set a timeout when using Stubs created with JAXRPC-RI:

  • The port class I am instantiating extends com.sun.xml.rpc.client.StubBase and implements javax.xml.rpc.Stub and com.sun.xml.rpc.spi.runtime.StubBase.
  • The JavaDocs for none of these classes mention any sort of timeout or method to do this.
  • Trying code like stub._setProperty("axis.connection.timeout", 1000); results in an exception at runtime: javax.xml.rpc.JAXRPCException: Stub does not recognize property: axis.connection.timeout

Does anyone have any ideas on how to set/enforce a timeout when using a JAXRPC-RI client? Is it even possible?

Update: Started a bounty on this question, as I could really use a solution here...

A: 

I'm just going to try to reap the bounty, so don't shoot me if I am completely on the wrong track :) If your application is "setting there forever waiting for a response" than you could do the request in a separate thread and after spawning your requestThread you could just say requestThread.join(max_time_to_wait); the next call would check if the requestThread is still alive and if it this then try to kill it.

You application will move on after a time-out and it's not the most inelegant solution...

Peter Perháč
This is an option, but I'd really like to do it within the confines of the web services client code / API itself, if possible. Thank you.
matt b
+1  A: 

You may have luck setting properties such as sun.net.client.defaultConnectTimeout or sun.net.client.defaultReadTimeout, though that would then introduce a system-wide timeout.

In code the properties values are set using Strings:

System.setProperty("sun.net.client.defaultConnectTimeout", "1000");
System.setProperty("sun.net.client.defaultReadTimeout", "1000");

For a quick test, it might be easier to set the environment variable JAVA_OPTS or use the command line:

java -Dsun.net.client.defaultConnectTimeout="1000" ...
Arjan
Any idea if this affects not just outgoing connections but incoming ones as well?
matt b
I guess the first can only apply to outgoing requests, unless any handshakes would be taken into account as well. And given the names "net.client" I assume both properties affect outgoing only, which for the second property is supported by its definition "[..] specifies the timeout (in milliseconds) when reading from input stream when a connection is established to a resource." Indeed, this makes one wonder if there are similar properties for sockets that have accepted an incoming connection, but I cannot quickly find those.
Arjan
And, "These properties specify the default connect and read timeout (resp.) for the protocol handler used by java.net.URLConnection." So, outgoing only for sure.http://java.sun.com/javase/6/docs/technotes/guides/net/properties.html
Arjan