liveBook · Manning

KafkaHeaders

As explained in Kafka Streams in Action, Second Edition

KafkaHeaders is a Spring Kafka constants class defining header names for Kafka record metadata.

KafkaHeaders defines constants for accessing Kafka record metadata in Spring Kafka. Consumer-side constants begin with RECEIVED, distinguishing incoming-record headers from producer metadata. section 12.2.2

Read incoming record metadata

Annotate listener parameters with KafkaHeaders constants to retrieve the incoming timestamp and message key; topic, partition, and offset are also available. section 12.2.2

Listing 12.13 Getting the message key and timestamp from the incoming Kafka record
public void doProcessLoanApplication(LoanApplication loanApplication,
          @Header(Kafka Headers.RECEIVED_TIMESTAMP)
 long timestamp,                                        #1
          @Header(Kafka Headers.RECEIVED_MESSAGE_KEY)
 String key)     #2

See the listener signature using RECEIVED_TIMESTAMP and RECEIVED_MESSAGE_KEY to extract metadata alongside the loan application. section 12.2.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Defines producer and consumer header constants and demonstrates incoming timestamp and key extraction, while deferring other header usages to brief mentions.
Examples
Uses a loan-processing listener that extracts RECEIVED_TIMESTAMP and RECEIVED_MESSAGE_KEY from an incoming record.
What it assumes
Assumes familiarity with Spring Kafka listeners and annotations while demonstrating header extraction directly from a listener method.
What you can do after
Operate Spring Kafka listeners that log or process incoming record keys and timestamps without manually parsing records.
How it connects
Connects Kafka record metadata with @Header, KafkaTemplate, listener methods, RecordMetadata, and asynchronous produce callbacks.

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.