简体   繁体   English

如何使用Unirest并行发送多个异步请求

[英]How to send multiple asynchronous requests in parallel using Unirest

While using Unirest, the program doesn't exit until we manually shutdown every thread by invoking Unirest.shutdown() . 在使用Unirest时,直到我们通过调用Unirest.shutdown()手动关闭每个线程后,程序才会退出。 If I had to make just one request, it's easy: 如果我只需要提出一个请求,这很容易:

private static void asyncRequest (String link) {
    try {
        Future <HttpResponse <JsonNode>> request = Unirest.head(link).asJsonAsync(
                new Callback<JsonNode>() {
                    @Override
                    public void completed(HttpResponse<JsonNode> httpResponse) {
                        print(httpResponse.getHeaders());
                        try {
                            Unirest.shutdown();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void failed(UnirestException e) {
                        print(e.getMessage());
                    }

                    @Override
                    public void cancelled() {
                        print("Request cancelled");
                    }
                }
        );

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws Exception {
   asyncRequest("https://entrepreneur.com");
}

But I have to make multiple HTTP request in parallel (subsequent requests are meant not to wait for previous requests to complete). 但是我必须并行发出多个HTTP请求(后续的请求不应等待先前的请求完成)。 In the code above, I have to execute the code inside asyncRequest more than once with different link s. 在上面的代码中,我必须使用不同的link多次在asyncRequest执行代码。 The problem is I can't decide when to invoke Unirest.shutdown() so that the program exits as soon as the last request receives response. 问题是我无法确定何时调用Unirest.shutdown()以便程序在最后一个请求收到响应后立即退出。 If I call Unirest.shutdown() after all the calls to asyncRequest in main , some or all the requests might get interrupted. 如果在mainasyncRequest所有调用之后都调用Unirest.shutdown() ,则某些或所有请求可能会被中断。 If I call it inside completed (and other overridden methods), only the first request is made and others are interrupted. 如果我在completed (和其他重写的方法)内部调用它,则仅发出第一个请求,而其他请求被中断。 How can I solve this? 我该如何解决?

In theory, you could make the current thread wait for the execution of the method and after they are all done you can call the shutdown. 从理论上讲,您可以让当前线程等待方法的执行,并在完成所有操作后调用关闭程序。 But this would make the whole process synchronous, which is not what we want. 但这会使整个过程同步,这不是我们想要的。 So what I would do is, run different thread (other than the main one) which will wait for the execution of all your http requests. 所以我要做的是,运行不同的线程(主线程除外),该线程将等待所有http请求的执行。 To do so you can use the class CountDownLatch , initializing with the countdown before it releases the control to the parent thread. 为此,您可以使用CountDownLatch类,在将控件释放到父线程之前,先用倒数计时初始化。 You pass the instance of the CountDownLatch to the async method and you decrease by one each time you complete an http request. 您将CountDownLatch的实例传递给async方法,并在每次完成http请求时减少by one When it reaches 0 it returns the control to the parent thread, where you know you can call shutdown method as all your requests are done. 当它达到0时,它将控制权返回给父线程,在此您可以在所有请求完成后调用shutdown方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM