Resilience4j + Spring boot @Retry 不使用异步方法
Resilience4j + Spring boot @Retry not working with async methods
我有一个 @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.");
}
测试:
@Test(expected = InternalServerErrorDarwinException.class)
public void procesarOperacionPendienteKO1() throws InterruptedException, ExecutionException, ParseException {
when(rest.exchange(Mockito.anyString(),
Mockito.any(HttpMethod.class),
Mockito.any(HttpEntity.class),
Mockito.eq(Void.class)))
.thenThrow(new NullPointerException());
this.operacionesPendientesService.insertarOperacionPendiente(obtenerOperacionPendienteWeb()).get();
verify(rest, timeout(100).times(1)).exchange(Mockito.anyString(),
Mockito.any(HttpMethod.class),
Mockito.any(HttpEntity.class),
Mockito.eq(Void.class));
}
我是不是漏掉了什么?
谢谢!
您的代码如下所示:
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);
}
我有一个 @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.");
}
测试:
@Test(expected = InternalServerErrorDarwinException.class)
public void procesarOperacionPendienteKO1() throws InterruptedException, ExecutionException, ParseException {
when(rest.exchange(Mockito.anyString(),
Mockito.any(HttpMethod.class),
Mockito.any(HttpEntity.class),
Mockito.eq(Void.class)))
.thenThrow(new NullPointerException());
this.operacionesPendientesService.insertarOperacionPendiente(obtenerOperacionPendienteWeb()).get();
verify(rest, timeout(100).times(1)).exchange(Mockito.anyString(),
Mockito.any(HttpMethod.class),
Mockito.any(HttpEntity.class),
Mockito.eq(Void.class));
}
我是不是漏掉了什么?
谢谢!
您的代码如下所示:
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);
}