如何设置JAX-WS Web服务客户端的超时? [英] How do I set the timeout for a JAX-WS webservice client?

查看:1815
本文介绍了如何设置JAX-WS Web服务客户端的超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用JAXWS-RI 2.1基于WSDL为我的Web服务创建了一个接口。我可以与Web服务进行交互没有问题,但是无法指定向Web服务发送请求的超时。如果由于某种原因它没有响应客户似乎永远旋转它的轮子。

I've used JAXWS-RI 2.1 to create an interface for my web service, based on a WSDL. I can interact with the web service no problems, but haven't been able to specify a timeout for sending requests to the web service. If for some reason it does not respond the client just seems to spin it's wheels forever.

狩猎已经显示我应该尝试做这样的事情:

Hunting around has revealed that I should probably be trying to do something like this:

((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.ws.request.timeout", 10000);
((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.ws.connect.timeout", 10000);

我还发现,根据您拥有的JAXWS-RI版本,您可能需要设置这些属性改为:

I also discovered that, depending on which version of JAXWS-RI you have, you may need to set these properties instead:

((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.internal.ws.request.timeout", 10000);
((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.internal.ws.connect.timeout", 10000);

我遇到的问题是,无论上述哪一项是正确的,我都不知道 where 我可以这样做。我所拥有的只是 服务 子类,它实现了自动生成的webservice接口,并且实现了这一点,如果WSDL没有响应,那么设置它已经太晚了属性:

The problem I have is that, regardless of which of the above is correct, I don't know where I can do this. All I've got is a Service subclass that implements the auto-generated interface to the webservice and at the point that this is getting instanciated, if the WSDL is non-responsive then it's already too late to set the properties:

MyWebServiceSoap soap;
MyWebService service = new MyWebService("http://www.google.com");
soap = service.getMyWebServiceSoap();
soap.sendRequestToMyWebService();

有人能指出我正确的方向吗?!

Can anyone point me in the right direction?!

推荐答案

我知道这已经过时了,并在其他地方得到了解答,但希望这会让这个结束。我不确定你为什么要动态下载WSDL但系统属性:

I know this is old and answered elsewhere but hopefully this closes this down. I'm not sure why you would want to download the WSDL dynamically but the system properties:

sun.net.client.defaultConnectTimeout (default: -1 (forever))
sun.net.client.defaultReadTimeout (default: -1 (forever))

应该适用于所有读取并使用JAX-WS使用的HttpURLConnection进行连接。如果从远程位置获取WSDL,这应该可以解决您的问题 - 但本地磁盘上的文件可能更好!

should apply to all reads and connects using HttpURLConnection which JAX-WS uses. This should solve your problem if you are getting the WSDL from a remote location - but a file on your local disk is probably better!

接下来,如果要设置超时对于特定服务,一旦您创建了代理,您需要将其转换为BindingProvider(您已经知道),获取请求上下文并设置您的属性。在线JAX-WS文档是错误的,这些是正确的属性名称(好吧,它们适用于我)。

Next, if you want to set timeouts for specific services, once you've created your proxy you need to cast it to a BindingProvider (which you know already), get the request context and set your properties. The online JAX-WS documentation is wrong, these are the correct property names (well, they work for me).

MyInterface myInterface = new MyInterfaceService().getMyInterfaceSOAP();
Map<String, Object> requestContext = ((BindingProvider)myInterface).getRequestContext();
requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, 3000); // Timeout in millis
requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, 1000); // Timeout in millis
myInterface.callMyRemoteMethodWith(myParameter);

当然,这是一种可怕的做事方式,我会创建一个很好的工厂来生产这些绑定提供程序,可以注入所需的超时。

Of course, this is a horrible way to do things, I would create a nice factory for producing these binding providers that can be injected with the timeouts you want.

这篇关于如何设置JAX-WS Web服务客户端的超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆