Spring Kafka
Custom Headers
Kafka Programming
Java Development
Message Broker

Adding custom header using Spring Kafka

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Spring Kafka is a robust framework that allows developers to connect to Apache Kafka and send or receive messages efficiently. One powerful feature of Spring Kafka is the ability to customize the headers of Kafka messages before sending them. This can be particularly useful for implementing features like routing, message filtering, or enriching messages with additional metadata.

Introduction to Kafka Headers

Kafka headers are a feature introduced in Kafka 0.11.0. Headers allow storing key-value pairs associated with the message. These headers are useful for handling cross-cutting concerns like routing, tracing, and so on, without cluttering the message payload.

Configuring Spring Kafka

Before we dive into adding custom headers, it's essential to ensure that your project is configured to use Spring Kafka. This typically involves adding the appropriate Spring Boot starter dependency to your pom.xml or build.gradle file. For Maven, add:

xml
1<dependency>
2    <groupId>org.springframework.kafka</groupId>
3    <artifactId>spring-kafka</artifactId>
4    <version>${spring-kafka.version}</version>
5</dependency>

For Gradle:

groovy
implementation 'org.springframework.kafka:spring-kafka:${springKafkaVersion}'

Adding Custom Headers to Kafka Messages

In Spring Kafka, you can customize headers when producing messages. To demonstrate this, let's assume we have a simple application that sends messages to a Kafka topic.

1. Creating Kafka Producer

First, define a Kafka template, which is required to send messages:

java
1@Configuration
2public class KafkaProducerConfig {
3
4    @Bean
5    public ProducerFactory<String, String> producerFactory() {
6        Map<String, Object> configProps = new HashMap<>();
7        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
8        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
9        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
10        return new DefaultKafkaProducerFactory<>(configProps);
11    }
12
13    @Bean
14    public KafkaTemplate<String, String> kafkaTemplate() {
15        return new KafkaTemplate<>(producerFactory());
16    }
17
18}

2. Sending Messages with Custom Headers

When sending a message, you can add custom headers using Message<?> of Spring Messaging. Here’s how it can be done:

java
1@Service
2public class KafkaSenderService {
3
4    @Autowired
5    private KafkaTemplate<String, String> kafkaTemplate;
6
7    public void sendMessageWithHeaders(String topic, String message) {
8        Message<String> messageToSend = MessageBuilder.withPayload(message)
9                .setHeader(KafkaHeaders.TOPIC, topic)
10                .setHeader("X-Custom-Header", "CustomHeaderValue")
11                .build();
12        this.kafkaTemplate.send(messageToSend);
13    }
14}

Here X-Custom-Header is the custom header with its value set to "CustomHeaderValue". The KafkaHeaders.TOPIC is a standard header provided by Spring Kafka to specify the target topic.

Conclusion

Adding custom headers in Spring Kafka is straightforward thanks to useful abstractions like KafkaTemplate and Spring's Message building utilities. Custom headers are beneficial for adding metadata to messages, which can be used by consumers to make more detailed processing decisions.

Summary Table

FeatureUtility or Use Case
Kafka HeadersIntroduced in Kafka 0.11.0, used for metadata
Spring KafkaFramework for Kafka integration
KafkaTemplateSpring template for Kafka producers
Custom HeadersUseful for routing, tracing, and more

By leveraging these features, developers can create robust Kafka applications with enhanced message capabilities.


Course illustration
Course illustration

All Rights Reserved.