JAVA: Interrupt Thread after so much time has passed
I am currently working on a project that calls me to download a WSDL file from the internet.
Everything outside of my problem works great however...
I use the following code:
private Definition getDefinition(String url) throws WSDLException {
// Read the url and return an instance to a WSDL Definition
return WSDLFactory.newInstance().newWSDLReader().readWSDL(url);
}
This WSDLFactory is a Thread(to my knowledge). The issue I have is that in the list of URL's I provide, a few of them point to WSDL's that no longer exist. This in turn causes my program to wait an Extraordinarily long time before it realizes that it should throw a FileNotFoundException. This 开发者_开发技巧is caused because the location the WSDL should be at takes forever to return an HTTP_RESPONSE code.
Is there a way I can wrap this WSDLFactory into something that attempts to get the WSDL but only gives it 5-10 seconds to do so? After which, it says "You have had too much time to do this. We are stepping over you"?
Look into the Executor and Futures frameworks.
Set the underlying socket's timeout if possible socket.setSoTimeout
or via a higher-level config option in the library.
Otheriwise (and if you've got Google's guava on the classpath) try:
http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/util/concurrent/TimeLimiter.html
You can try setting the http timeout sun.net.client.defaultConnectTimeout
. Of course this depends on the implementation of WSDLReader
Check here:
networking properties
Use a secondary thread, that calls the interrupt() method on the first thread after 5 seconds. Handle the InterruptedException in the first thread.
精彩评论