Last active
November 10, 2025 16:21
-
-
Save up1/f263ad7d80302644d0861393e8c0e1c9 to your computer and use it in GitHub Desktop.
Spring Boot 4 and Kafka share consumer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| public class ShareConsumerConfig { | |
| @Bean | |
| public ShareConsumerFactory<String, String> shareConsumerFactory() { | |
| Map<String, Object> props = new HashMap<>(); | |
| props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); | |
| props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, | |
| StringDeserializer.class); | |
| props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, | |
| StringDeserializer.class); | |
| return new DefaultShareConsumerFactory<>(props); | |
| } | |
| @Bean | |
| public ShareKafkaListenerContainerFactory<String, String> | |
| shareKafkaListenerContainerFactory( | |
| ShareConsumerFactory<String, String> shareConsumerFactory) { | |
| return new ShareKafkaListenerContainerFactory<>(shareConsumerFactory); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @KafkaListener( | |
| topics = "image-processing", | |
| groupId = "image-processors", | |
| containerFactory = "shareKafkaListenerContainerFactory" | |
| ) | |
| public void processImage(String imageUrl) { | |
| // Process the image | |
| imageService.process(imageUrl); | |
| // Implicit ACCEPT when method completes successfully | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Bean | |
| public ShareKafkaListenerContainerFactory<String, String> | |
| shareKafkaListenerContainerFactory( | |
| ShareConsumerFactory<String, String> shareConsumerFactory) { | |
| ShareKafkaListenerContainerFactory<String, String> factory = | |
| new ShareKafkaListenerContainerFactory<>(shareConsumerFactory); | |
| factory.setConcurrency(10); // 10 concurrent consumer threads | |
| return factory; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment