且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何设置JAX-WS Web服务客户端的超时?

更新时间:2023-09-11 22:06:22

我知道这已经过时了,并在其他地方得到了解答,但希望这会让这个结束。我不确定你为什么要动态下载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.