如何处理多个ListenableFutures? (弹簧)

时间:2016-04-03 15:23:18

标签: java spring asynchronous

我正在编写一个控制器,我需要让它异步。如何处理ListenableFuture列表?因为我有一个URL列表,我需要逐个发送GET请求,它的最佳解决方案是什么?

@RequestMapping(value = "/repositories", method = RequestMethod.GET)
    private void getUsername(@RequestParam(value = "username") String username) {
        System.out.println(username);
        List<ListenableFuture> futureList = githubRestAsync.getRepositoryLanguages(username);
        System.out.println(futureList.size());
}

在服务中我使用List<ListanbleFuture>似乎不起作用,因为它是异步的,在控制器方法中我不能有futureList的大小来为它运行for loop回调。

public List<ListenableFuture> getRepositoryLanguages(String username){
      return getRepositoryLanguages(username, getUserRepositoriesFuture(username));
    }

private ListenableFuture getUserRepositoriesFuture(String username) throws HttpClientErrorException {
        HttpEntity entity = new HttpEntity(httpHeaders);
        ListenableFuture future = restTemplate.exchange(githubUsersUrl + username + "/repos", HttpMethod.GET, entity, String.class);
        return future;
    }
private List<ListenableFuture> getRepositoryLanguages(final String username, ListenableFuture<ResponseEntity<String>> future) {
        final List<ListenableFuture> futures = new ArrayList<>();
        future.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
            @Override
            public void onSuccess(ResponseEntity<String> response) {
                ObjectMapper mapper = new ObjectMapper();
                try {
                    repositories = mapper.readValue(response.getBody(), new TypeReference<List<Repositories>>() {
                    });
                    HttpEntity entity = new HttpEntity(httpHeaders);
                    System.out.println("Repo size: " + repositories.size());
                    for (int i = 0; i < repositories.size(); i++) {
                        futures.add(restTemplate.exchange(githubReposUrl + username + "/" + repositories.get(i).getName() + "/languages", HttpMethod.GET, entity, String.class));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("FAILURE in getRepositoryLanguages: " + throwable.getMessage());
            }
        });

        return futures;
    }

我应该使用ListenableFuture<List>而不是List<ListenableFuture>吗?

2 个答案:

答案 0 :(得分:5)

好像你有一个List<ListenableFuture<Result>>,但是你需要一个ListenableFuture<List<Result>>,所以你可以在所有期货完成时采取一个行动。

public static <T> ListenableFuture<List<T>> allOf(final List<? extends ListenableFuture<? extends T>> futures) {
    // we will return this ListenableFuture, and modify it from within callbacks on each input future
    final SettableListenableFuture<List<T>> groupFuture = new SettableListenableFuture<>();

    // use a defensive shallow copy of the futures list, to avoid errors that could be caused by
    // someone inserting/removing a future from `futures` list after they call this method
    final List<? extends ListenableFuture<? extends T>> futuresCopy = new ArrayList<>(futures);

    // Count the number of completed futures with an AtomicInt (to avoid race conditions)
    final AtomicInteger resultCount = new AtomicInteger(0);
    for (int i = 0; i < futuresCopy.size(); i++) {
        futuresCopy.get(i).addCallback(new ListenableFutureCallback<T>() {
            @Override
            public void onSuccess(final T result) {
                int thisCount = resultCount.incrementAndGet();

                // if this is the last result, build the ArrayList and complete the GroupFuture
                if (thisCount == futuresCopy.size()) {
                   List<T> resultList = new ArrayList<T>(futuresCopy.size());
                    try {
                        for (ListenableFuture<? extends T> future : futuresCopy) {
                            resultList.add(future.get());
                        }
                        groupFuture.set(resultList);
                    } catch (Exception e) {
                        // this should never happen, but future.get() forces us to deal with this exception.
                        groupFuture.setException(e);
                    }
                }
            }

            @Override
            public void onFailure(final Throwable throwable) {
                groupFuture.setException(throwable);

                // if one future fails, don't waste effort on the others
                for (ListenableFuture future : futuresCopy) {
                    future.cancel(true);
                }
            }
        });
    }

    return groupFuture;
}

答案 1 :(得分:2)

我不太确定你是在开始一个新项目还是在开发一个新项目,但如果你的主要要求是无阻塞和异步休息服务,我建议你去看看即将推出的Spring Framework 5吧与reactive streams集成。特别是Spring 5将允许您创建完全被动和异步的Web服务,几乎没有编码。

例如,您的代码的完整功能版本可以使用这个小代码片段编写。

@RestController
public class ReactiveController {
    @GetMapping(value = "/repositories")
    public Flux<String> getUsername(@RequestParam(value = "username") String username) {
        WebClient client = WebClient.create(new ReactorClientHttpConnector());
        ClientRequest<Void> listRepoRequest = ClientRequest.GET("https://api.github.com/users/{username}/repos", username)
            .accept(MediaType.APPLICATION_JSON).header("user-agent", "reactive.java").build();

        return client.exchange(listRepoRequest).flatMap(response -> response.bodyToFlux(Repository.class)).flatMap(
            repository -> client
                    .exchange(ClientRequest
                            .GET("https://api.github.com/repos/{username}/{repo}/languages", username,
                                    repository.getName())
                            .accept(MediaType.APPLICATION_JSON).header("user-agent", "reactive.java").build())
                    .map(r -> r.bodyToMono(String.class)))
            .concatMap(Flux::merge);
    }

    static class Repository {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}

要在本地运行此代码,只需克隆spring-boot-starter-web-reactive并将代码复制到其中。

结果类似{"Java":50563,"JavaScript":11541,"CSS":1177}{"Java":50469}{"Java":130182}{"Shell":21222,"Makefile":7169,"JavaScript":1156}{"Java":30754,"Shell":7058,"JavaScript":5486,"Batchfile":5006,"HTML":4865},你仍然可以将它映射到更有用的异步方式:)