1

我有两个模块,一个从休息模板调用另一个。(管理员调用 notifServer)notifServer 有一个用 注释的方法@Async。我想在该方法中抛出异常,但是管理员太快得到响应,并且无法在管理员处捕获异常方法。

我是 spring 和 @Async 流程的新手。我尝试将响应正文从 NotifServer 映射到 C CompletableFuture.class。但我仍然没有收到错误响应。

此代码来自管理员

ResponseEntity response = fcmRestTemplate.exchange(nsUrl + "/fcm/admin/" + bulkFcmId, HttpMethod.POST,
                    HttpEntityUtils.getHttpEntity(moduleCode), CompletableFuture.class);
            if (response.getStatusCode() != HttpStatus.CREATED && response.getStatusCode() != HttpStatus.ACCEPTED) {
                String errorMessage = ErrorResourceUtil.getErrorMessage((HashMap) response.getBody(),"Unable to send fcm");
                setStatusToFailedByBulkFcmId(bulkFcmId);
                throw new ClientException(errorMessage);
            }

现在这是来自NotifServer

JobExecution jobExecution = jobLauncher
                    .run(importJob, new JobParametersBuilder()
                    .addString("fullPathFileName", TMP_DIR)
                    .addString("batch_fcm_id", String.valueOf(id))
                    .addLong("time",System.currentTimeMillis())
                    .toJobParameters());
            if(jobExecution.getStepExecutions().stream().map(StepExecution::getStatus).findFirst().get().equals(BatchStatus.ABANDONED)){
             throw new ClientException("INVALID CSV");

这是用@Async 注释的。

那么有没有办法让我在管理员的响应正文中捕获客户端异常?

编辑 这是来自 notifServer 的 API

@ResponseStatus(HttpStatus.CREATED)
    @PostMapping(value = "/admin/{bulkFcmId}")
    public void pushFCMByAdmin(@PathVariable Long bulkFcmId) {
        fcmService.sendFcmByAdmin(bulkFcmId, AuthUtil.getCurrentUser());
    }

然后 sendFcmByAdmin 有 @Async 注释。

4

1 回答 1

0

在下面的代码中,您是否可以将返回类型提供为业务对象而不是 CompletableFuture.class。由于您将 CompletableFuture.class 作为参数传递给交换,因此它需要 CompletableFuture.class 类型的响应返回值

ResponseEntity response = fcmRestTemplate.exchange(nsUrl + "/fcm/admin/" + bulkFcmId, HttpMethod.POST,
                        HttpEntityUtils.getHttpEntity(moduleCode), CompletableFuture.class);
                if (response.getStatusCode() != HttpStatus.CREATED && response.getStatusCode() != HttpStatus.ACCEPTED) {
                    String errorMessage = ErrorResourceUtil.getErrorMessage((HashMap) response.getBody(),"Unable to send fcm");
                    setStatusToFailedByBulkFcmId(bulkFcmId);
                    throw new ClientException(errorMessage);
                }

您可以尝试按如下方式创建它,而不是传递 Completable Future :

使用异步方法在管理员中进行其余模板调用:

@Async
public CompletableFuture<List<BusinessObject>> getResponseAsynchronously(String value) {
    String url = "https://restendpoint.eu/rest/v2/lang/" + value + "?fields=name";
    BusinessObject[] response = restTemplate.getForObject(url, Country[].class);
    return CompletableFuture.completedFuture(Arrays.asList(response));
}

然后在控制器中读取 CompletableFuture ,如:

@GetMapping("")
public List<String> getAllDataFromRestCall() throws Throwable {

    CompletableFuture<List<BusinessObject>> businessObjectsFuture = countryClient.getResponseAsynchronously("fr");
    List<String> europeanFrenchSpeakingCountries;

            try {
                europeanFrenchSpeakingCountries = new ArrayList<>(businessObjectsFuture
                .get()
                .stream()
                .map(Country::getName)
                .collect(Collectors.toList()));

     } catch (Throwable e) {
       throw e.getCause();
     }

   return europeanFrenchSpeakingCountries;
}
于 2019-04-05T06:13:09.213 回答