V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
qwer666df
V2EX  ›  科技

关于 httpasyncclient 异步效果不起作用

  •  
  •   qwer666df · Feb 11, 2022 · 1333 views
    This topic created in 1537 days ago, the information mentioned may be changed or developed.
    我理解的是当前 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("结束了");
    }
    ```
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3310 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 193ms · UTC 13:27 · PVG 21:27 · LAX 06:27 · JFK 09:27
    ♥ Do have faith in what you're doing.