Java - 从 OkHttp 异步 GET 中获取结果
Java - Retrieving Result from OkHttp Asynchronous GET
所以我在 Spring 引导中有一个网络应用程序,并且有一部分我向 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());
}
}
我 return 将响应作为 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());
}
});
}
我不能简单地将结果 return 作为 JSON,因为响应包含在回调方法中,它的 return 值为 void。关于如何实现与提取响应的同步 GET 类似的结果的任何想法?
我不是 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);
}
}
然后将作业加入队列
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();
}
});
如果您有多个请求需要处理,您可以分别提交它们,然后等待所有结果可用,然后再合并和返回
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));
}
所以我在 Spring 引导中有一个网络应用程序,并且有一部分我向 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());
}
}
我 return 将响应作为 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());
}
});
}
我不能简单地将结果 return 作为 JSON,因为响应包含在回调方法中,它的 return 值为 void。关于如何实现与提取响应的同步 GET 类似的结果的任何想法?
我不是 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);
}
}
然后将作业加入队列
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();
}
});
如果您有多个请求需要处理,您可以分别提交它们,然后等待所有结果可用,然后再合并和返回
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));
}