GH-726 Enhance MessageRoutingCallback to optionally return enriched Message

Resolves #726
This commit is contained in:
Oleg Zhurakousky
2021-11-11 17:16:19 +01:00
parent a716662340
commit fc39f09f1a
5 changed files with 246 additions and 31 deletions

View File

@@ -22,8 +22,7 @@ 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.
* it will be picked up by the {@link RoutingFunction}.
*
* While {@link RoutingFunction} provides several mechanisms to determine the route-to function definition
* this callback takes precedence over all of them.
@@ -34,10 +33,58 @@ import org.springframework.messaging.Message;
public interface MessageRoutingCallback {
/**
* Determines the name of the function definition to route incoming {@link Message}.
*
* @param message instance of incoming {@link Message}
* @return the name of the route-to function definition
* @deprecated in 3.1 in favor of {@link #routingResult(Message)}
*/
String functionDefinition(Message<?> message);
@Deprecated
default String functionDefinition(Message<?> message) {
return null;
}
/**
* Computes and returns the instance of {@link FunctionRoutingResult} which encapsulates,
* at the very minimum, function definition and optionally Message to be used downstream.
* <br><br>
* Providing such message is primarily an optimization feature. It could be useful for cases
* where routing procedure is complex and results in, let's say, conversion of the payload to
* the target type, which would effectively be thrown away if the ability to modify the target
* message for downstream use didn't exist, resulting in repeated transformation, type conversion etc.
*
* @param message input message
* @return instance of {@link FunctionRoutingResult} containing the result of the routing computation
*/
default FunctionRoutingResult routingResult(Message<?> message) {
return new FunctionRoutingResult(functionDefinition(message));
}
/**
* Domain object that represents the result of the {@link MessageRoutingCallback#routingResult(Message)}
* computation. It consists of function definition and optional Message to be used downstream
* (see {@link MessageRoutingCallback#routingResult(Message)} for more details.
*
* @author Oleg Zhurakousky
*
*/
final class FunctionRoutingResult {
private final String functionDefinition;
private final Message<?> message;
FunctionRoutingResult(String functionDefinition, Message<?> message) {
this.functionDefinition = functionDefinition;
this.message = message;
}
public FunctionRoutingResult(String functionDefinition) {
this(functionDefinition, null);
}
public String getFunctionDefinition() {
return functionDefinition;
}
public Message<?> getMessage() {
return message;
}
}
}

View File

@@ -1091,6 +1091,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
}
private boolean isExtractPayload(Message<?> message, Type type) {
if (this.isRoutingFunction()) {
return false;
}
if (FunctionTypeUtils.isCollectionOfMessage(type)) {
return true;
}

View File

@@ -27,6 +27,7 @@ import reactor.core.publisher.Mono;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionProperties;
import org.springframework.cloud.function.context.MessageRoutingCallback;
import org.springframework.cloud.function.context.MessageRoutingCallback.FunctionRoutingResult;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.BeanResolver;
@@ -104,7 +105,15 @@ public class RoutingFunction implements Function<Object, Object> {
if (input instanceof Message) {
Message<?> message = (Message<?>) input;
if (this.routingCallback != null) {
function = this.functionFromCallback(message);
FunctionRoutingResult routingResult = this.routingCallback.routingResult(message);
if (routingResult != null) {
if (StringUtils.hasText(routingResult.getFunctionDefinition())) {
function = this.functionFromDefinition(routingResult.getFunctionDefinition());
}
if (routingResult.getMessage() != null) {
message = routingResult.getMessage();
}
}
}
if (function == null) {
if (StringUtils.hasText((String) message.getHeaders().get("spring.cloud.function.definition"))) {
@@ -172,15 +181,19 @@ public class RoutingFunction implements Function<Object, Object> {
+ "spring.cloud.function.routing-expression' as application properties.");
}
private FunctionInvocationWrapper functionFromCallback(Object input) {
if (input instanceof Message) {
String functionDefinition = this.routingCallback.functionDefinition((Message<?>) input);
if (StringUtils.hasText(functionDefinition)) {
return this.functionFromDefinition(functionDefinition);
}
}
return null;
}
// private FunctionInvocationWrapper functionFromCallback(Object input) {
// if (input instanceof Message) {
// Object routingResult = this.routingCallback.functionDefinition((Message<?>) input);
// if (routingResult != null && routingResult instanceof String) {
//
// }
// if (StringUtils.hasText(functionDefinition)) {
// return this.functionFromDefinition(functionDefinition);
// }
// }
// logger.info("Unable to determine route-to function from the provided MessageRoutingCallback");
// return null;
// }
private FunctionInvocationWrapper functionFromDefinition(String definition) {
FunctionInvocationWrapper function = functionCatalog.lookup(definition);