In lines 226-230 of org.springframework.kafka.listener.KafkaMessageListenerContainer
, Spring Kafka assigns a default SimpleAsyncTaskExecutor
as the listener container's consumer task executor:
if (containerProperties.getConsumerTaskExecutor() == null) {
SimpleAsyncTaskExecutor consumerExecutor = new SimpleAsyncTaskExecutor(
(getBeanName() == null ? "" : getBeanName()) + "-C-");
containerProperties.setConsumerTaskExecutor(consumerExecutor);
}
In the docs, it is pointed out that it is possible to use a custom executor instead of SimpleAsyncTaskExecutor
:
If you don’t provide a consumer executor, a
SimpleAsyncTaskExecutor
is used; this executor creates threads with names<beanName>-C-1
(consumer thread). For theConcurrentMessageListenerContainer
, the<beanName>
part of the thread name becomes<beanName>-m
, wherem
represents the consumer instance.n
increments each time the container is started. So, with a bean name of container, threads in this container will be namedcontainer-0-C-1
,container-1-C-1
etc., after the container is started the first time;container-0-C-2
,container-1-C-2
etc., after a stop/start.
However I can't find a way to customize the executor when Spring Boot's auto-configuration is enabled for Kafka (i.e. KafkaAutoConfiguration
).
In short, what is the correct way to set (ContainerProperties#consumerTaskExecutor
)?
答案 0 :(得分:0)
是的,你不能通过配置属性来做到这一点,因为它不是那么简单的属性,但你可以覆盖bean:
@Bean
@ConditionalOnMissingBean
public ConcurrentKafkaListenerContainerFactoryConfigurer kafkaListenerContainerFactoryConfigurer() {
ConcurrentKafkaListenerContainerFactoryConfigurer configurer = new ConcurrentKafkaListenerContainerFactoryConfigurer();
configurer.setKafkaProperties(this.properties);
return configurer;
}
使用您的自定义实现。并在其configure()
方法中调用适当的listenerContainerFactory.getContainerProperties().setConsumerTaskExecutor()
。