Problem connecting clients

I am using Jersey v10 and wrote the following code. This is the right way to close the client connection in Jersey to avoid memory leaks. I did not make any calls before this happened.

ClientConfig config = setupHttps(); final Client c = Client.create(config); final WebResource r = c.resource(baseUri); ClientResponse response = null; try { response = r.path("/....") .header("contentId", id) .header("sid", sid).get(ClientResponse.class); ... } catch (Exception e) { log.error("Error returning contentServiceName."); } finally { if (response != null) { response.close(); } if (c!= null) { c.destroy(); } } 

TIA, Vijay

+4
source share
1 answer

As far as I know, yes, this is the right way to close a Jersey client ... with the following reservations.

1) What you are trying to prevent is not a memory leak, but a connection (to the address you are accessing) of a leak ...

2) The Client section says the following in chapter 3 of the Jersey Handbook

Customer instances are expensive resources. It is recommended that you use a configured instance to create web resources. Building web resources, building queries, and receiving responses ensures thread safety. This way, client instance instances and WebResource instances can be shared across multiple threads.

Therefore, if you plan to make multiple calls, it is not a good idea to call destroy for each call. The same (but to a lesser extent) is true for WebResources.

3) What I am describing is from Jersey 1.1 (but I see topics about it back in 2009 ).

+8
source

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


All Articles