相关疑难解决方法(0)

Java 8 - 重试方法,直到满足条件(间隔)

我想创建一个可以运行方法的类,直到满足返回值的条件.

看起来应该是这样的

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)

但是我很难从这里开始.
在满足条件之前,如何实现对常规方法的重试?
谢谢.

java hamcrest java-8

16
推荐指数
3
解决办法
1万
查看次数

在RXjava中应用重试

我想运行一个使用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方法实现它?

java java-8 rx-java

4
推荐指数
1
解决办法
1266
查看次数

标签 统计

java ×2

java-8 ×2

hamcrest ×1

rx-java ×1