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:
Dave Syer
2018-03-26 10:06:13 +01:00
parent a1b624b28a
commit 5aeba1ea96
13 changed files with 144 additions and 76 deletions

View File

@@ -29,6 +29,7 @@ import java.util.jar.Manifest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -49,7 +50,7 @@ public class SpringFunctionInitializer implements Closeable {
private final Class<?> configurationClass;
private Function<Flux<?>, Flux<?>> function;
private Function<Flux<?>, Publisher<?>> function;
private Consumer<Flux<?>> consumer;
@@ -104,16 +105,20 @@ public class SpringFunctionInitializer implements Closeable {
if (defaultName) {
name = "consumer";
}
this.consumer = this.catalog.lookup(Consumer.class, name);
if (this.consumer == null) {
if (defaultName) {
name = "supplier";
this.function = this.catalog.lookup(Function.class, name);
if (this.function == null) {
this.consumer = this.catalog.lookup(Consumer.class, name);
if (this.consumer == null) {
if (defaultName) {
name = "supplier";
}
this.supplier = this.catalog.lookup(Supplier.class, name);
}
this.supplier = this.catalog.lookup(Supplier.class, name);
}
}
}
this.context = context;
}
private SpringApplicationBuilder springApplication() {
@@ -143,7 +148,7 @@ public class SpringFunctionInitializer implements Closeable {
protected Flux<?> apply(Flux<?> input) {
if (this.function != null) {
return function.apply(input);
return Flux.from(function.apply(input));
}
if (this.consumer != null) {
this.consumer.accept(input);