AsyncInvoker does not release a thread

I am using AsyncInvoker using Jersey 2.0. This works great for me. However, the flow does not end after the return is complete. Please note that I do not expect a response to the services that I call.

 public Future<Response> fire(WebTarget webTarget) { Future<Response> response = null; try { response = webTarget.request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .headers(headers) .async().get(); } catch (Exception e) { e.printStackTrace(); } return response; } 
+5
source share
2 answers

As long as you do nothing with the actual javax.ws.rs.core.Response , which is provided to you after resolving the future value, the response flow to the request remains open (and the flow of the raw HTTP request associated with it, like I Guess). You must either:

  • Do something with the javax.ws.rs.core.Response object and close it (or stream).
  • Use . get (MyPojo.class) to get the response pairs converted to the actual object. This will close the stream for you and allow the instance of MyPojo in the future instead.
0
source

You need to close your client object that you created in the calling method. In the calling method you will have something like below -

 Client client = ClientBuilder.newClient(); WebTarget webTarget = client.target(SERVER_URL).path(API_PATH).path(String.valueOf(id)); fire(webTarget); 

So you need to close client after calling this method -

 client.close() 

However, the recommended way to close client is after receiving a response. Something like below -

 public void fire(WebTarget webTarget) { try { webTarget.request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .headers(headers) .async().get(new InvocationCallback<Response>() { @Override public void completed(Response response) { // Do whatever your wish with response client.close(); } @Override public void failed(Throwable throwable) { throwable.printStackTrace(); client.close(); } }); } catch (Exception e) { e.printStackTrace(); } } 
0
source

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


All Articles