JAVA:经过这么长时间后中断线程
我目前正在开发一个项目,要求我从 Internet 下载 WSDL 文件。
然而,我的问题之外的一切都很好......
我使用以下代码:
private Definition getDefinition(String url) throws WSDLException {
// Read the url and return an instance to a WSDL Definition
return WSDLFactory.newInstance().newWSDLReader().readWSDL(url);
}
这个 WSDLFactory 是一个线程(据我所知)。我遇到的问题是,在我提供的 URL 列表中,其中一些指向不再存在的 WSDL。这反过来又导致我的程序等待非常长的时间才意识到它应该抛出 FileNotFoundException。这是因为 WSDL 应该所在的位置需要很长时间才能返回 HTTP_RESPONSE 代码。
有没有办法可以将这个 WSDLFactory 包装成尝试获取 WSDL 但只给它 5-10 秒的时间?之后,它说“你已经有太多的时间来做这件事。我们正在超越你”?
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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 Executor 和 Futures 框架。
Look into the Executor and Futures frameworks.
如果可能,请设置底层套接字的超时
socket.setSoTimeout
或通过库中的更高级别配置选项。否则(如果你的类路径上有 Google 的 guava)请尝试:
http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/util/concurrent/TimeLimiter.html
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
您可以尝试设置http超时
sun.net.client.defaultConnectTimeout
。当然这取决于WSDLReader的实现检查这里:
网络属性
You can try setting the http timeout
sun.net.client.defaultConnectTimeout
. Of course this depends on the implementation of WSDLReaderCheck here:
networking properties
使用辅助线程,它会在 5 秒后调用第一个线程的 Interrupt() 方法。在第一个线程中处理 InterruptedException。
Use a secondary thread, that calls the interrupt() method on the first thread after 5 seconds. Handle the InterruptedException in the first thread.