GH-1773 Added support for native spring.cloud.function.definition property

Resolves #1773
This commit is contained in:
Oleg Zhurakousky
2019-07-30 14:41:27 +02:00
parent 2ddd7973c4
commit e08f669a54
3 changed files with 36 additions and 2 deletions

View File

@@ -591,7 +591,7 @@ Since Spring Cloud Stream v2.1, another alternative for defining _stream handler
support for https://cloud.spring.io/spring-cloud-function/[Spring Cloud Function] where they can be expressed as beans of
type `java.util.function.[Supplier/Function/Consumer]`.
To specify which functional bean to bind to the external destination(s) exposed by the bindings, you must provide `spring.cloud.stream.function.definition` property.
To specify which functional bean to bind to the external destination(s) exposed by the bindings, you must provide `spring.cloud.stream.function.definition` or native to spring-cloud-function `spring.cloud.function.definition` property.
Here is the example of the Processor application exposing message handler as `java.util.function.Function`
[source,java]
@@ -601,7 +601,7 @@ Here is the example of the Processor application exposing message handler as `ja
public class MyFunctionBootApp {
public static void main(String[] args) {
SpringApplication.run(MyFunctionBootApp.class, "--spring.cloud.stream.function.definition=toUpperCase");
SpringApplication.run(MyFunctionBootApp.class, "--spring.cloud.function.definition=toUpperCase");
}
@Bean

View File

@@ -288,6 +288,9 @@ public class BinderFactoryAutoConfiguration {
private String determineFunctionName(FunctionCatalog catalog, Environment environment) {
String name = environment.getProperty("spring.cloud.stream.function.definition");
if (!StringUtils.hasText(name)) {
name = environment.getProperty("spring.cloud.function.definition");
}
if (!StringUtils.hasText(name) && Boolean.parseBoolean(
environment.getProperty("spring.cloud.function.routing.enabled", "false"))) {
name = RoutingFunction.FUNCTION_NAME;

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.stream.function;
import java.util.function.Consumer;
import java.util.function.Function;
import org.junit.After;
import org.junit.Test;
import reactor.core.publisher.Flux;
@@ -46,6 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class ImplicitFunctionBindingTests {
@After
public void after() {
System.clearProperty("spring.cloud.stream.function.definition");
System.clearProperty("spring.cloud.function.definition");
}
@Test
public void testBindingWithNoEnableBindingConfiguration() {
@@ -70,6 +77,30 @@ public class ImplicitFunctionBindingTests {
}
}
@Test
public void testBindingWithNoEnableBindingConfigurationWithFunctionNativeDefinitionProperty() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestChannelBinderConfiguration.getCompleteConfiguration(
NoEnableBindingConfiguration.class))
.web(WebApplicationType.NONE)
.run("--spring.jmx.enabled=false",
"--spring.cloud.function.definition=func")) {
InputDestination inputDestination = context.getBean(InputDestination.class);
OutputDestination outputDestination = context
.getBean(OutputDestination.class);
Message<byte[]> inputMessage = MessageBuilder
.withPayload("Hello".getBytes()).build();
inputDestination.send(inputMessage);
Message<byte[]> outputMessage = outputDestination.receive();
assertThat(outputMessage.getPayload()).isEqualTo("Hello".getBytes());
}
}
@Test
public void testBindingWithEnableBindingConfiguration() {