I've read the Jersey documentation, and it says Jersey automatically closes a connection after an entity is read (e.g. response.readEntity(SomeObject.class))
But when an exception is thrown, either a bad request or a socket timeout, does Jersey automatically close the connection, or should I have a finally clause that calls client.close()?
For this situation, you need to invoke Response#close() to close the connection. Or invoke Response#readEntity(Class<T>) to make Jersey close the connection for you.
Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities.
Jersey ClientBuilder. JAX-RS Client API is a designed to allow fluent programming model. To create jersey client follow these steps – Use ClientBuilder. newClient() static method.
Jersey is a REST-client, featuring full JAX-RS implementation, neat fluent API and a powerfull filter stack. Apache Http Client is a HTTP-client, perfect in managing low-level details like timeouts, complex proxy routes and connection polling. They act on a different levels of your protocol stack.
No. Neither does Jersey call client.close()
in case of an exception nor does the JerseyClient implement AutoCloseable.
You can easily test this. A client throws a IllegalStateException if you invoke a method after closing:
Client client = ClientBuilder.newClient();
client.close();
client.target("http://stackoverflow.com").request().get(); // IllegalStateException
But you can invoke a method after catching an exception:
Client client = ClientBuilder.newClient();
try {
client.target("http://foo.bar").request().get(); // java.net.ConnectException: Operation timed out
} catch (Exception ex) {
client.target("http://stackoverflow.com").request().get(); // works
}
So closing is your job.
Update: JAX-RS 2.1 will use AutoClosables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With