简体   繁体   English

在Spring Java中同时调用多Web服务

[英]Calling mutiple webservice at same time in Spring Java

I have a list of 30 servers and I have to make a REST call to each server to get their status. 我有30台服务器的列表,我必须对每个服务器进行REST调用以获取其状态。 Currently I iterating through list of server and sequentially calling each REST call against each server. 目前,我遍历服务器列表,并针对每个服务器依次调用每个REST调用。 So totally it takes around 30 seconds in total to get the response from each server before returning the result to JSP VIEW. 因此,将结果返回到JSP VIEW总共需要大约30秒才能从每个服务器获得响应。

How can we improve this? 我们该怎样改进这个?

One option you could consider is the Java8 streams like: 您可以考虑的一种选择是Java8流,例如:

public void check() {
    List<String> endPoints = Arrays.asList("http://www.google.com", "http://www.stackoverflow.com", "inexistent");
    {
        // this will execute the requests in parallel
        List<Boolean> collected = performCheckOverStream(endPoints.parallelStream());
        System.out.println(collected);
    }
    {
        // this will execute the requests in serial
        List<Boolean> collected = performCheckOverStream(endPoints.stream());
        System.out.println(collected);
    }

}

private List<Boolean> performCheckOverStream(Stream<String> stream) {
    List<Boolean> collected = stream.map(new Function<String, Boolean>() {
        @Override
        public Boolean apply(String t) {
            // do what you need here
        }
    }).collect(Collectors.toList());
    return collected;
}

Using Spring you could either use a @Async annotated method or even use the AsyncRestTemplate, in both cases you will receive a Future<?> . 使用Spring,您可以使用@Async注释的方法,甚至可以使用AsyncRestTemplate,在两种情况下,您都将收到Future<?> A nice introduction to @Async can be found here and to the AsyncRestTemplate here . 一个很好的介绍@Async可以发现这里和到AsyncRestTemplate 这里

You can do it via ThreaPool like this , with Thread count as your API call count. 您可以像这样通过ThreaPool通过线程计数作为API调用计数来实现。

public void REST_Thread_executor(int Thread_count, ArrayList URLS) {
    ExecutorService executor = Executors.newFixedThreadPool(Thread_count);
    for (int i = 0; i < Thread_count; i++) {
        String URL = URLS.get(i).toString();
        Runnable worker = new MyRunnable(URL);
        executor.execute(worker);
    }
    executor.shutdown();
    while (!executor.isTerminated()) {

    }
}

public String restAPICALL(URL) {
    GET or POST or PUT or DELETE 
}

public static class MyRunnable implements Runnable {

    private final String URL;

    RESTThreadExecutor restThreadExecutor = new RESTThreadExecutor();

    MyRunnable(String URL) {
        this.URL = URL;
    }

    @Override
    public void run() {
        restThreadExecutor.restAPICALL(URL);
    }
}

You can use the CompletableFuture Interface from java 9. Or the enable on your app the @EnableAsync and on your method use the @Async that will return to you an interface Future. 您可以使用Java 9中的CompletableFuture接口。或者在应用程序上启用@EnableAsync,并在方法上使用@Async,它将返回给您一个Future接口。 The both are asynchronous stream. 两者都是异步流。

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

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