Prevent JAXRSClientFactory to reuse connections

笑着哭i 提交于 2019-12-13 18:50:30

问题


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

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 ?


回答1:


Dammit,

I finally found how to do it.

here is the code :

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


来源:https://stackoverflow.com/questions/14810825/prevent-jaxrsclientfactory-to-reuse-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!