防止JAXRSClientFactory重用连接 [英] Prevent JAXRSClientFactory to reuse connections

查看:96
本文介绍了防止JAXRSClientFactory重用连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JAXRSClientFactory 实例化REST客户端以进行集成测试。

I am using JAXRSClientFactory to instantiate REST clients for integration test purpose.

在两个测试之间,我重新启动Jetty服务器,并实例化新的REST客户端,以相同的URL。但是,似乎CXF正在使用某种连接池,或者是在后台进行连接保持活动的系统,因为在服务器重新启动后,第一次测试时出现连接错误。

Between two tests, I restart my Jetty server, and instantiate new REST clients, to the same URL. However, it seems that CXF is using some kind of connection pooling, or connection keep-alive system under the hood, since I get connection errors for the first test following the restart of the server.

我在文档中没有发现任何说明使用连接池的信息:是这样吗?如果是这样,我该如何防止它,或者在测试结束时刷新给定客户端的连接?

I have not found anything stating the use of connection pooling in the documentation though : It is the case ? If so, how do I prevent it, or flush connections for a given client at the end of a test ?

推荐答案

Dammit,

我终于找到了解决方法。

I finally found how to do it.

这是代码:

import org.apache.cxf.jaxrs.client.ClientConfiguration;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.transport.http.HTTPConduit;
import static org.apache.cxf.transports.http.configuration.ConnectionType.CLOSE;

...

MyService proxy = JAXRSClientFactory.create("url", MyService.class);

// Disable keep-alive connection
ClientConfiguration config = WebClient.getConfig(proxy);
HTTPConduit conduit = config.getHttpConduit();
conduit.getClient().setConnection(CLOSE);

这篇关于防止JAXRSClientFactory重用连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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