0
  • I have two microservices written in Java, using Spring Boot.
  • I use Kafka, through Spring Cloud Stream Kafka, to send messages between them.
  • I need to send a custom header, but with no success until now.
  • I have read and tried most of the things I have found on internet and Spring Cloud Stream documentation...

... still I have been unable to make it work.

Which means I never receive a message in the receiver because the header cannot be found and cannot be null. I suspect the header is never written in the message. Right now I am trying to verify this with Kafkacat.

Any help will be wellcome

Thanks in advance.

------ information --------------------

Here it is the sender code:

@SendTo("notifications")
public void send(NotificationPayload payload, String eventId) {
   var headerMap = Collections.singletonMap("EVENT_ID",
                                eventId.getBytes(StandardCharsets.UTF_8));
   MessageHeaders headers = new MessageHeaders(headerMap);
   var message = MessageBuilder.createMessage(payload, headers);
   notifications.send(message);
}

Where notifications is a MessageChannel

Here is the related configuration for message sender.

spring:
  cloud:
    stream:
      defaultBinder: kafka
      bindings:
        notifications:
          binder: kafka
          destination: notifications
          contentType: application/x-java-object;type=com.types.NotificationPayload
              producer:
                partitionCount: 1
                headerMode: headers
      kafka:
        binder:
          headers: EVENT_ID

I have also tried with headers: "EVENT_ID"

Here is the code for the receiver part:

@StreamListener("notifications")
public void receiveNotif(@Header("EVENT_ID") byte[] eventId, 
                         @Payload NotificationPayload payload) {
var eventIdS = new String((byte[]) eventId, StandardCharsets.UTF_8);
...
// do something with the payload
}

And the configuration for the receiving part:

spring:
  cloud:
     stream:
       kafka:
         bindings:
           notifications:
             consumer:
               headerMode: headers

Versions

    <spring-cloud-stream-dependencies.version>Horsham.SR4</spring-cloud-stream-dependencies.version>
    <spring-cloud-stream-binder-kafka.version>3.0.4.RELEASE</spring-cloud-stream-binder-kafka.version>
    <spring-cloud-schema-registry.version>1.0.4.RELEASE</spring-cloud-schema-registry.version>
    <spring-cloud-stream.version>3.0.4.RELEASE</spring-cloud-stream.version>

1 Answers1

0

What version are you using? Describe "can't get it to work" in more detail.

This works fine...

@SpringBootApplication
@EnableBinding(Source.class)
public class So64586916Application {

    public static void main(String[] args) {
        SpringApplication.run(So64586916Application.class, args);
    }

    @InboundChannelAdapter(channel = Source.OUTPUT)
    Message<String> source() {
        return MessageBuilder.withPayload("foo")
                .setHeader("myHeader", "someValue")
                .build();
    }

    @KafkaListener(id = "in", topics = "output")
    void listen(Message<?> in) {
        System.out.println(in);
    }

}
spring.kafka.consumer.auto-offset-reset=earliest
GenericMessage [payload=byte[3], headers={myHeader=someValue, kafka_offset=0, ...
GenericMessage [payload=byte[3], headers={myHeader=someValue, kafka_offset=1, ...

EDIT

I also tested it by sending to the channel directly; again with no problems:

@Autowired
MessageChannel output;

@Bean
public ApplicationRunner runner() {
    return args -> {
        this.output.send(MessageBuilder.withPayload("foo")
                .setHeader("myHeader", "someValue")
                .build());
    };
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Hi. Thanks for your answer. I have edited the questions with the version information and a better explanation of what means "can't get to work" I have also notice that your example is using `@KafkaListener` while I am using `@StreamListener`, could that affect the result? – Jose Luis Ojosnegros Manchon Oct 29 '20 at 15:07
  • I added the `@KafkaListener` just so I could consume the messages published to the `output` channel with the custom header - to prove that the header was set properly (see the edit). I also tested it by sending to the channel directly; again with no problems. I am testing with SR8/3.0.8. – Gary Russell Oct 29 '20 at 15:26