Java Web 服务客户端中的连接详细信息和超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3012787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:34:13  来源:igfitidea点击:

Connection details & timeouts in a java web service client

javatimeoutjax-wsjmxjava-web-start

提问by f1sh

I have to implement a webservice client to a given WSDL file. I used the SDK's 'wsimport' tool to create Java classes from the WSDL as well as a class that wrap's the webservice's only method (enhanceAddress(auth, param, address)) into a simple java method. So far, so good. The webservice is functional and returning results correcty. The code looks like this:

我必须为给定的 WSDL 文件实现一个 web 服务客户端。我使用 SDK 的“wsimport”工具从 WSDL 创建 Java 类,以及一个将 web 服务的唯一方法 ( enhanceAddress(auth, param, address))包装成一个简单的 Java 方法的类。到现在为止还挺好。Web 服务正常运行并正确返回结果。代码如下所示:

try {
  EnhancedAddressList uniservResponse = getWebservicePort().enhanceAddress(m_auth, m_param, uniservAddress);
  //Where the Port^ is the HTTP Soap 1.2 Endpoint
}catch (Throwable e) {
  throw new AddressValidationException("Error during uniserv webservice request.", e);
}

The Problem now: I need to get Information about the connection and any error that might occur in order to populate various JMX values (such as COUNT_READ_TIMEOUT, COUNT_CONNECT_TIMEOUT, ...) Unfortunately, the method does not officially throw any Exceptions, so in order to get details about a ConnectException, i need to use getCause()on the ClientTransportExceptionthat will be thrown.

现在的问题:我需要获取有关连接和可能发生的任何错误的信息以填充各种 JMX 值(例如 COUNT_READ_TIMEOUT、COUNT_CONNECT_TIMEOUT,...)不幸的是,该方法并未正式抛出任何异常,因此为了要获取有关 ConnectException 的详细信息,我需要getCause()ClientTransportException将抛出的异常上使用。

Even worse: I tried to test the read timeout value, but there is none. I changed the service's location in the wsdl file to post the request to a php script that simply waits forever and does not return. Guess what: The web service client does nottime out but waits forever as well (I killed the app after 30+ minutes of waiting). That is not an option for my application as i eventually run out of tcp connections if some of them get 'stuck'.

更糟糕的是:我试图测试读取超时值,但没有。我更改了 wsdl 文件中服务的位置,以将请求发布到一个 php 脚本,该脚本只是永远等待并且不返回。你猜怎么着:Web服务客户端确实没有时间了,但永远等待,以及(我30多分钟的等待后,杀死了应用程序)。这不是我的应用程序的选项,因为如果其中一些“卡住”,我最终会耗尽 tcp 连接。

The enhanceAddress(auth, param, address)method is not implemented but annotated with javax.jws.* Annotations, meaning that i cannot see/change/inspect the code that is actually executed.

enhanceAddress(auth, param, address)方法未实现但使用 javax.jws.* 注释进行注释,这意味着我无法查看/更改/检查实际执行的代码。

Do i have any option but to throw the whole wsimport/javax.jsw-stuff away and implement my own soap client?

除了扔掉整个 wsimport/javax.jsw-stuff 并实现我自己的肥皂客户端之外,我还有什么选择吗?

采纳答案by Helter Scelter

to setup read-timeout and connect timeouts you can configure the binding parameters when you setup your Service and Port instances:

要设置读取超时和连接超时,您可以在设置服务和端口实例时配置绑定参数:



    Service = new Service();

    Port = Service.getPort();

    ((BindingProvider) Port).getRequestContext().put(
            BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
            "http://localhost:8080/service");
    ((BindingProvider) Port).getRequestContext().put(
            BindingProviderProperties.CONNECT_TIMEOUT,
            30);
    ((BindingProvider) Port).getRequestContext().put(
            BindingProviderProperties.REQUEST_TIMEOUT,
            30);


now whenever you execute a service via "Port" you will get response timeouts and/or connection timeouts if the backend is slow to respond. the values follow the timeout values of the Socket Class.

现在,每当您通过“端口”执行服务时,如果后端响应缓慢,您将收到响应超时和/或连接超时。这些值遵循 Socket 类的超时值。

when these timeouts are exceeded you will get timeout exeption or a connection exception and you can put counter-code to keep track of how many you get.

当超过这些超时时,您将获得超时例外或连接异常,您可以放置​​计数器代码来跟踪获得的数量。