Skip to content

Instantly share code, notes, and snippets.

@up1
Last active November 10, 2025 16:21
Show Gist options
  • Select an option

  • Save up1/f263ad7d80302644d0861393e8c0e1c9 to your computer and use it in GitHub Desktop.

Select an option

Save up1/f263ad7d80302644d0861393e8c0e1c9 to your computer and use it in GitHub Desktop.
Spring Boot 4 and Kafka share consumer
@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);
}
}
@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
}
@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