如何使用CXF为JAX-RS客户端设置超时 [英] How to set timeout to JAX-RS client with CXF

查看:90
本文介绍了如何使用CXF为JAX-RS客户端设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rest Client,并且正在将 CXF JAX-RS 一起使用。

I am working on a Rest Client and I am using CXF with JAX-RS.

我遇到的问题是我找不到任何方法来覆盖客户端的默认超时值。

The problem that I have is that I cannot find any way to override the default timeout values of the client.

一个简单的客户

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/MyApp");
target = target.path("jsp/Test.jsp");
Response response = target.request().get();

我读到 CXF 中有两个超时属性,称为 ReceiveTimeout ConnectionTimeout ,但是我还没有找到在客户端中设置它们的方法。

I have read that there are two timeout properties in CXF called ReceiveTimeout and ConnectionTimeout but I have not managed to find a way to set them in my client.

我尝试了 client.property( ReceiveTimeout,5000); ,但是它不起作用。

I have tried client.property("ReceiveTimeout", 5000); but it doesn't work.

我已经看到了使用 xml 配置文件来配置客户端的示例,但是如果可能的话,我不希望采用该路径。

I have seen examples of using an xml configuration file to configure the client but I prefer not to take that path if it is possible.

有什么想法吗?

推荐答案

您可以尝试执行以下操作:

You can try something like this:

HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(30000);
http.setClient(httpClientPolicy);

请参见 http://cxf.apache.org/javadoc/latest/org/apache/cxf/transports/http/configuration/HTTPClientPolicy.html

这篇关于如何使用CXF为JAX-RS客户端设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆