Код: Выделить всё
List list = List.of(1, 2, 3, 4, 5, 0, 7, 8, 9, 10);
CompletionStage future = ReactiveStreams.fromIterable(list)
.map(i -> 100 / i)
.collect(Collectors.summingInt(i -> i))
.run();
future.whenComplete((res, err) -> {
if (err != null) {
System.out.println("error " + err);
} else
System.out.println("res: " + res);
});
Код: Выделить всё
error java.lang.ArithmeticException: / by zero
Код: Выделить всё
Multi.createFrom().items(1, 2, 3, 4, 5, 0, 7, 8, 9, 10)
.onRequest().invoke(req -> System.out.println("Got a request: " + req))
.onItem().transform(i -> 100/i)
.subscribe().withSubscriber(new Flow.Subscriber() {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription s) {
this.subscription = s;
s.request(1);
}
@Override
public void onNext(Integer item) {
System.out.println("Got item " + item);
subscription.request(1);
}
@Override
public void onError(Throwable t) {
System.out.println("error " + t);
}
@Override
public void onComplete() {
System.out.println("finish");
}
}
);
Код: Выделить всё
Got a request: 1 //1
Got item 100
Got a request: 1 //2
Got item 50
Got a request: 1 //3
Got item 33
Got a request: 1 //4
Got item 25
Got a request: 1 //5
Got item 20 // 100/5 =20
Got a request: 1 //0! and the stop
error java.lang.ArithmeticException: / by zero
Я попробую использовать
. onFailure().recoverWithItem(0), но после этого он завершается и останавливается вместо продолжения!
Код: Выделить всё
Got a request: 1
Got item 100
Got a request: 1
Got item 50
Got a request: 1
Got item 33
Got a request: 1
Got item 25
Got a request: 1
Got item 20
Got a request: 1
Got item 0
finish
Код: Выделить всё
Multi.createFrom().items(1, 2, 3, 4, 5, 0, 7, 8, 9, 10)
.onRequest().invoke(req -> System.out.println("Got a request: " + req))
.onItem().transform(i -> 100/i)
.onFailure().recoverWithItem(0)
.subscribe().withSubscriber(new Flow.Subscriber() {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription s) {
this.subscription = s;
s.request(1);
}
@Override
public void onNext(Integer item) {
System.out.println("Got item " + item);
subscription.request(1);
}
@Override
public void onError(Throwable t) {
System.out.println("error " + t);
}
@Override
public void onComplete() {
System.out.println("finish");
}
}
);
Роби
Подробнее здесь: https://stackoverflow.com/questions/789 ... rrors-arri