Java 11在自定义条件下可完整的未来重试,而不是例外
I have a completable future defined below
CompletableFuture<Person> personFutures = personService.getPersons();
Now, based on a particular condition, I need to check and do the call to getPersons until the condition is matched or the number of retries (5 retries, 5seconds apart) have finished.
The condition will be
if(personFutures.get().size() != totalPersonsInOrg){
retry(personService.getPersons(), 5, 5)
} else {
return persons
}
I want to use the thenApply and thenCompose to chain these after the first completablefuture.
personFutures.thenApply(persons -> {
if(persons.size() != totalPersonsOrg){
retry(personservice,5,5)
}
})
这就是需要更改的
private boolean allPersonsFound(String id, int retry, int c
count)
{
if (retry > maxRetries) {
return false;
}
CompletableFuture<List<Persons>> personsFuture = personaService.getPersons();
List<Persons> persons = personsFuture.get();
if (persons.size() != count) {
//add delay of 50ms
return allPersonsFound(id, retry++, count);
}
return true;
}
I have a completable future defined below
CompletableFuture<Person> personFutures = personService.getPersons();
Now, based on a particular condition, I need to check and do the call to getPersons until the condition is matched or the number of retries (5 retries, 5seconds apart) have finished.
The condition will be
if(personFutures.get().size() != totalPersonsInOrg){
retry(personService.getPersons(), 5, 5)
} else {
return persons
}
I want to use the thenApply and thenCompose to chain these after the first completablefuture.
personFutures.thenApply(persons -> {
if(persons.size() != totalPersonsOrg){
retry(personservice,5,5)
}
})
This is what needs to be changed
private boolean allPersonsFound(String id, int retry, int c
count)
{
if (retry > maxRetries) {
return false;
}
CompletableFuture<List<Persons>> personsFuture = personaService.getPersons();
List<Persons> persons = personsFuture.get();
if (persons.size() != count) {
//add delay of 50ms
return allPersonsFound(id, retry++, count);
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的
PersonsService
是:您可能想拥有具有其他验证和重试逻辑的代理实现。
一种可能性是使用异步递归。这样的事情(我没有尝试运行它!):
Assuming your
PersonsService
is:You probably want to have a proxy implementation with additional validation and retry logic.
One possibility is to use asynchronous recursion. Something like this (I have not tried to run it!):