Java - Retrieving Result from OkHttp Asynchronous GET

倾然丶 夕夏残阳落幕 提交于 2019-12-19 04:57:15

问题


So I have a web-app in Spring Boot, and there is a part where I make many HTTP requests to an API, and it seems to timeout if too many requests are made. I heard that switching from Synchronous to Asynchronous requests might help this issue.

Using OkHttp, this is what my Synchronous GET request looks like:

private JSONObject run(String url) throws Exception {
    Request newRequest = new Request.Builder()
            .url(url)
            .addHeader("Authorization", token)
            .build();

    try (Response response = client.newCall(newRequest).execute()) {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        return new JSONObject(response.body().string());

    }
}

I return the response as a JSON object from parsing the response body. However, in trying to use an OkHttp asynchronous call, it seems that I can't use the same approach. This is what I have so far:

public void runAsync(String url) throws Exception {
    Request request = new Request.Builder()
            .url(url)
            .addHeader("Authorization", token)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            System.out.println(response.body().string());
        }
    });
}

I cannot simply return the result as a JSON, as the response is wrapped inside of the Callback method, which has a return value of void. Any ideas on how I might achieve similar results to my Synchronous GET of extracting the response?


回答1:


I'm not a user of Spring Boot, so this is not a complete answer. But if it supports returning a Future, then it's trivial to bridge from OkHttp Callback to a Future.

This may be relevant https://spring.io/guides/gs/async-method/

As for producing the future

public class OkHttpResponseFuture implements Callback {
  public final CompletableFuture<Response> future = new CompletableFuture<>();

  public OkHttpResponseFuture() {
  }

  @Override public void onFailure(Call call, IOException e) {
    future.completeExceptionally(e);
  }

  @Override public void onResponse(Call call, Response response) throws IOException {
    future.complete(response);
  }
}

And then enqueue the job something like

  OkHttpResponseFuture callback = new OkHttpResponseFuture();
  client.newCall(request).enqueue(callback);

  return callback.future.thenApply(response -> {
    try {
      return convertResponse(response);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    } finally {
      response.close();
    }
  });

If you have multiple requests to process you can submit them separately and then wait on all results being available before combining and returning

  public static <T> CompletableFuture<List<T>> join(List<CompletableFuture<T>> futures) {
    CompletableFuture[] cfs = futures.toArray(new CompletableFuture[futures.size()]);

    return CompletableFuture.allOf(cfs)
        .thenApply(v -> combineIndividualResults(c));
  }


来源:https://stackoverflow.com/questions/42308439/java-retrieving-result-from-okhttp-asynchronous-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!