GH-461 Add to register more than one functional class

Similar to the way we allow multiple functions to be listed with 'definition' property, this enhancement allows several functional classes to be deployed

Resolves #461
This commit is contained in:
Oleg Zhurakousky
2020-03-23 14:51:56 +01:00
parent 85000ee084
commit 9434a68bd2
5 changed files with 51 additions and 11 deletions

View File

@@ -397,6 +397,19 @@ All you need to do is specify `location` and `function-class` properties when de
--spring.cloud.function.location=target/it/simplestjar/target/simplestjar-1.0.0.RELEASE.jar
--spring.cloud.function.function-class=function.example.UpperCaseFunction
```
It's conceivable in some cases that you might want to package multiple functions together. For such scenarios you can use
`spring.cloud.function.function-class` property to list several classes delimiting them by `;`.
For example,
```
--spring.cloud.function.function-class=function.example.UpperCaseFunction;function.example.ReverseFunction
```
Here we are identifying two functions to deploy, which we can now access in function catalog by name (e.g., `catalog.lookup("reverseFunction");`).
For more details please reference the complete sample available https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-deployer/src/it/simplestjar[here].
You can also find a corresponding test in https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionDeployerTests.java#L70[FunctionDeployerTests].

View File

@@ -160,7 +160,7 @@ public class BeanFactoryAwareFunctionRegistry
if (!routing && this.declaredFunctionDefinitions.size() > 0) {
if (StringUtils.hasText(definition)) {
if (this.declaredFunctionDefinitions.size() > 1 &&!this.declaredFunctionDefinitions.contains(definition)) {
if (this.declaredFunctionDefinitions.size() > 1 && !this.declaredFunctionDefinitions.contains(definition)) {
logger.warn("Attempted to access un-declared function definition '" + definition + "'. Declared functions are '" + this.declaredFunctionDefinitions
+ "' specified via `spring.cloud.function.definition` property. If the intention is to access "
+ "any function available in FunctionCatalog, please remove `spring.cloud.function.definition` property.");

View File

@@ -0,0 +1,13 @@
package function.example;
import java.util.function.Function;
public class ReverseFunction implements Function<String, String> {
@Override
public String apply(String value) {
System.out.println("Reversing " + value);
return new StringBuilder(value).reverse().toString();
}
}

View File

@@ -93,11 +93,13 @@ class FunctionArchiveDeployer extends JarLauncher {
}
}
String functionClassName = discoverFunctionClassName(functionProperties);
if (!StringUtils.isEmpty(functionClassName)) {
FunctionRegistration registration = this.discovereAndLoadFunctionFromClassName(functionClassName);
if (registration != null) {
functionRegistry.register(registration);
String[] functionClassNames = discoverFunctionClassName(functionProperties);
for (String functionClassName : functionClassNames) {
if (!StringUtils.isEmpty(functionClassName)) {
FunctionRegistration registration = this.discovereAndLoadFunctionFromClassName(functionClassName);
if (registration != null) {
functionRegistry.register(registration);
}
}
}
}
@@ -169,11 +171,11 @@ class FunctionArchiveDeployer extends JarLauncher {
private String discoverFunctionClassName(FunctionDeployerProperties functionProperties) {
private String[] discoverFunctionClassName(FunctionDeployerProperties functionProperties) {
try {
return StringUtils.hasText(functionProperties.getFunctionClass())
? functionProperties.getFunctionClass()
: this.getArchive().getManifest().getMainAttributes().getValue("Function-Class");
? functionProperties.getFunctionClass().split(";")
: new String[] {this.getArchive().getManifest().getMainAttributes().getValue("Function-Class")};
}
catch (Exception e) {
throw new IllegalStateException("Failed to discover function class name", e);
@@ -241,7 +243,7 @@ class FunctionArchiveDeployer extends JarLauncher {
Class<?> bootAppClass = Thread.currentThread().getContextClassLoader()
.loadClass(SpringApplication.class.getName());
Method runMethod = bootAppClass.getDeclaredMethod("run", Class.class, String[].class);
Object applicationContext = runMethod.invoke(null, mainClass, (Object) args);
Object applicationContext = runMethod.invoke(null, mainClass, args);
if (logger.isInfoEnabled()) {
logger.info("Application context for archive '" + this.getArchive().getUrl() + "' is created.");
}

View File

@@ -56,7 +56,7 @@ public class FunctionDeployerTests {
public void testWithMainAndStartClassNoSpringConfiguration() throws Exception {
String[] args = new String[] {
"--spring.cloud.function.location=target/it/bootjar/target/bootjar-1.0.0.RELEASE-exec.jar",
"--spring.cloud.function.function-class=function.example.UpperCaseFunction" };
"--spring.cloud.function.function-class=function.example.UpperCaseFunction;function.example.ReverseFunction" };
ApplicationContext context = SpringApplication.run(DeployerApplication.class, args);
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
@@ -65,11 +65,23 @@ public class FunctionDeployerTests {
assertThat(function.apply("bob")).isEqualTo("BOB");
assertThat(function.apply("stacy")).isEqualTo("STACY");
function = catalog.lookup("reverseFunction");
assertThat(function.apply("bob")).isEqualTo("bob");
assertThat(function.apply("stacy")).isEqualTo("ycats");
Function<Flux<String>, Flux<String>> functionAsFlux = catalog.lookup("upperCaseFunction");
List<String> results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block();
assertThat(results.get(0)).isEqualTo("BOB");
assertThat(results.get(1)).isEqualTo("STACY");
functionAsFlux = catalog.lookup("reverseFunction");
results = functionAsFlux.apply(Flux.just("bob", "stacy")).collectList().block();
assertThat(results.get(0)).isEqualTo("bob");
assertThat(results.get(1)).isEqualTo("ycats");
}
@Test