V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
qwer666df
V2EX  ›  科技

关于 httpasyncclient 异步效果不起作用

  •  
  •   qwer666df · 2022-02-11 14:56:17 +08:00 · 700 次点击
    这是一个创建于 776 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我理解的是当前 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("结束了");
    }
    ```
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5876 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 02:18 · PVG 10:18 · LAX 19:18 · JFK 22:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.