Tweak logic in stream invokers

* If there is only one function, use it unconditionally
* If no functions match, ensure that null is not the selector
* If a conversion succeeds, check the type of the result
This commit is contained in:
Dave Syer
2017-06-29 14:29:01 +01:00
parent cf707fd872
commit 2dd7dfb900
4 changed files with 197 additions and 11 deletions

View File

@@ -48,6 +48,8 @@ public class StreamListeningConsumerInvoker implements SmartInitializingSingleto
private final String[] names;
private static final String NOENDPOINT = "__NOENDPOINT__";
public StreamListeningConsumerInvoker(FunctionCatalog functionCatalog,
FunctionInspector functionInspector,
CompositeMessageConverterFactory converterFactory, String defaultEndpoint,
@@ -79,14 +81,23 @@ public class StreamListeningConsumerInvoker implements SmartInitializingSingleto
private String select(Message<?> input) {
String name = defaultEndpoint;
if (name == null) {
for (String candidate : names) {
Class<?> inputType = functionInspector.getInputType(candidate);
if (this.converter.fromMessage(input, inputType) != null) {
name = candidate;
break;
if (names.length == 1) {
name = names[0];
}
else {
for (String candidate : names) {
Class<?> inputType = functionInspector.getInputType(candidate);
Object value = this.converter.fromMessage(input, inputType);
if (value != null && inputType.isInstance(value)) {
name = candidate;
break;
}
}
}
}
if (name == null) {
return NOENDPOINT;
}
return name;
}

View File

@@ -50,6 +50,8 @@ public class StreamListeningFunctionInvoker implements SmartInitializingSingleto
private final String[] names;
private static final String NOENDPOINT = "__NOENDPOINT__";
public StreamListeningFunctionInvoker(FunctionCatalog functionCatalog,
FunctionInspector functionInspector,
CompositeMessageConverterFactory converterFactory, String defaultEndpoint,
@@ -82,14 +84,23 @@ public class StreamListeningFunctionInvoker implements SmartInitializingSingleto
private String select(Message<?> input) {
String name = defaultEndpoint;
if (name == null) {
for (String candidate : names) {
Class<?> inputType = functionInspector.getInputType(candidate);
if (this.converter.fromMessage(input, inputType) != null) {
name = candidate;
break;
if (names.length == 1) {
name = names[0];
}
else {
for (String candidate : names) {
Class<?> inputType = functionInspector.getInputType(candidate);
Object value = this.converter.fromMessage(input, inputType);
if (value != null && inputType.isInstance(value)) {
name = candidate;
break;
}
}
}
}
if (name == null) {
return NOENDPOINT;
}
return name;
}