且构网

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

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

更新时间:2023-09-11 22:10:34

我知道这已经过时并在其他地方得到了回答,但希望这能解决这个问题.我不确定您为什么要动态下载 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.