As per spring doc -> https://docs.spring.io/spring-cloud-function/reference/spring-cloud-function/programming-model.html#messageroutingcallback
Using functionRouter defined in my application-test.yml I was able to consume a message from a topic and to route via MessageRoutingCallBack to two different beans filtering by the value of a header:
application-test.yml
spring: cloud: function.definition: functionRouter stream: function.routing.enabled: true bindings: functionRouter-in-0: content-type: application/json destination: ${CLOUD_TOPIC:Example.Input} group: ${CLOUD_STREAM_BINDINGS_INPUT_GROUPID:getExampleConsumerGroup}
MessageRoutingConfig.java
@Configuration@Slf4jpublic class MessageRoutingConfig { private static final Map<String, String> routingFunctions = Map.of( "CONS_A", "consumerA","CONS_B", "consumerB"); @Bean public MessageRoutingCallback routingCallback() { return new MessageRoutingCallback() { @Override public String routingResult(Message<?> message) { String messageType = (String) message.getHeaders().get(KAFKA_HEADER_MSG_TYPE); return routingFunctions.get(messageType); } }; }}
Again from documentation -> https://docs.spring.io/spring-cloud-function/reference/spring-cloud-function/programming-model.html#multiple-routersit is then explained how to create custom RoutingFunction but no examples are given on how to then invoke it in order to have the message consumed by consumerA or consumerB.
Below is my current implementation taken as an example from the spring documentation and adapted.
application-test.yml
spring: cloud: function.definition: functionRouter;mySpecialRouter stream: function.routing.enabled: true bindings: functionRouter-in-0: content-type: application/json destination: ${CLOUD_TOPIC:Example.Input} group: ${CLOUD_STREAM_BINDINGS_INPUT_GROUPID:getExampleConsumerGroup} mySpecialRouter-in-0: content-type: application/json destination: ${CLOUD_TOPIC_NEW:Example.Input2} group: ${CLOUD_STREAM_BINDINGS_INPUT_GROUPID:getExampleConsumerGroup}
MultipleRouterConfiguration.java
@Configurationpublic class MultipleRouterConfiguration2 { @Bean RoutingFunction mySpecialRouter( FunctionCatalog functionCatalog, BeanFactory beanFactory, @Nullable MessageRoutingCallback routingCallback) { Map<String, String> propertiesMap = new HashMap<>(); // Implement correctly propertiesMap with definition or routing-expression return new RoutingFunction( functionCatalog, propertiesMap, new BeanFactoryResolver(beanFactory), routingCallback); }}
Is it possible to route the call to consumerA or consumerB using custom RoutingFunction or am I totally off the mark ?