【问题标题】:Complete CompletableFuture with another CompletableFuture result用另一个 CompletableFuture 结果完成 CompletableFuture
【发布时间】:2021-02-24 20:00:04
【问题描述】:

我正在以这种方式进行异步 http 调用

public CompletableFuture<String> doPost(String path, String json) {
        CompletableFuture<String> result = new CompletableFuture<>();
        Request request = new Request.Builder().url(this.address + path).post(RequestBody.create(json, JSON)).build();
        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                result.completeExceptionally(new TerminationException());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                result.complete(response.body().string());
            }
        });
    }

但响应可能包含我需要重试的代码之一,并且代码应该是

@Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (!retries.contains(responce.code()) {
                    result.complete(response.body().string());
                } else {
                    // Do retry here
                }
            }

在重试中,我想递归调用 doPost 并使用它的返回值作为初始调用的结果。 所以它返回了一些可完成的未来,如何以异步方式完成初始 CF(没有 doint .get())?

谢谢。

【问题讨论】:

  • @JanezKuhar 我有高级 API,如 addUser(...)、removeUser(...)、addOrder(...) 等。它们都执行一些 json 序列化/反序列化并调用doPost 方法为请求传递字符串并接收字符串作为结果。在 doPost 内部,我想实现一次重试逻辑,因此对于 api 的所有方法,如果其中存在错误 503,则会重试。

标签: java asynchronous completable-future


【解决方案1】:

您可以使用委托,例如

public CompletableFuture<String> doPost(String path, String json) {
    CompletableFuture<String> result = new CompletableFuture<>();
    doPostImpl(this.address + path, json, result, 10);
    return result;
}

private void doPostImpl(
    String url, String json, CompletableFuture<String> result, int maxRetries) {

    Request request = new Request.Builder()
        .url(url).post(RequestBody.create(json, JSON)).build();

    httpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            result.completeExceptionally(new TerminationException());
        }

        @Override
        public void onResponse(
            @NotNull Call call, @NotNull Response response) throws IOException {

            if(maxRetries <= 0 || !retries.contains(response.code())) {
                result.complete(response.body().string());
            } else {
                doPostImpl(url, json, result, maxRetries - 1);
            }
        }
    });
}

前端方法委托给接收目标未来的方法。重试时,以相同的未来再次调用实现方法。因此,没有必要将结果从一个未来转移到另一个未来。


从字面上理解这个问题,您可以将未来的结果转移到另一个使用

future2.whenComplete((value,throwable) -> {
    if(throwable != null) future1.completeExceptionally(throwable);
    else future1.complete(value);
});

但这可能会创建一个依赖链,只要重试次数。如上所示,没有这个必要。

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 2017-09-09
    • 1970-01-01
    • 2023-01-04
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多