Reputation: 11
With Spring Boot 3.3.3 (spring.threads.virtual.enabled=true), the default Kafka autoconfiguration uses virtual threads. However, my custom Kafka bean configuration still uses platform threads. Could someone share a working example of how to configure a Kafka listener to use virtual threads? Specifically, I'm looking for configuration examples for:
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(
ConsumerFactory<String, String> consumerFactory) {
// How to configure this to use virtual threads?
}
Upvotes: 1
Views: 68
Reputation: 121542
So, if you have your own kafkaListenerContainerFactory
, then Spring Boot's one is not going to be auto-configured:
@Bean
@ConditionalOnMissingBean(
name = {"kafkaListenerContainerFactory"}
)
ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
To enable virtual threads on your custom container factory, you have to modify its bean like this:
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(
ConsumerFactory<String, String> consumerFactory) {
ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory();
...
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("kafka-");
executor.setVirtualThreads(true);
factory.getContainerProperties().setListenerTaskExecutor(executor);
}
This mimics what is there in Spring Boot auto-configuration.
NOTE: Apache Kafka uses new Thread()
internally a lot, so assigning virtual threads for the listener container might not help too much.
Upvotes: 0