防止数据在重试失败调用时持续存在 - Spring 重试
Prevent data persist on retry failure call - Spring Retry
我在一个也用@Scheduled 注释的方法上使用@Retry,我尝试使用RestTemplate 连接另一个服务。
@Scheduled
@Retry
public void method(){
//Generate excel by getting data from DB
//Saving the document in DB
//Calling another service using rest template by passing document id
}
上面的代码工作正常,但如果另一个服务出现故障或出现任何异常,则会执行此完整方法并重试尝试再次调用它,因此会在 Db 中插入另一个文档。
所以问题是我可以只运行重试 RestTemplate 服务调用而不是再次创建文档吗?
我读了一些文章,其中提到我们应该尝试使我们的重试逻辑幂等。
所以现在检查同名文档是否已经存在我不保存文档而是从数据库中获取。
这样我就解决了问题。
@Scheduled
@Retry
public void method(){
//Generate excel by getting data from Db
//Checking document's availability in db
if(exists){
// Fetching from Db
}else{
//Saving the document in DB
}
//Calling another service using rest template by passing document id
}
我在一个也用@Scheduled 注释的方法上使用@Retry,我尝试使用RestTemplate 连接另一个服务。
@Scheduled
@Retry
public void method(){
//Generate excel by getting data from DB
//Saving the document in DB
//Calling another service using rest template by passing document id
}
上面的代码工作正常,但如果另一个服务出现故障或出现任何异常,则会执行此完整方法并重试尝试再次调用它,因此会在 Db 中插入另一个文档。
所以问题是我可以只运行重试 RestTemplate 服务调用而不是再次创建文档吗?
我读了一些文章,其中提到我们应该尝试使我们的重试逻辑幂等。
所以现在检查同名文档是否已经存在我不保存文档而是从数据库中获取。
这样我就解决了问题。
@Scheduled
@Retry
public void method(){
//Generate excel by getting data from Db
//Checking document's availability in db
if(exists){
// Fetching from Db
}else{
//Saving the document in DB
}
//Calling another service using rest template by passing document id
}