Spring boot 弹性4j+;Spring boot@Retry不使用异步方法

Spring boot 弹性4j+;Spring boot@Retry不使用异步方法,spring-boot,junit,mockito,resilience4j,resilience4j-retry,Spring Boot,Junit,Mockito,Resilience4j,Resilience4j Retry,我有一个@Async方法,尝试在其中添加@Retry,但当异常发生时,不会执行回退方法。我还试图测试抛出异常的模拟,但由于它从未进入回退方法,因此它永远不会成功 这是我的代码: @Retry(name = "insertarOperacionPendienteService", fallbackMethod = "fallbackInsertarOperacionPendiente") @Override @Async public Completable

我有一个
@Async
方法,尝试在其中添加
@Retry
,但当异常发生时,不会执行回退方法。我还试图测试抛出异常的模拟,但由于它从未进入回退方法,因此它永远不会成功

这是我的代码:

@Retry(name = "insertarOperacionPendienteService", fallbackMethod = "fallbackInsertarOperacionPendiente")
@Override
@Async
public CompletableFuture<String> insertarOperacionPendiente(final OperacionPendienteWeb operacionPendienteWeb)  throws InterruptedException, ExecutionException {
    StringBuilder debugMessage = new StringBuilder("[insertarOperacionPendiente] Operacion pendiente a insertar en BB.DD.: ").append(operacionPendienteWeb);
    CompletableFuture<String> result = new CompletableFuture<>();
    HttpEntity<List<OperacionPendienteWeb>> entity = new HttpEntity<>();
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("url");
    try {
        rest.exchange(builder.toUriString(), HttpMethod.POST, entity, Void.class);
    } catch (HttpClientErrorException | HttpServerErrorException e) {
        result.completeExceptionally(e);
    } catch (Exception e) {
        result.completeExceptionally(e);
    }   

    result.complete("OK");
    return result;
}

public CompletableFuture<String> fallbackInsertarOperacionPendiente(Exception e) {
       System.out.println("HI");
       throw new InternalServerErrorDarwinException("Error al insertar la operacion pendiente.");
}
我错过什么了吗


谢谢

您的代码如下所示:

try {
    rest.exchange(builder.toUriString(), HttpMethod.POST, entity, Void.class);
} catch (HttpClientErrorException | HttpServerErrorException e) {
    result.completeExceptionally(e);
} catch (Exception e) {
    result.completeExceptionally(e);
}   

result.complete("OK");
因此,在最后一行中,您总是将结果设置为完成

将其更改为:

try {
    rest.exchange(builder.toUriString(), HttpMethod.POST, entity, Void.class);
    result.complete("OK");
} catch (HttpClientErrorException | HttpServerErrorException e) {
    result.completeExceptionally(e);
} catch (Exception e) {
    result.completeExceptionally(e);
}   

我首先注意到的一件事是,您捕获了所有异常。因此,除了(e)之外,不会有重试IMHOresult.Complete;就像一个“扔”。如果我改为使用“throw”,它也不起作用,如果我将该方法放回同步状态,它会起作用。它现在正在执行回退,但它返回的是ExecutionException,而不是InternalServerErrorDarwinException。为什么?您的异常嵌套在ExecutionException中吗?如果您调用异常的getCause(),我不知道“嵌套”是什么意思。您的异常是原因吗?并且您确定调用了FallbackInsertaPropertiationPendiente吗?
try {
    rest.exchange(builder.toUriString(), HttpMethod.POST, entity, Void.class);
    result.complete("OK");
} catch (HttpClientErrorException | HttpServerErrorException e) {
    result.completeExceptionally(e);
} catch (Exception e) {
    result.completeExceptionally(e);
}