Closing connection in GET request using Jersey Client 2.22.1 -


i using jersey client rest calls java code:

<dependency>     <groupid>org.glassfish.jersey.core</groupid>     <artifactid>jersey-client</artifactid>     <version>2.22.1</version> </dependency>  

in request,

javax.ws.rs.client.invocation.builder builder = clientbuilder.newclient().target(url).request();  builder.get().readentity(string.class); 

the client closed automatically after calling readentity(string.class).

if use,

builder.get(string.class);   

i same output.

is connection closed automatically or need close manually in case?

short answer

consider following code:

client client = clientbuilder.newclient(); string result = client.target(url).request().get(string.class); 

under hood, jersey invokes response#readentity(class<t>) if request has succeeded , connection closed you. so connection doesn't need closed manually in situation.

now consider following code:

client client = clientbuilder.newclient(); response response = client.target(url).request().get(); 

for situation, need invoke response#close() close connection. or invoke response#readentity(class<t>) make jersey close connection you.

long answer

as stated in documentation, if don't read entity, need close response manually invoking response#close().

for more details, have @ jersey's documentation how close connections:

5.7. closing connections

the underlying connections opened each request , closed after response received , entity processed (entity read). see following example:

final webtarget target = ... web target response response = target.path("resource").request().get(); system.out.println("connection still open."); system.out.println("string response: " + response.readentity(string.class)); system.out.println("now connection closed."); 

if don't read entity, need close response manually response.close().

also if entity read inputstream (by response.readentity(inputstream.class)), connection stays open until finish reading inputstream. in case, inputstream or response should closed manually @ end of reading inputstream.

additionally, have @ jerseyinvocation source. important parts quoted below.

in translate(clientresponse, requestscope, class<t>) method you'll see response.readentity(class<t>) invoked.

jerseyinvocation.builder#get(class<t>)

invoke http get method 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>)

invoke arbitrary method current request synchronously.

@override public <t> t method(final string name, final class<t> responsetype)     throws processingexception, webapplicationexception {      // responsetype null check omitted brevity      requestcontext.setmethod(name);     return new jerseyinvocation(this).invoke(responsetype); } 

jerseyinvocation#invoke(class<t>)

synchronously invoke request , receive response of specified type back.

@override public <t> t invoke(final class<t> responsetype)     throws processingexception, webapplicationexception {      // responsetype null check omitted 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 brevity             }         }     }); } 

jerseyinvocation#translate(clientresponse, requestscope, class<t>)

if request suceeded, response entity read instance of 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 brevity         }      } else {         throw converttoexception(new inboundjaxrsresponse(response, scope));     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -