gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:37:36 25 4
gpt4 key购买 nike

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

四处寻找发现我可能应该尝试做这样的事情:

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

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

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

我遇到的问题是,无论以上哪一项是正确的,我都不知道在哪里我可以做到这一点。我只有一个 Service实现 Web 服务的自动生成接口(interface)的子类,并且在实例化时,如果 WSDL 没有响应,那么设置属性已经太晚了:

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

谁能指出我正确的方向?!

最佳答案

我知道这已经过时并且在其他地方得到了回答,但希望这可以解决这个问题。我不确定您为什么要动态下载 WSDL 但系统属性:

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

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

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

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);

当然,这是一种可怕的做事方式,我会创建一个很好的工厂来生产这些绑定(bind)提供程序,这些提供程序可以注入(inject)你想要的超时时间。

关于java - 如何为 JAX-WS Web 服务客户端设置超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2148915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com