java - Retry for specific exception in Reactor
问题描述
I have microservices application. For collaborating each service uses async messaging. I know, that spring data jpa uses optimistic lock by default. But in case of this lock in methods called not by user, but by another service (in my example there are validation services, that can update status of object to valid or non valid) I want to handle exception and retry to update object. Also I had to use webflux stack on this microservice. Now i have such code:
public void updateStatus(String id, EventStatus status) {
eventRepository.findById(id)
.doOnNext(eventDocument -> {
eventDocument.setStatus(status);
eventRepository.save(eventDocument).subscribe();
}).doOnError(OptimisticLockingFailureException.class, exception -> { //Retry in 2 sec if optimistic lock occurs on update
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
updateStatus(id, status);
})
.subscribe();
}
I don't like recursion here. Is there any better solution for this?
解决方案
The following retries every 2 seconds in case OptimisticLockingFailureException
is thrown.
private static final int MAX_ATTEMPTS = Integer.MAX_VALUE;
public void updateStatus(String id, EventStatus status) {
eventRepository.findById(id)
.map(eventDocument -> {
eventDocument.setStatus(status);
return eventDocument;
})
.flatMap(event -> Mono.defer(() -> eventRepository.save(eventDocument)
.retryWhen(Retry.backoff(MAX_ATTEMPTS, Duration.ofSeconds(2))
.filter(t -> t instanceof OptimisticLockingFailureException))))
.subscribe();
}
推荐阅读
- symfony - symfony 4 使用 UsernamePasswordToken 保持用户身份验证
- php - in_array() 如何检查两个值 PHP WordPress
- kubernetes - 本地无法访问kubernetes服务
- javascript - 如何计算以十为底的数字?
- windows - Docker Windows 容器上的 MSBuild.exe 冻结
- java - IntelliJ 中的 Maven 测试结果
- jsf - JSF 在一个数据表中显示两个列表
- laravel - 在请求中检查 Laravel 中的验证
- python - 如何在 Python 中编写一个函数来打印一个月的一周?
- flutter - 如果我单击设备后退按钮,它应该重定向到上一页,但它直接转到应用程序的主页