spring-bootapache-kafkaspring-kafka

@KafkaListener with single Topic and single Partition


I use the spring-kafka annotation @KafkaListener to designate my listener method.

I consume a single topic with a single partition. The messages are never more than one or two a second so a single thread is acceptable. The spring-kafka docs say the @KafkaListener defaults to using a ConcurrentMessageListenerContainer. Is the correct way to control concurrency by using setConcurrency?

Or, should I be somehow create a KafkaMessageListenerContainer, which is single threaded?

I currently use this:

    @Bean("appContainerFactory")
    KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<Integer, String>>
    kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<Integer, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConcurrency(1);
    ...
    }

Solution

  • Is the correct way to control concurrency by using setConcurrency?

    What you have is correct, but the default concurrency on the container is 1 so it's not necessary to specify it when you don't need concurrency.

    Or, should I be somehow create a KafkaMessageListenerContainer, which is single threaded?

    The concurrent container spins up a "child" KafkaMessageListenerContainer for each concurrency so just one is already created for you.