我有3个级别的异步函数调用,
这是下面的代码
@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
函数中的}接口可以访问我的类成员函数?