Java 如何使用 CXF 为 JAX-RS 客户端设置超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28319446/
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
How to set timeout to JAX-RS client with CXF
提问by NikosDim
I am working on a Rest Client and I am using CXFwith JAX-RS.
我正在开发一个 Rest Client,我正在将CXF与JAX-RS 一起使用。
The problem that I have is that I cannot find any way to override the default timeout values of the client.
我遇到的问题是我找不到任何方法来覆盖客户端的默认超时值。
A simple client:
一个简单的客户端:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/MyApp");
target = target.path("jsp/Test.jsp");
Response response = target.request().get();
I have read that there are two timeout properties in CXFcalled ReceiveTimeout
and ConnectionTimeout
but I have not managed to find a way to set them in my client.
我已经读到CXF中有两个超时属性被调用ReceiveTimeout
,ConnectionTimeout
但我还没有设法找到在我的客户端中设置它们的方法。
I have tried client.property("ReceiveTimeout", 5000);
but it doesn't work.
我试过了,client.property("ReceiveTimeout", 5000);
但它不起作用。
I have seen examples of using an xmlconfiguration file to configure the client but I prefer not to take that path if it is possible.
我已经看到使用xml配置文件来配置客户端的示例,但如果可能的话,我不希望采用该路径。
Any ideas?
有任何想法吗?
采纳答案by isalgueiro
You can try something like this:
你可以尝试这样的事情:
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(30000);
http.setClient(httpClientPolicy);
回答by Dennis Kieselhorst
You can find the correct properties in org.apache.cxf.jaxrs.client.spec.ClientImpl: "http.connection.timeout" and "http.receive.timeout"
您可以在 org.apache.cxf.jaxrs.client.spec.ClientImpl 中找到正确的属性:“http.connection.timeout”和“http.receive.timeout”
So just use them as property when building the client:
因此,只需在构建客户端时将它们用作属性即可:
ClientBuilder.newClient().property("http.receive.timeout", 1000);
With JAX-RS 2.1 (supported from CXF 3.2) you can use these standard methods in ClientBuilder:
使用 JAX-RS 2.1(受 CXF 3.2 支持),您可以在 ClientBuilder 中使用这些标准方法:
connectTimeout(long timeout, TimeUnit unit);
readTimeout(long timeout, TimeUnit unit);
See also: https://github.com/eclipse-ee4j/jaxrs-api/issues/467
回答by laughing buddha
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
conduit.getClient().setConnectionTimeout(1000 * 3);
conduit.getClient().setReceiveTimeout(1000 * 3);