Closing a connection in a GET request using Jersey Client 2.22.1

I am using the Jersey client for REST calls from Java code:

<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.22.1</version> </dependency> 

In my GET request

 javax.ws.rs.client.Invocation.Builder builder = ClientBuilder.newClient().target(url).request(); builder.get().readEntity(String.class); 

the client will be closed automatically after calling readEntity(String.class) .

If i use

 builder.get(String.class); 

I get the same result.

Is the connection closed automatically or do I need to close it manually in this case?

+5
source share
1 answer

Short answer

Consider the following code:

 Client client = ClientBuilder.newClient(); String result = client.target(url).request().get(String.class); 

Under the hood, Jersey calls Response#readEntity(Class<T>) if the request succeeds and the connection is closed to you. Therefore, in this situation, the connection does not need to be closed manually.

Now consider the following code:

 Client client = ClientBuilder.newClient(); Response response = client.target(url).request().get(); 

In this situation, you need to call Response#close() to close the connection. Or call Response#readEntity(Class<T>) to force Jersey to close the connection for you.

Long answer

As stated in the documentation , if you do not read the object , then you need to close the response manually by calling Response#close() .

For more information, see the documentation on how to close connections:

5.7. Closing Connections

Suitable connections are opened for each request and closed after receiving a response and processing the object (read object). See the following example:

 final WebTarget target = ... some web target Response response = target.path("resource").request().get(); System.out.println("Connection is still open."); System.out.println("string response: " + response.readEntity(String.class)); System.out.println("Now the connection is closed."); 

If you are not reading the object, you need to close the response manually response.close() .

Also, if an object is read in an InputStream (via response.readEntity(InputStream.class) ), the connection remains open until you finish reading with InputStream . In this case, the InputStream or Response should be manually closed at the end of reading from the InputStream .

Also, see the JerseyInvocation source . The following are the most important parts.

In the translate(ClientResponse, RequestScope, Class<T>) method translate(ClientResponse, RequestScope, Class<T>) you will see that response.readEntity(Class<T>) called.

JerseyInvocation.Builder#get(Class<T>)

Call the HTTP GET method for the current request synchronously.

 @Override public <T> T get(final Class<T> responseType) throws ProcessingException, WebApplicationException { return method("GET", responseType); } 

JerseyInvocation.Builder#method(String, Class<T>)

Call an arbitrary method for the current request synchronously.

 @Override public <T> T method(final String name, final Class<T> responseType) throws ProcessingException, WebApplicationException { // responseType null check omitted for brevity requestContext.setMethod(name); return new JerseyInvocation(this).invoke(responseType); } 

JerseyInvocation#invoke(Class<T>)

Synchronously call a request and receive a response of the specified type back.

 @Override public <T> T invoke(final Class<T> responseType) throws ProcessingException, WebApplicationException { // responseType null check omitted for brevity final ClientRuntime runtime = request().getClientRuntime(); final RequestScope requestScope = runtime.getRequestScope(); return requestScope.runInScope(new Producer<T>() { @Override public T call() throws ProcessingException { try { return translate(runtime.invoke(requestForCall(requestContext)), requestScope, responseType); } catch (final ProcessingException ex) { // Exception handling omitted for brevity } } }); } 

JerseyInvocation#translate(ClientResponse, RequestScope, Class<T>)

If the request Response#readEntity(Class<T>) , the response object is read as an instance of the specified Java type using Response#readEntity(Class<T>) :

 private <T> T translate(final ClientResponse response, final RequestScope scope, final Class<T> responseType) throws ProcessingException { if (responseType == Response.class) { return responseType.cast(new InboundJaxrsResponse(response, scope)); } if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { try { return response.readEntity(responseType); } catch (final ProcessingException ex) { // Exception handling omitted for brevity } } else { throw convertToException(new InboundJaxrsResponse(response, scope)); } } 
+12
source

Source: https://habr.com/ru/post/1242517/


All Articles