2

I am trying to use the Content-based routing in the latest version of the Spring Cloud stream. As per this document -> Content-based routing, it mentions how to use it in the legacy system/StreamListener.

This is my code with StreamListener

@StreamListener(target = EventChannels.FILE_REQUEST_IN
            , condition = "headers['saga_request']=='FILE_SUBMIT'")
public void handleSubmitFile(@Payload FileSubmitRequest request) {
        
}

@StreamListener(target = EventChannels.FILE_REQUEST_IN
            , condition = "headers['saga_request']=='FILE_CANCEL'")
public void handleCancelFile(@Payload FileCancelRequest request) {
        
}

By using the condition, it was possible to route the message to two different functions.

I am trying to consume the message with a Functional interface approach as below.

    @Bean
    public Consumer<String> consumeMessage(){
        return event -> {
            try {
                LOGGER.info("Consumer is working: {}", event);
            } catch (Exception ex) {
                LOGGER.error("Exception while processing");
            }
        };
    }

How can I achieve similar content-based routing in the functions? TIA.

Other details->

  1. Spring boot version - 2.3.12.RELEASE
  2. Spring cloud version - Hoxton.SR11
Swapnil
  • 801
  • 3
  • 19
  • 42

1 Answers1

1

Have you seen this - https://docs.spring.io/spring-cloud-stream/docs/3.1.4/reference/html/spring-cloud-stream.html#_event_routing? We provide two different routing models TO and FROM. The included link contains samples so please look through it and feel free to post any followups

Oleg Zhurakousky
  • 5,820
  • 16
  • 17
  • I checked this document. Looks like to achieve the routing, I need to use spring.cloud.function.routing-expression. But I have multiple functions on the same topic. As I mentioned in the StreamListner example, I have only one Kafka topic but the header separates them into different functions. Would you mind explaining how can I use the routing expression? If you have any sample examples, that would be helpful. – Swapnil Oct 20 '21 at 04:12
  • Exactly similar requirement - https://stackoverflow.com/questions/69216229/spring-cloud-streamlistener-condition-deprecated-what-is-the-alternative – Swapnil Oct 20 '21 at 06:04
  • Were you able to get it to work Swapnil ? – secret129 Oct 06 '22 at 11:59
  • StreamListener is GONE. It's living out its last days and is already removed from the main branch. There is a whole section on routing that I included in the link and there are samples – Oleg Zhurakousky Oct 06 '22 at 14:18