GH-654 Add more tests, cleanup and initial javadoc

This commit is contained in:
Oleg Zhurakousky
2021-02-24 08:49:53 +01:00
parent a838f31be6
commit 410d494467
5 changed files with 175 additions and 68 deletions

View File

@@ -16,14 +16,29 @@
package org.springframework.cloud.function.context;
import org.springframework.cloud.function.context.config.RoutingFunction;
import org.springframework.messaging.Message;
/**
* Java-based strategy to assist with determining the name of the route-to function definition.
* Once implementation is registered as a bean in application context
* it will be picked up by a {@link RoutingFunction} and used to determine the name of the
* route-to function definition.
*
* While {@link RoutingFunction} provides several mechanisms to determine the route-to function definition
* this callback takes precedence over all of them.
*
* @author Oleg Zhurakousky
* @since 3.1
*/
public interface MessageRoutingCallback {
String route(Message<?> message, FunctionProperties functionProperties);
/**
* Determines the name of the function definition to route incoming {@link Message}.
*
* @param message instance of incoming {@link Message}
* @param functionProperties instance of {@link FunctionProperties}
* @return the name of the route-to function definition
*/
String functionDefinition(Message<?> message, FunctionProperties functionProperties);
}

View File

@@ -86,19 +86,23 @@ public class RoutingFunction implements Function<Object, Object> {
/*
* - Check if spring.cloud.function.definition is set in header and if it is use it.
* - Check if `this.routingCallback` is present and if it is use it (only for Message input)
* If NOT
* - Check spring.cloud.function.routing-expression and if it is set use it
* - Check if spring.cloud.function.definition is set in header and if it is use it.(only for Message input)
* If NOT
* - Check spring.cloud.function.definition is set in FunctionProperties and if it is use it
* - Check if spring.cloud.function.routing-expression is set in header and if it is set use it (only for Message input)
* If NOT
* - Check `spring.cloud.function.definition` is set in FunctionProperties and if it is use it (Message and Publisher)
* If NOT
* - Check `spring.cloud.function.routing-expression` is set in FunctionProperties and if it is use it (Message and Publisher)
* If NOT
* - Fail
*/
private Object route(Object input, boolean originalInputIsPublisher) {
FunctionInvocationWrapper function = null;
if (input instanceof Message) {
Message<?> message = (Message<?>) input;
if (this.routingCallback != null) {
function = this.functionFromCallback(message);
}
@@ -129,17 +133,13 @@ public class RoutingFunction implements Function<Object, Object> {
}
}
else if (input instanceof Publisher) {
if (this.routingCallback != null) {
function = this.functionFromCallback(input);
}
if (function == null) {
if (StringUtils.hasText(functionProperties.getRoutingExpression())) {
function = this.functionFromExpression(functionProperties.getRoutingExpression(), input);
}
else
if (StringUtils.hasText(functionProperties.getDefinition())) {
function = functionFromDefinition(functionProperties.getDefinition());
}
else if (StringUtils.hasText(functionProperties.getRoutingExpression())) {
function = this.functionFromExpression(functionProperties.getRoutingExpression(), input);
}
else {
return input instanceof Mono
? Mono.from((Publisher<?>) input).map(v -> route(v, originalInputIsPublisher))
@@ -174,7 +174,7 @@ public class RoutingFunction implements Function<Object, Object> {
private FunctionInvocationWrapper functionFromCallback(Object input) {
if (input instanceof Message) {
String functionDefinition = this.routingCallback.route((Message<?>) input, this.functionProperties);
String functionDefinition = this.routingCallback.functionDefinition((Message<?>) input, this.functionProperties);
if (StringUtils.hasText(functionDefinition)) {
return this.functionFromDefinition(functionDefinition);
}