我有此代码:
int finalAttempts = attempts;
Certificate certificate = Observable.range(1, attempts)
.delay(3, TimeUnit.SECONDS)
.map(integer -> {
try {
order.update();
if(order.getStatus() != Status.VALID) {
if(integer == finalAttempts) {
Exceptions.propagate(new AcmeException("Order failed... Giving up."));
}
} else if(order.getStatus() == Status.VALID) {
Certificate cert = order.getCertificate();
return cert;
}
} catch (AcmeException e) {
Exceptions.propagate(e);
}
return null; // return only if this is TRUE: order.getStatus() == Status.VALID
}).toBlocking().first();
我想知道在Observable
仍然不正确的情况下阻止此order.getStatus() == Status.VALID
完全返回的最佳方法。同时,如果所有尝试或尝试都已被使用,并且状态仍然为true,则应引发异常。
答案 0 :(得分:2)
在这种情况下,filter()运算符可能是您的朋友。我想到这样的事情:
int finalAttempts = attempts;
Certificate certificate = Observable.range(1, attempts)
.delay(3, TimeUnit.SECONDS)
.filter(integer -> {
order.update();
return order.getStatus() == Status.VALID;
})
.map(integer -> {
// do your stuff
}).toBlocking().first();