setConcurrency configures a ConcurrentKafkaListenerContainerFactory with the desired number of concurrent listener containers or consumer threads. Setting it to the input topic’s partition count can provide one KafkaConsumer per partition and maximize throughput. section 12.2.2
Listing 12.11 Increasing the concurrency level for a container listener factory
@Bean
public ConcurrentKafkaListenerContainerFactory<String, LoanApplication>
kafkaListenerContainerFactory() { #1
ConcurrentKafkaListenerContainerFactory<String, LoanApplication>
kafkaListenerContainerFactory =
new ConcurrentKafkaListenerContainerFactory<>();
kafkaListenerContainerFactory.setConsumerFactory(consumerFactory());
kafkaListenerContainerFactory.setConcurrency(partitions); #2
return kafkaListenerContainerFactory;
}
Configure the consumer factory, set concurrency from the configured partition count, and return the customized listener-container factory. section 12.2.2
Apply the configuration in a Spring Kafka application by:
- Defining a ConcurrentKafkaListenerContainerFactory bean and calling setConcurrency(partitions). section 12.2.2
- Naming the bean kafkaListenerContainerFactory to replace Spring Boot’s default without changing listener code. section 12.2.2
- Using a custom bean name with the @KafkaListener containerFactory attribute when explicit selection is preferred. section 12.2.2
