Fix function eligibility filtering

This commit is contained in:
Oleg Zhurakousky
2022-06-16 18:27:23 +02:00
parent 6ce65e42d4
commit 10244a044b
3 changed files with 25 additions and 8 deletions

View File

@@ -121,6 +121,9 @@ public class BeanFactoryAwareFunctionRegistry extends SimpleFunctionRegistry imp
+ "use 'spring.cloud.function.definition' property to explicitly define it."); + "use 'spring.cloud.function.definition' property to explicitly define it.");
return null; return null;
} }
if (!isFunctionDefinitionEligible(functionDefinition)) {
return null;
}
FunctionInvocationWrapper function = this.doLookup(type, functionDefinition, expectedOutputMimeTypes); FunctionInvocationWrapper function = this.doLookup(type, functionDefinition, expectedOutputMimeTypes);
Object syncInstance = functionDefinition == null ? this : functionDefinition; Object syncInstance = functionDefinition == null ? this : functionDefinition;
synchronized (syncInstance) { synchronized (syncInstance) {
@@ -139,6 +142,9 @@ public class BeanFactoryAwareFunctionRegistry extends SimpleFunctionRegistry imp
if (functionCandidate instanceof FunctionRegistration) { if (functionCandidate instanceof FunctionRegistration) {
functionRegistration = (FunctionRegistration) functionCandidate; functionRegistration = (FunctionRegistration) functionCandidate;
} }
else if (functionCandidate instanceof BiFunction || functionCandidate instanceof BiConsumer) {
functionRegistration = this.registerMessagingBiFunction(functionCandidate, functionName);
}
else if (this.isFunctionPojo(functionCandidate, functionName)) { else if (this.isFunctionPojo(functionCandidate, functionName)) {
Method functionalMethod = FunctionTypeUtils.discoverFunctionalMethod(functionCandidate.getClass()); Method functionalMethod = FunctionTypeUtils.discoverFunctionalMethod(functionCandidate.getClass());
functionCandidate = this.proxyTarget(functionCandidate, functionalMethod); functionCandidate = this.proxyTarget(functionCandidate, functionalMethod);
@@ -148,9 +154,6 @@ public class BeanFactoryAwareFunctionRegistry extends SimpleFunctionRegistry imp
functionRegistration = this.applicationContext functionRegistration = this.applicationContext
.getBean(functionName + FunctionRegistration.REGISTRATION_NAME_SUFFIX, FunctionRegistration.class); .getBean(functionName + FunctionRegistration.REGISTRATION_NAME_SUFFIX, FunctionRegistration.class);
} }
else if (functionCandidate instanceof BiFunction || functionCandidate instanceof BiConsumer) {
functionRegistration = this.registerMessagingBiFunction(functionCandidate, functionName);
}
else { else {
functionType = FunctionTypeUtils.discoverFunctionType(functionCandidate, functionName, this.applicationContext); functionType = FunctionTypeUtils.discoverFunctionType(functionCandidate, functionName, this.applicationContext);
} }

View File

@@ -188,6 +188,17 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
return true; return true;
} }
boolean isFunctionDefinitionEligible(String functionDefinition) {
if (this.functionProperties != null) {
for (String definition : this.functionProperties.getIneligibleDefinitions()) {
if (functionDefinition.contains(definition)) {
return false;
}
}
}
return true;
}
//----- //-----
@Override @Override
@@ -217,8 +228,10 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
function = this.compose(type, functionDefinition); function = this.compose(type, functionDefinition);
} }
if (function != null && !ObjectUtils.isEmpty(expectedOutputMimeTypes)) { if (function != null) {
function.expectedOutputContentType = expectedOutputMimeTypes; if (!ObjectUtils.isEmpty(expectedOutputMimeTypes)) {
function.expectedOutputContentType = expectedOutputMimeTypes;
}
} }
else if (logger.isDebugEnabled()) { else if (logger.isDebugEnabled()) {
logger.debug("Function '" + functionDefinition + "' is not found in cache"); logger.debug("Function '" + functionDefinition + "' is not found in cache");
@@ -243,8 +256,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
? functionDefinition.replaceAll(",", "|") ? functionDefinition.replaceAll(",", "|")
: System.getProperty(FunctionProperties.FUNCTION_DEFINITION, ""); : System.getProperty(FunctionProperties.FUNCTION_DEFINITION, "");
if (!this.getNames(null).contains(functionDefinition)) { Set<String> names = this.getNames(null);
List<String> eligibleFunction = this.getNames(null).stream() if (!names.contains(functionDefinition)) {
List<String> eligibleFunction = names.stream()
.filter(name -> !RoutingFunction.FUNCTION_NAME.equals(name)) .filter(name -> !RoutingFunction.FUNCTION_NAME.equals(name))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (eligibleFunction.size() == 1 if (eligibleFunction.size() == 1

View File

@@ -114,7 +114,7 @@ public class BeanFactoryAwareFunctionRegistryTests {
for (String beanName : context.getBeanDefinitionNames()) { for (String beanName : context.getBeanDefinitionNames()) {
try { try {
FunctionInvocationWrapper function = catalog.lookup(beanName); FunctionInvocationWrapper function = catalog.lookup(beanName);
if (function != null) { if (function != null && function.getFunctionDefinition().equals(beanName)) {
registeredFunction.add(function); registeredFunction.add(function);
} }
} }