Reactor compose vs flatMap
Tag : java , By : desmiserables
Date : March 29 2020, 07:55 AM
around this issue An excellent explanation by Dan Lew: The difference is that compose() is a higher level abstraction: it operates on the entire stream, not individually emitted items. In more specific terms:
|
map vs flatMap in reactor
Tag : java , By : Sebastián Ucedo
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further map is for synchronous, non-blocking, 1-to-1 transformations flatMap is for asynchronous (non-blocking) 1-to-N transformations The difference is visible in the method signature:
|
How to use Context with flatMap() in Reactor?
Date : March 29 2020, 07:55 AM
will help you Chain your Publishers and may the Context be with you In the case, you connected all your Publishers (and this includes connections within the flatMap/concatMap and similar operators) you will have Context correctly propagated among the whole stream runtime. public class TestFlatMap {
public static void main(final String ...args) {
final Flux<String> greetings = Flux.just("Hubert", "Sharon")
.flatMap(TestFlatMap::nameToGreeting)
.subscriberContext(context ->
Context.of("greetingWord", "Hello") // context initialized
);
greetings.subscribe(System.out::println);
}
private static Mono<String> nameToGreeting(final String name) {
return Mono.subscriberContext()
.filter(c -> c.hasKey("greetingWord"))
.map(c -> c.get("greetingWord"))
.flatMap(greetingWord -> Mono.just(greetingWord + " " + name + " " + "!!!"));// ALERT: we have Context here !!!
}
}
public class TestFlatMap {
public static void main(final String ...args) {
final Flux<String> greetings = Flux.just("Hubert", "Sharon")
.flatMap(TestFlatMap::nameToGreeting)
.subscriberContext(context ->
Context.of("greetingWord", "Hello") // context initialized
);
greetings.subscribe(System.out::println);
}
private static Mono<String> nameToGreeting(final String name) {
return Mono.zip(
Mono.subscriberContext()
.filter(c -> c.hasKey("greetingWord"))
.map(c -> c.get("greetingWord")), // ALERT: we have Context here !!!
Mono.just(name),
(greetingWord, receivedName) -> greetingWord + " " + receivedName + " " + "!!!"
);
}
}
|
Issue with use of project reactor's flatMap and switchIfEmpty operators
Date : March 29 2020, 07:55 AM
Does that help It's not to do with flatMap() or switchIfEmpty() directly - it's because you're trying to consume the same Mono twice: Mono<User> userMono = serverRequest.bodyToMono(User.class);
return validateUser(userMono)
.switchIfEmpty(saveUser(userMono))
.single();
|
Backpressure with flatmap in io reactor
Date : March 29 2020, 07:55 AM
hop of those help? There is an overload of flatMap in which you can set the concurrency parameter (actually more of a parallelism factor) and the prefetch parameter (which defaults to 256). The last one is the amount the inner flux is requested on the first time, so you could set it to 1.
|