Spring Kafka Multiple Listeners for different objects within an ApplicationContext
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a popular distributed streaming platform that enables you to build robust messaging solutions. With an integration library like Spring Kafka, developers can more easily incorporate Kafka within their Spring applications. A common requirement in these applications is to have multiple listener methods that consume different types of messages from various Kafka topics. Understanding how to correctly configure and use multiple Kafka listeners within the same Spring ApplicationContext can greatly enhance your application's responsiveness and scalability.
Overview of Kafka Listeners in Spring
Spring Kafka provides @KafkaListener annotation, which is used to mark a method to be the target of a Kafka message listener on the specified topics. The basic usage involves annotating a method of a managed bean with @KafkaListener and setting the topics or topicPattern attribute to define what topics this listener should subscribe to.
Here is a simple example:
Configuring Multiple Listeners
Configuring multiple listeners in the same application context involves declaring multiple methods with the @KafkaListener annotation, potentially listening to different topics or using different consumer configurations.
Example: Multiple Listeners for Different Topics
Below is an example showing two listeners configured to listen to different topics within the same class:
Example: Different Data Types
If the messages on different topics are of different data types, Spring's message conversion can be leveraged. By configuring appropriate message converters, each listener method can accept a message already converted to the correct type:
Essential Configuration
Handling multiple types of messages and complex configurations often requires defining multiple KafkaListenerContainerFactory beans that configure the specifics of message listener containers for different scenarios.
The following snippet shows how to define a container factory for JSON messages:
Summary Table
Here's a summary of key aspects to consider when setting up multiple Kafka listeners:
| Feature | Description |
| Multiple Methods | Use multiple @KafkaListener methods in the same or different beans. |
| Topic Configuration | Configure each listener to subscribe to specific topics. |
| Data Type Handling | Use different container factories to handle various data formats. |
@KafkaListener Essentials | Utilize attributes like containerFactory to specify which listener container should be used. |
Additional Considerations
- Error Handling: Customize error handling strategies per listener by setting the
errorHandlerattribute on the@KafkaListener. - Concurrency: Configure the
concurrencysetting in theConcurrentKafkaListenerContainerFactoryto handle more messages concurrently. - Ordering and Partitions: Ensure understanding of how Kafka partitions and consumer groups affect message order and processing.
Thorough configuration and understanding of Spring Kafka's facilities can make your application robust, scalable, and responsive, allowing you to effectively handle diverse data streams coming from different Kafka topics.
Related reading
- spring kafka No type information in headers and no default type provided
- spring kafka offset increment even auto commit offset is set to false
- Spring Kafka, overriding max.poll.interval.ms?
- Spring Kafka Partitioning
- Spring Kafka Poll for new messages instead of being notified using `onMessage`
- Spring Kafka Producer not sending to Kafka 1.0.0 (Magic v1 does not support record headers)
- Spring Kafka producers throwing TimeoutExceptions
- Spring Kafka producers throwing TimeoutExceptions

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.