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:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.");
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user