如何处理CompletableFuture.supplyAsync抛出的异常

时间:2016-11-14 09:24:41

标签: java-8

我有以下代码块。我得到org.springframework.web.context.request.async.AsyncRequestTimeoutException catch块没有处理它。谁能告诉我如何处理下面supplyAsync块抛出的异常?

 @org.springframework.scheduling.annotation.Async
    public CompletableFuture<ResponseEntity<?>> getTeam(String teamCode) {
        CompletableFuture.supplyAsync(() -> {
            CricketTeam team;
            try {
                team = service.getTeamInfo(teamCode);
            } catch (Exception ex) {
                Error error = new Error(500, ex.getMessage());
                return new ResponseEntity<>(error, HttpStatus.OK);
            }
            return new ResponseEntity<>(team, HttpStatus.OK);
        });

}

1 个答案:

答案 0 :(得分:3)

也许是这样的(你必须调整类型):

    CompletableFuture.supplyAsync(() -> possiblyFailingMethod())
            .exceptionally((e) ->
                    {
                        log.error("Something went wrong", e);
                        return "KO";
            });
相关问题