我想创建一个可以运行方法的类,直到满足返回值的条件.
看起来应该是这样的
methodPoller.poll(pollDurationSec, pollIntervalMillis)
.method(dog.bark())
.until(dog -> dog.bark().equals("Woof"))
.execute();
Run Code Online (Sandbox Code Playgroud)
我的方法poller看起来有点像这个()//跟随GuiSim回答
public class MethodPoller {
Duration pollDurationSec;
int pollIntervalMillis;
public MethodPoller() {
}
public MethodPoller poll(Duration pollDurationSec, int pollIntervalMillis) {
this.pollDurationSec = pollDurationSec;
this.pollIntervalMillis = pollIntervalMillis;
return this;
}
public <T> MethodPoller method(Supplier<T> supplier) {
return this;
}
public <T> MethodPoller until(Predicate<T> predicate) {
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
但是我很难从这里开始.
在满足条件之前,如何实现对常规方法的重试?
谢谢.
我想运行一个使用RXJava重试的方法
return Observable
.just(myObj)
.flatMap(doc ->
myFunc(myObj, ....)
)
.doOnError(e -> log.Error())
.onErrorResumeNext(myObj2 ->
methodIWantToRunWithRetries(...)
.onErrorResumeNext(myObj3 ->
methodIWantToRunWithRetries(...)
)
);
}
Run Code Online (Sandbox Code Playgroud)
如果我使用,onErrorResumeNext我需要多次嵌套它我想重试.
(除非我想用try/catch包围它)
有没有选择用RXJava方法实现它?