0

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.

Mangu
  • 3,160
  • 2
  • 25
  • 42
  • Maybe not duplicated but similar: https://stackoverflow.com/questions/29906956/jersey-client-async-post-request-to-not-wait-for-response – AntonioGarcía Dec 08 '18 at 10:09
  • 2
    I don't know if this client provides asynchronous calls, but if not, you can always run it in a thread. One other point, by the HTTP specification a GET request is meant to be idempotent. This may not be the case if you run some code because of it. You may want to change it to a POST request. – Henry Dec 08 '18 at 10:16
  • @Henry For more context, the code that's run it's a [Cloud Function](https://cloud.google.com/functions/), so, I could change the method to POST. Yeah, I could create threads, but I'm trying to find a solution that doesn't rely on them. – Mangu Dec 08 '18 at 10:22

2 Answers2

1

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.

Mangu
  • 3,160
  • 2
  • 25
  • 42
0

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.

Fabiano Tarlao
  • 3,024
  • 33
  • 40