Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jersey-dev] [External] : client lifecycle question
  • From: Jan Supol <jan.supol@xxxxxxxxxx>
  • Date: Tue, 24 May 2022 16:59:10 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wx77dmt5BJ944AEnYWtKl6ZCCI9n2DGcvirK49EOTWQ=; b=OHaY4i+qMZn9inpMHSpmJZKrxK97wPdaxQVyabrlafNS16DCHRZjfJFZKgMzudwr/ugV5AjmiRHqX6fxLgnTolKTHYT0OPaQz8GnIuembWagNAxTLJFziODH+NKi8XkzFxQTzpBtK/5O0bmPPc0Y0+mrOZn0tfn45v4RHj09v9UOLPitrEFQS57wjSb0kqh9FB0ouTl/q68rYlpu5iydwLnxQ8P0zXRNTnMjuXxF3a/TDdd847hCoFbmS6CHAlCHWb9TP/XdtxaF9Qhc/rghGJYcjUff5/yV6geO7Vn8qBPkALA6KafNuyp4PcZVac6qNiDU+0SGXKc473S+Fdxt2A==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g1P3waGV2L44o4Wi5qR5C6B43qsNvgbH/ZC+aXPST8yOlQ5xRw4kPM76pdZFpB7mnrIE29oJAJfUL4m+h5FNxgKwkybUBvImyi3Qjm8fs/3peMf1Mqota37c9ZBQA9R9ePkmxAftQDk8txoiMuDYhIMwfMrVSb7FSq0csUv4a4u/xXd3cHF//58T+2Y0LuJmSj273IKY3YqDUSO8ZLS/ZQ3jHKrvm3I2x9rIf9w6nZbhdVLbTnkf2gwqFDwjS+93wBjA7eufsLRoRdpwKz9ZV/hqHXq0t2bqb8jWANyaVLlUZ/VwID4f1RTi5/LgOpLDpwOzmikct4g3k+yzSbzGQg==
  • Delivered-to: jersey-dev@xxxxxxxxxxx
  • List-archive: <https://www.eclipse.org/mailman/private/jersey-dev/>
  • List-help: <mailto:jersey-dev-request@eclipse.org?subject=help>
  • List-subscribe: <https://www.eclipse.org/mailman/listinfo/jersey-dev>, <mailto:jersey-dev-request@eclipse.org?subject=subscribe>
  • List-unsubscribe: <https://www.eclipse.org/mailman/options/jersey-dev>, <mailto:jersey-dev-request@eclipse.org?subject=unsubscribe>
  • Msip_labels:
  • Thread-index: AQHYbKFw1UYydkgid0OyvNk/a+7giq0uPDUz
  • Thread-topic: [External] : [jersey-dev] client lifecycle question

Hi John,
>From the top of my head, I'd say closing the client at point A nor B will work. The client should not be closed before the response is read, closing the client will drop Jersey runtime classes required to read the response properly. So closing it at point A or B could work or might not work, depending on whether the garbage collection took place or when the thread invoking the InvocationCallback will process it.

Note that the client does not need to be closed after a response, it could be reused for a new request.

The client itself is a lightweight object, but it keeps WeakReferences to heavy runtime objects. Closing the client makes sure the heavy runtime objects are released. However, Jersey tries to release the heavy objects on its own, when not needed (garbage collected). In some cases, however, the users keep references to Jersey internal objects, such as ClientResponse which prevents the proper garbage collection (and resource release). The client is better to be closed, then. In an ideal situation not closing the client should not cause leaking. I have seen more issues caused by not closing the Response.

HTH,
Jan

From: jersey-dev <jersey-dev-bounces@xxxxxxxxxxx> on behalf of John Calcote <john.calcote@xxxxxxxxx>
Sent: Saturday, May 21, 2022 1:25 AM
To: jersey-dev@xxxxxxxxxxx <jersey-dev@xxxxxxxxxxx>
Subject: [External] : [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