使用回调Java执行嵌套的异步函数调用

时间:2019-07-10 02:30:49

标签: java asynchronous asynchttpclient

我有3个级别的异步函数调用,

  1. 我得到一个事件。
  2. 我异步地从API获取事件相关数据。
  3. 一旦回调完成,我将处理数据并异步调用另一个API。
  4. 第二个API给我响应后,我准备发布最终数据并将其异步发送到服务。
  5. 如果上述所有步骤都成功,那么我会将从卡夫卡消费的那批消息标记为成功,然后提交给卡夫卡。

这是下面的代码

 @Override
    public void run() {
        Request r1 = getr1();
        this.asyncHttpClient.executeRequest(r1, new AsyncCompletionHandler<Response>() {
            @Override
            public Response onCompleted(Response res1) {
                try {
                    if (res1.getStatusCode() == 200) {
                        LOG.info("Successfully retrieved samples from R1");
                        R1Response r1Response = Utils.GSON.fromJson(
                                res1.getResponseBody(), R1Response.class
                        );
                        final List<T1> t1 = r1Response.getData().stream().
                                filter(Objects::nonNull).map(T1::getSample).filter(Objects::nonNull).
                                map(x -> Utils.GSON.fromJson(x, T1.class)).
                                collect(Collectors.toList());
                        final List<T2> l1 = new LinkedList<>();
                        final Request r2 = getr2(t1, l1);
                        asyncHttpClient.executeRequest(r2, new AsyncCompletionHandler<Response>() {
                            @Override
                            public Response onCompleted(Response r2) {
                                try {
                                    if (r2.getStatusCode() == 200) {
                                        LOG.info("Got success response from service");
                                        T3 r3 = Utils.GSON.fromJson(
                                                r2.getResponseBody(), T3.class
                                        );
                                        T4 iocMessages = getT4(l1, r3);
                                        grpcClient.flushAsync(iocMessages, x -> {
                                            if (x.getCode().equals(x.Retcode.INTERNAL_ERROR)) {
                                                LOG.error("Failed to publish to service, error message: {}",
                                                        x.getErrmsg());
                                                markIntervalFailed();
                                            } else {
                                                LOG.info("Callback completed: response code: {}", x.getCode());
                                                markIntervalSuccess();
                                            }
                                        });
                                    } else {
                                        LOG.error("Failed to get the response from the API, status code: {}",
                                                r2.getStatusCode());
                                        LOG.error("Response body: {}", r2.getResponseBody());
                                        markIntervalFailed();
                                    }
                                } catch (Exception e) {
                                    LOG.error("Exception encountered while waiting for response from API: {}",
                                            e.getMessage());
                                    LogExceptionTrace.logExceptionStackTrace(e);
                                    markIntervalFailed();
                                }
                                return r2;
                            }
                        });
                    } else {
                        LOG.error("Failed to get the response from the R1, status code: {}",
                                res1.getStatusCode());
                        LOG.error("Response body: {}", res1.getResponseBody());
                        markIntervalFailed();
                    }
                } catch (Exception e) {
                    LOG.error("Exception encountered while waiting for response from R1: {}", e.getMessage());
                    LogExceptionTrace.logExceptionStackTrace(e);
                    markIntervalFailed();
                }
                return res1;
            }
        });
    }

我很好奇,因为所有这些回调都是由IO线程处理的,所以将调用一个接一个地链接是一个好主意,而且我对{{1}的内联实现方式有点困惑AsyncCompletionHandler函数中的}接口可以访问我的类成员函数?

0 个答案:

没有答案