1

I want get the offset and partition information after i produce a message to kafka topic. I read through spring cloud stream kafka binding document and found that that can be achieved by fecthing RECORD_METADATA kafka header.

From Spring documentation: (https://cloud.spring.io/spring-cloud-static/spring-cloud-stream-binder-kafka/3.0.0.RELEASE/reference/html/spring-cloud-stream-binder-kafka.html#kafka-producer-properties)

recordMetadataChannel The bean name of a MessageChannel to which successful send results should be sent; the bean must exist in the application context. The message sent to the channel is the sent message (after conversion, if any) with an additional header KafkaHeaders.RECORD_METADATA. The header contains a RecordMetadata object provided by the Kafka client; it includes the partition and offset where the record was written in the topic.

ResultMetadata meta = sendResultMsg.getHeaders().get(KafkaHeaders.RECORD_METADATA, RecordMetadata.class)


I have configured my output topic bean name as message channel in the property file spring.cloud.stream.kafka.bindings.acknowledgement-out.producer.record-metadata-channel = acknowledgement-out

my customized interface and producer class as below:

public interface OutputAcknowledgement {    

    @Output("acknowledgement-out")
    MessageChannel output();

}

Producer class:

@EnableBinding(OutputAcknowledgement.class)
public class AcknowledgementProducer {
   

    @Autowired
    OutputAcknowledgement outputAcknowledgement;

    public Boolean produce(Acknowledgement acknowledgement) {
        Message msg = MessageBuilder.withPayload(acknowledgement).build();

        boolean val = outputAcknowledgement.output().send(msg);

        RecordMetadata recordMetadata = msg.getHeaders().get(KafkaHeaders.RECORD_METADATA, RecordMetadata.class);

Getting null for recordMetadata.

Please suggest whether my approach is correct?

Robert Christopher
  • 4,940
  • 1
  • 20
  • 21
Jay
  • 11
  • 1

1 Answers1

0

You're getting null because it doesn't exist in that message object at the time you're accessing it. According to the docs the metadata is only provided after successful publish. See this answer on how to get record metadata by providing a handler/consumer for the record metadata channel.

CleverChuk
  • 141
  • 1
  • 6