问题
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