Mangu
Mangu

Reputation: 3325

Jersey client don't wait for result of GET method

I have a simple app in Java, using Jersey for the requests, where I hit some endpoints in other application using a GET request with Jersey like this:

Client client = new Client();
WebResource webResource = client.resource("MY_ENDPOINT");
webResource.get(String.class);

As you can see, I don't even care about the result of the endpoint, I just want to 'trigger' it, as the endpoint, once it receives a call, it will run some code on its own.

My 'issue' here is that I do this operation for 5 endpoints, and they usually take up to 3 seconds, and I don't need to wait that much, as the endpoint only returns an 'OK' message and I don't care about the actual message.

Is there any way to do this GET operation without 'blocking' Java? As in "do this call and ignore the result"? I would like to keep it with Jersey but I'm open to other ways.

Upvotes: 0

Views: 1085

Answers (2)

Mangu
Mangu

Reputation: 3325

I just moved to OkHttp:

    Request request = new Request.Builder().url(MY_ENDPOINT).build();
    client.newCall(request).enqueue(callback);

and the callback variable is just an empty Callback.

Upvotes: 1

Fabiano Tarlao
Fabiano Tarlao

Reputation: 3232

I have seen time ago that JAX-RS added async client/server workflows (here the link for Jersey implementation): https://jersey.github.io/documentation/latest/async.html

I have not used (and tested) it but I think you can try something like (consider like pseudocode):

  WebTarget target = client.target("http://targetsite/entrypoint");
  target.request().async().get(new InvocationCallback<Response>() {
      @Override
      public void completed(Response response) {
          return; //do nothing
      }

      @Override
      public void failed(Throwable throwable) {
          System.out.println("Error");
      }
  });
  System.out.println("Immediately here");

Please tell me in the case this works like intended, just curious

P.s: I think that, with this code, your program is still going to wait for the response but in another thread, you have not to manage things in the main thread, ..but this complexity is hidden.

Upvotes: 0

Related Questions