Search code examples
spring-cloud-stream

Spring cloud stream 4.x metadatachannel


want to get the output channel name after message is published to the destination.

metadata channel is not available in 4.x spring.cloud.stream.kafka.bindings.myBinding.producer.**record-metadata-channel**

Spring cloud stream : 4.0.1

binder type : Kafka


Solution

  • This works for me with version 4.0.1. When the supplier is invoked and the message is published to the Kafka topic, the handler method on the record metadata channel is called. I can see the sent message in the output. What is not working in your application?

    @SpringBootApplication
    public class So75681179Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So75681179Application.class, args);
        }
    
        @Bean
        public Supplier<Message<String>> supplier() {
            return () -> MessageBuilder.withPayload("foo").setHeader(KafkaHeaders.KEY, "my-foo".getBytes(StandardCharsets.UTF_8)).build();
        }
    
        @Bean
        public MessageChannel fooRecordChannel() {
            return new DirectChannel();
        }
    
        @Bean
        public IntegrationFlow integrationFlow() {
            return f -> f.channel("fooRecordChannel")
                    .handle((payload, messageHeaders) -> {
                        System.out.println("Sent message: " + new String((byte[]) payload));
                        return payload;
                    });
        }
    }
    

    and the configuration:

    spring:
      cloud:
        function:
          definition: supplier
        stream:
          kafka:
            bindings:
              supplier-out-0:
                producer:
                  recordMetadataChannel: fooRecordChannel