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?