Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jersey-dev] client lifecycle question

Hello Jersey developers,

I have a question regarding when a client may be closed:

Client client = ClientBuilder.newBuilder()...<setup additional attributes>...build();
...
client.target(baseUri)
   .path(OSVS_CGC).path(sessionId.toString()).path(UNLOCK)
   .request()
   .async()
   .put(Entity.entity(req, MediaType.APPLICATION_JSON), new InvocationCallback<Response>() {

       @Override
       public void completed(Response response) {
           client.close();                   <---- B
           response.getStatus();
           SomeClass sc = response.readEntity(SomeClass.class);
           // process response
           ...
           client.close();                   <---- C
       }

       @Override
       public void failed(Throwable t) {
           client.close();                   <---- B
           // handle failure
           ...
           client.close();                   <---- C
       }
});
client.close();                              <---- A

Please, please, please do not rake me over the coals for closing my client right after using it once. That is not the point of this question. (I've already been through that ringer with some jackass over on stackoverflow - I finally had to delete my question over there because he hijacked it for his own selfish prideful purposes.)

This example is simplistic on purpose. In reality my code doesn't call close(), but rather, merely reduces the reference count on the client. The client cache also has a reference that keeps it alive for the life of the process. When the cache finally shuts down, it releases its reference, and if anyone is using the client at that moment, their reference keeps it open until the last guy is done using it.

The main point is this: Do the jersey client code authors expect me to drop my reference to (and potentially close) the client at point A, point B, or point C?

My rationale for asking this question is simple: I want to do things right so as not to close it prematurely. Yes, I could play it safe and always just close it a point C - that would always work. However, if any code in completed or failed threw an exception, I'd have to catch that and close in a finally clause to make sure I actually closed the resource.

Thanks in advance,
John Calcote

Back to the top