Convert Consumer<Foo> to Function<Flux<Foo>,Mono<Void>>
This results in a better experience for users because the consumer that they write is only applied to a Flux that is subscribed to by the framework once. It gives better control over the flow of foos, e.g. if some component wants to subscribe on a thread.
This commit is contained in:
@@ -17,18 +17,21 @@
|
||||
package org.springframework.cloud.function.core;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* {@link Consumer} implementation that wraps a target Consumer so that the target's
|
||||
* simple input type will be wrapped as a {@link Flux} instance.
|
||||
* Wrapper for a {@link Consumer} implementation that converts a non-reactive consumer
|
||||
* into a reactive function.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @param <T> input type of target consumer
|
||||
*/
|
||||
public class FluxConsumer<T> implements Consumer<Flux<T>>, FluxWrapper<Consumer<T>> {
|
||||
public class FluxConsumer<T>
|
||||
implements Function<Flux<T>, Mono<Void>>, FluxWrapper<Consumer<T>> {
|
||||
|
||||
private final Consumer<T> consumer;
|
||||
|
||||
@@ -42,7 +45,7 @@ public class FluxConsumer<T> implements Consumer<Flux<T>>, FluxWrapper<Consumer<
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Flux<T> input) {
|
||||
input.subscribe(t -> consumer.accept(t));
|
||||
public Mono<Void> apply(Flux<T> input) {
|
||||
return input.doOnNext(consumer).then();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user