Примерно так:
Код: Выделить всё
public Mono test() {
Mono monoA = Mono.fromCallable(() -> {
try {
log.info("A Started");
Thread.sleep(2000);
log.info("A Ended");
return Math.random() > 0.5 ? "A" : "a";
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}).subscribeOn(Schedulers.boundedElastic());
Mono monoB = Mono.fromCallable(() -> {
try {
log.info("B Started");
Thread.sleep(8000);
log.info("B Ended");
return "B";
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}).subscribeOn(Schedulers.boundedElastic());
long startTime = System.currentTimeMillis();
return monoA.flatMap(a -> {
Mono result;
if(a.equals("A")) {
result = Mono.just(a);
} else {
result = monoB;
}
return result;
}).map(response -> {
long totalTime = System.currentTimeMillis() - startTime;
return "Returning '" + response + "' in: " + totalTime + " ms";
});
}
Код: Выделить всё
A Started
A Ended
Returning 'A' in: 2032 ms
Код: Выделить всё
A Started
A Ended
B Started
B Ended
Returning 'B' in: 10007 ms
Mono.zip(monoA, monoB)
Лучший и худший случай всегда 8000 мс
Хочу
Код: Выделить всё
if monoA = "A" response in 2000ms
if monoA = "a" response in 8000ms
Подробнее здесь: https://stackoverflow.com/questions/793 ... not-wait-f
Мобильная версия