Java - 从 OkHttp 异步 GET 检索结果

Ale*_*tad 2 java spring asynchronous okhttp okhttp3

所以我在 Spring Boot 中有一个 web 应用程序,并且有一部分我向 API 发出了许多 HTTP 请求,如果发出的请求太多,它似乎会超时。我听说从同步请求切换到异步请求可能有助于解决这个问题。

使用 OkHttp,这就是我的同步 GET 请求的样子:

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());

    }
}
Run Code Online (Sandbox Code Playgroud)

我通过解析响应正文将响应作为 JSON 对象返回。但是,在尝试使用 OkHttp 异步调用时,似乎无法使用相同的方法。这是我到目前为止:

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());
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我不能简单地将结果作为 JSON 返回,因为响应包含在回调方法中,该方法的返回值为 void。关于如何获得与提取响应的同步 GET 类似的结果的任何想法?

Yur*_*mke 5

我不是 Spring Boot 的用户,所以这不是一个完整的答案。但是如果它支持返回一个 Future,那么从 OkHttp Callback 桥接到一个 Future 就很简单了。

这可能是相关的https://spring.io/guides/gs/async-method/

至于生产未来

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

然后将工作加入队列

  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();
    }
  });
Run Code Online (Sandbox Code Playgroud)

如果您有多个请求要处理,您可以分别提交它们,然后等待所有结果可用,然后再合并和返回

  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));
  }
Run Code Online (Sandbox Code Playgroud)