【发布时间】:2019-01-28 09:43:13
【问题描述】:
我正在尝试为我的工作实现反应堆核心。我被困在发生错误时我们需要执行的重试。以下是添加任何错误之前的示例代码
FluxSink<String> mainSink;
// Create the fulx and get handle to Sink
Flux<String> mainFlux = Flux.create(sink -> {
mainSink = sink;
}, FluxSink.OverflowStrategy.BUFFER);
// Convert to Hot Flux
ConnectableFlux<String> hotFlux = mainFlux.publish();
// Two operations, add A and B to the input
hotFlux.flatMap(o -> Mono.just(o).map(s -> Mono.just(o + "A")))
.flatMap(o -> Mono.just(o).map(s -> Mono.just(o + "B")))
.log()
.subscribe();
// Activate
hotFlux.connect();
// Publish messages to test
Thread.sleep(5000);
int pendingItems = 25;
while(pendingItems > 0) {
System.out.println("Publishing " + pendingItems + " item");
mainSink.next(String.valueOf(pendingItems));
System.out.println("Published " + pendingItems + " item");
pendingItems--;
}
当我这样做时。它工作正常。
来到错误案例,假设第二个操作(附加“A”)对某个项目失败。我正在尝试获得以下行为。
- 我尝试添加“A”的部分必须重试 3 次才能放弃
- 另外我想让整个 Flux 在放弃之前重试 5 次
想知道我怎样才能做到这一点。
AtomicInteger count = new AtomicInteger(0);
FluxSink<String> mainSink;
// Create the fulx and get handle to Sink
Flux<String> mainFlux = Flux.create(sink -> {
mainSink = sink;
}, FluxSink.OverflowStrategy.BUFFER);
// Convert to Hot Flux
ConnectableFlux<String> hotFlux = mainFlux.publish();
// Two operations, add A and B to the input
hotFlux.flatMap(o -> Mono.just(o).map(s -> {
System.out.println("Processing for adding A : " + o);
if(count.incrementAndGet() >= 25) {
throw new RuntimeException("More than 25th item.. Boom.. !!!");
} else {
return Mono.just(o + "A")));
}
}).retry(5)
.doOnError(throwable -> System.out.println("**** Inner Error"))
).flatMap(o -> Mono.just(o).map(s -> Mono.just(o + "B")))
.log()
.subscribe();
// Activate
hotFlux.connect();
// Publish messages to test
Thread.sleep(5000);
int pendingItems = 25;
while(pendingItems > 0) {
System.out.println("Publishing " + pendingItems + " item");
mainSink.next(String.valueOf(pendingItems));
System.out.println("Published " + pendingItems + " item");
pendingItems--;
}
当我如上所示在第一个 flatMap 中添加 retry(5) 时,它可以正常工作,它会为第 25 个进来的人重试 A 的附加 5 次 - 从日志中可以明显看出
我无法实现完整的助焊剂重试(上述要求中的第 (2) 点)。我尝试在第二个通量之后添加一个 .retry(3) ,认为它会重试整个通量。但它似乎没有重试。有人可以帮忙吗?
AtomicInteger count = new AtomicInteger(0);
FluxSink<String> mainSink;
// Create the fulx and get handle to Sink
Flux<String> mainFlux = Flux.create(sink -> {
mainSink = sink;
}, FluxSink.OverflowStrategy.BUFFER);
// Convert to Hot Flux
ConnectableFlux<String> hotFlux = mainFlux.publish();
// Two operations, add A and B to the input
hotFlux.flatMap(o -> Mono.just(o).map(s -> {
System.out.println("Processing for adding A : " + o);
if(count.incrementAndGet() >= 25) {
throw new RuntimeException("More than 25th item.. Boom.. !!!");
} else {
return Mono.just(o + "A")));
}
}).retry(5)
.doOnError(throwable -> System.out.println("**** Inner Error"))
).flatMap(o -> Mono.just(o).map(s -> Mono.just(o + "B")))
.retry(3)
.log()
.subscribe();
// Activate
hotFlux.connect();
// Publish messages to test
Thread.sleep(5000);
int pendingItems = 25;
while(pendingItems > 0) {
System.out.println("Publishing " + pendingItems + " item");
mainSink.next(String.valueOf(pendingItems));
System.out.println("Published " + pendingItems + " item");
pendingItems--;
}
【问题讨论】:
标签: reactive-programming project-reactor