关于 httpasyncclient 异步效果不起作用

2022-02-11 14:56:17 +08:00
 qwer666df
我理解的是当前 main 请求 B 接口, 然后不需要管 B 接口的处理时间, main 线程立马就返回
我现在对 B 接口 做了线程等待 10s 在操作的处理
然后 main 线程发起请求的时候, 同样也要等到 10s, 而不是直接输出 "结束了"
```
// 1.创建 CallBack
private static final class MyCallback implements FutureCallback<HttpResponse> {

public void failed(final Exception ex) {
System.out.println("请求失败");
}

public void completed(final HttpResponse response) {
System.out.println("完成");
}

public void cancelled() {
System.out.println("cancelled");
}
}

public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
// 2.创建异步 httpclient 对象
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().build();

// 3.发起调用
try {

// 3.0 启动
httpclient.start();
// 3.1 请求参数
HttpGet httpget1 = new HttpGet("http://localhost:8080/service/test");
// 3.2 发起请求,不阻塞,马上返回
httpclient.execute( httpget1, new MyCallback());

} finally {
httpclient.close();
}

System.out.println("结束了");
}
```
725 次点击
所在节点   科技
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/833211

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX