gpt4 book ai didi

Java - AsyncHttpClient - 中止正在进行的异步调用

转载 作者:行者123 更新时间:2023-11-30 06:19:33 26 4
gpt4 key购买 nike

我正在使用HttpAsyncClientGET一些url。在某些情况下,假设当我收到状态 HTTP-304 时,我想放弃正在进行的调用。我不想等待尸体,我不想在它上面花费机器资源。有什么办法可以取消这个吗?显然,诸如 futureResponse.cancel(true)futureResponse.finalize() 之类的东西不起作用。

Future<Response> futureResponse = httpClient.prepareGet("some-url").addHeader("some", "header").execute(new AsyncCompletionHandler<Response>() {
@Override
public State onStatusReceived(HttpResponseStatus status) throws Exception {
logger.info("status {}", status);
// CONDITIONAL FINISH THIS CALL
return super.onStatusReceived(status);
}

@Override
public Response onCompleted(Response response) throws Exception {
logger.info("its ok");
return response;
}

@Override
public void onThrowable(Throwable t) {
logger.error(t);
}
});

最佳答案

来自doc :

您可以返回 ABORT 来关闭连接。

    @Override
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
Integer status = responseStatus.getStatusCode();
if (304 == status) { // CONDITIONAL FINISH THIS CALL
return State.ABORT;
} else {
doSomething()
return ...
}
}

关于Java - AsyncHttpClient - 中止正在进行的异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48461469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com