Fixed consumer sample to accept bare string
This commit is contained in:
@@ -156,7 +156,7 @@ curl -H "Accept: application/json" localhost:9001/words
|
|||||||
=== Register a Consumer:
|
=== Register a Consumer:
|
||||||
|
|
||||||
----
|
----
|
||||||
./registerConsumer.sh -n print -f "f->f.subscribe(System.out::println)"
|
./registerConsumer.sh -n print -t String -f "System.out::println"
|
||||||
----
|
----
|
||||||
|
|
||||||
=== Run a REST Microservice using that Consumer:
|
=== Run a REST Microservice using that Consumer:
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public class ConsumerCompiler<T> extends AbstractFunctionCompiler<Consumer<T>> {
|
|||||||
private final String inputType;
|
private final String inputType;
|
||||||
|
|
||||||
public ConsumerCompiler() {
|
public ConsumerCompiler() {
|
||||||
this("Object");
|
this("Flux<Object>");
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConsumerCompiler(String inputType) {
|
public ConsumerCompiler(String inputType) {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
public class CompilationFailedException extends RuntimeException {
|
public class CompilationFailedException extends RuntimeException {
|
||||||
|
|
||||||
public CompilationFailedException(List<CompilationMessage> messages) {
|
public CompilationFailedException(List<CompilationMessage> messages) {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.InitializingBean;
|
|||||||
import org.springframework.cloud.function.compiler.CompilationResultFactory;
|
import org.springframework.cloud.function.compiler.CompilationResultFactory;
|
||||||
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
||||||
import org.springframework.cloud.function.compiler.java.SimpleClassLoader;
|
import org.springframework.cloud.function.compiler.java.SimpleClassLoader;
|
||||||
|
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
@@ -30,7 +31,7 @@ import org.springframework.util.ReflectionUtils;
|
|||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean {
|
public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean, FunctionFactoryMetadata {
|
||||||
|
|
||||||
private final Resource resource;
|
private final Resource resource;
|
||||||
|
|
||||||
@@ -81,6 +82,7 @@ public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBea
|
|||||||
return this.factory.getResult();
|
return this.factory.getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Method getFactoryMethod() {
|
public Method getFactoryMethod() {
|
||||||
return this.method;
|
return this.method;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNa
|
|||||||
return this.factory.getResult();
|
return this.factory.getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Method getFactoryMethod() {
|
public Method getFactoryMethod() {
|
||||||
return this.factory.getFactoryMethod();
|
return this.factory.getFactoryMethod();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package org.springframework.cloud.function.compiler.proxy;
|
|||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import org.springframework.cloud.function.support.ConsumerProxy;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,7 +26,7 @@ import org.springframework.core.io.Resource;
|
|||||||
*
|
*
|
||||||
* @param <T> type
|
* @param <T> type
|
||||||
*/
|
*/
|
||||||
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>> implements Consumer<T> {
|
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>> implements ConsumerProxy<T> {
|
||||||
|
|
||||||
public ByteCodeLoadingConsumer(Resource resource) {
|
public ByteCodeLoadingConsumer(Resource resource) {
|
||||||
super(resource, Consumer.class);
|
super(resource, Consumer.class);
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.function.compiler.proxy;
|
|||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.cloud.function.support.FunctionProxy;
|
import org.springframework.cloud.function.support.FunctionProxy;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
@@ -28,7 +27,7 @@ import org.springframework.core.io.Resource;
|
|||||||
* @param <T> Function input type
|
* @param <T> Function input type
|
||||||
* @param <R> Function result type
|
* @param <R> Function result type
|
||||||
*/
|
*/
|
||||||
public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<Function<T, R>> implements FunctionProxy<T, R>, InitializingBean {
|
public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<Function<T, R>> implements FunctionProxy<T, R> {
|
||||||
|
|
||||||
public ByteCodeLoadingFunction(Resource resource) {
|
public ByteCodeLoadingFunction(Resource resource) {
|
||||||
super(resource, Function.class);
|
super(resource, Function.class);
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
|
|||||||
import org.springframework.cloud.function.compiler.ConsumerCompiler;
|
import org.springframework.cloud.function.compiler.ConsumerCompiler;
|
||||||
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
||||||
import org.springframework.cloud.function.compiler.SupplierCompiler;
|
import org.springframework.cloud.function.compiler.SupplierCompiler;
|
||||||
|
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||||
import org.springframework.cloud.function.support.FunctionUtils;
|
import org.springframework.cloud.function.support.FunctionUtils;
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
|
|
||||||
@@ -51,6 +52,7 @@ public class ByteCodeLoadingFunctionTests {
|
|||||||
};
|
};
|
||||||
ByteCodeLoadingConsumer<String> consumer = new ByteCodeLoadingConsumer<>(resource);
|
ByteCodeLoadingConsumer<String> consumer = new ByteCodeLoadingConsumer<>(resource);
|
||||||
consumer.afterPropertiesSet();
|
consumer.afterPropertiesSet();
|
||||||
|
assertThat(consumer instanceof FunctionFactoryMetadata);
|
||||||
assertThat(FunctionUtils.isFluxConsumer(consumer.getFactoryMethod())).isFalse();
|
assertThat(FunctionUtils.isFluxConsumer(consumer.getFactoryMethod())).isFalse();
|
||||||
consumer.accept("foo");
|
consumer.accept("foo");
|
||||||
}
|
}
|
||||||
@@ -67,6 +69,7 @@ public class ByteCodeLoadingFunctionTests {
|
|||||||
};
|
};
|
||||||
ByteCodeLoadingSupplier<String> supplier = new ByteCodeLoadingSupplier<>(resource);
|
ByteCodeLoadingSupplier<String> supplier = new ByteCodeLoadingSupplier<>(resource);
|
||||||
supplier.afterPropertiesSet();
|
supplier.afterPropertiesSet();
|
||||||
|
assertThat(supplier instanceof FunctionFactoryMetadata);
|
||||||
assertThat(FunctionUtils.isFluxSupplier(supplier.getFactoryMethod())).isFalse();
|
assertThat(FunctionUtils.isFluxSupplier(supplier.getFactoryMethod())).isFalse();
|
||||||
assertThat(supplier.get()).isEqualTo("foo");
|
assertThat(supplier.get()).isEqualTo("foo");
|
||||||
}
|
}
|
||||||
@@ -83,6 +86,7 @@ public class ByteCodeLoadingFunctionTests {
|
|||||||
};
|
};
|
||||||
ByteCodeLoadingFunction<String, String> function = new ByteCodeLoadingFunction<>(resource);
|
ByteCodeLoadingFunction<String, String> function = new ByteCodeLoadingFunction<>(resource);
|
||||||
function.afterPropertiesSet();
|
function.afterPropertiesSet();
|
||||||
|
assertThat(function instanceof FunctionFactoryMetadata);
|
||||||
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isFalse();
|
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isFalse();
|
||||||
assertThat(function.apply("foo")).isEqualTo("FOO");
|
assertThat(function.apply("foo")).isEqualTo("FOO");
|
||||||
}
|
}
|
||||||
@@ -99,6 +103,7 @@ public class ByteCodeLoadingFunctionTests {
|
|||||||
};
|
};
|
||||||
ByteCodeLoadingFunction<Flux<String>, Flux<String>> function = new ByteCodeLoadingFunction<>(resource);
|
ByteCodeLoadingFunction<Flux<String>, Flux<String>> function = new ByteCodeLoadingFunction<>(resource);
|
||||||
function.afterPropertiesSet();
|
function.afterPropertiesSet();
|
||||||
|
assertThat(function instanceof FunctionFactoryMetadata);
|
||||||
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isTrue();
|
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isTrue();
|
||||||
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
|
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
|
||||||
}
|
}
|
||||||
@@ -54,12 +54,14 @@ public class ContextFunctionCatalogAutoConfigurationTests {
|
|||||||
private ConfigurableApplicationContext context;
|
private ConfigurableApplicationContext context;
|
||||||
private InMemoryFunctionCatalog catalog;
|
private InMemoryFunctionCatalog catalog;
|
||||||
private FunctionInspector inspector;
|
private FunctionInspector inspector;
|
||||||
|
private static String value;
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void close() {
|
public void close() {
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
context.close();
|
context.close();
|
||||||
}
|
}
|
||||||
|
ContextFunctionCatalogAutoConfigurationTests.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -149,6 +151,17 @@ public class ContextFunctionCatalogAutoConfigurationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void compiledFunction() throws Exception {
|
public void compiledFunction() throws Exception {
|
||||||
|
create(EmptyConfiguration.class,
|
||||||
|
"spring.cloud.function.compile.foos.lambda=v -> v.toUpperCase()",
|
||||||
|
"spring.cloud.function.compile.foos.inputType=String",
|
||||||
|
"spring.cloud.function.compile.foos.outputType=String");
|
||||||
|
assertThat(context.getBean("foos")).isInstanceOf(Function.class);
|
||||||
|
assertThat(catalog.lookupFunction("foos")).isInstanceOf(Function.class);
|
||||||
|
assertThat(inspector.getInputWrapper("foos")).isEqualTo(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void byteCodeFunction() throws Exception {
|
||||||
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
|
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
|
||||||
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String",
|
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String",
|
||||||
"String");
|
"String");
|
||||||
@@ -161,6 +174,33 @@ public class ContextFunctionCatalogAutoConfigurationTests {
|
|||||||
assertThat(inspector.getInputWrapper("foos")).isEqualTo(String.class);
|
assertThat(inspector.getInputWrapper("foos")).isEqualTo(String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void compiledConsumer() throws Exception {
|
||||||
|
create(EmptyConfiguration.class,
|
||||||
|
"spring.cloud.function.compile.foos.lambda=" + getClass().getName() + "::set",
|
||||||
|
"spring.cloud.function.compile.foos.type=consumer",
|
||||||
|
"spring.cloud.function.compile.foos.inputType=String");
|
||||||
|
assertThat(catalog.lookupConsumer("foos")).isInstanceOf(Consumer.class);
|
||||||
|
assertThat(inspector.getInputWrapper("foos")).isEqualTo(String.class);
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Consumer<String> consumer = (Consumer<String>)context.getBean("foos");
|
||||||
|
consumer.accept("hello");
|
||||||
|
assertThat(ContextFunctionCatalogAutoConfigurationTests.value).isEqualTo("hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void compiledFluxConsumer() throws Exception {
|
||||||
|
create(EmptyConfiguration.class,
|
||||||
|
"spring.cloud.function.compile.foos.lambda=f -> f.subscribe(" + getClass().getName() + "::set)",
|
||||||
|
"spring.cloud.function.compile.foos.type=consumer");
|
||||||
|
assertThat(catalog.lookupConsumer("foos")).isInstanceOf(Consumer.class);
|
||||||
|
assertThat(inspector.getInputWrapper("foos")).isEqualTo(Flux.class);
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Consumer<Flux<String>> consumer = (Consumer<Flux<String>>)context.getBean("foos");
|
||||||
|
consumer.accept(Flux.just("hello"));
|
||||||
|
assertThat(ContextFunctionCatalogAutoConfigurationTests.value).isEqualTo("hello");
|
||||||
|
}
|
||||||
|
|
||||||
private void create(Class<?> type, String... props) {
|
private void create(Class<?> type, String... props) {
|
||||||
create(new Class<?>[] { type }, props);
|
create(new Class<?>[] { type }, props);
|
||||||
}
|
}
|
||||||
@@ -170,6 +210,10 @@ public class ContextFunctionCatalogAutoConfigurationTests {
|
|||||||
catalog = context.getBean(InMemoryFunctionCatalog.class);
|
catalog = context.getBean(InMemoryFunctionCatalog.class);
|
||||||
inspector = context.getBean(FunctionInspector.class);
|
inspector = context.getBean(FunctionInspector.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void set(Object value) {
|
||||||
|
ContextFunctionCatalogAutoConfigurationTests.value = value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|||||||
Reference in New Issue
Block a user