如何使 重试时 重试引发异常的流



我正在尝试使用retryWhen进行非常基本的流程。 我发出 3 个 Flowable,其中一个抛出一个IOException在这种情况下,我最多会触发重试 2 次。 问题是重试时它会重新启动所有内容。导致其他可流动对象重新发出。 这是我的代码:

Flowable.just("AA", "BB", "CC")//
.flatMap(station -> getStation(station))//
.retryWhen( RetryWhen
.maxRetries(2)
.retryWhenInstanceOf(IOException.class)
.build())
.subscribe(//
station -> System.out.println("Received Availability for station=" + station),
error -> System.err.println("Failed with error=" + error.getMessage()),
() -> System.out.println("Completed!")//
);
private Flowable<String> getStation(String station)
{
if (station.equals("CC"))
{
System.err.println("Failed staton=" + station + " --> Going to retry");
return Flowable.error(new IOException("Server for station=" + station + " is down!"));
}
System.out.println("Querying for Station=" + station);
return Flowable.just(station);
}

如何调整它以仅使引发异常重试的那个?

编辑: 根据反馈,我更改了代码以在每个Flowable实例上重试:

Flowable<String> flw1 = getStationAvailability("AA");
Flowable<String> flw2 = getStationAvailability("BB");
Flowable<String> flw3 = getStationAvailability("CC");
Flowable.concat(//
flw1.retryWhen(RetryWhen.maxRetries(2).retryWhenInstanceOf(IOException.class).build()),
flw2.retryWhen(RetryWhen.maxRetries(2).retryWhenInstanceOf(IOException.class).build()),
flw3.retryWhen(RetryWhen.maxRetries(2).retryWhenInstanceOf(IOException.class).build())//
).subscribe(//
station -> System.out.println("Received Availability for station=" + station),
error -> System.err.println("Failed with error=" + error.getMessage()),// 
() -> System.out.println("Completed!")//
);

但是,发生的情况是它根本不重试。 对此有什么见解吗? 谢谢!

您需要将retryWhen()运算符放在各个可观察量的观察者链上。

Flowable.just("AA", "BB", "CC")//
.flatMap(station -> getStation(station)
.retryWhen( retryStrategy ) )//
.subscribe( ... );

这样,重新订阅只发生在一个观察者链上。

最新更新