Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Jersey client close the connection on exception?

Tags:

jersey

jax-rs

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()?

like image 797
Jason La Avatar asked Sep 04 '14 20:09

Jason La


People also ask

How do I close my jersey client?

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.

What is jersey client used for?

Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities.

What is Jersey client API?

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.

What is Jersey Apache client?

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.


1 Answers

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.

like image 129
lefloh Avatar answered Oct 10 '22 08:10

lefloh