title: Spring Kafka and number of topic consumers
body: <p>In my Spring Boot/Kafka project I have the following consumer config:</p>
  
  <pre><code>@Configuration
  public class KafkaConsumerConfig {
  
      @Bean
      public ConsumerFactory&lt;String, String&gt; consumerFactory(KafkaProperties kafkaProperties) {
          return new DefaultKafkaConsumerFactory&lt;&gt;(kafkaProperties.buildConsumerProperties(), new StringDeserializer(), new JsonDeserializer&lt;&gt;(String.class));
      }
  
      @Bean
      public ConcurrentKafkaListenerContainerFactory&lt;String, String&gt; kafkaListenerContainerFactory(KafkaProperties kafkaProperties) {
  
          ConcurrentKafkaListenerContainerFactory&lt;String, String&gt; factory = new ConcurrentKafkaListenerContainerFactory&lt;&gt;();
          factory.setConsumerFactory(consumerFactory(kafkaProperties));
          factory.setConcurrency(10);    
          return factory;
      }
  
      @Bean
      public ConsumerFactory&lt;String, Post&gt; postConsumerFactory(KafkaProperties kafkaProperties) {
          return new DefaultKafkaConsumerFactory&lt;&gt;(kafkaProperties.buildConsumerProperties(), new StringDeserializer(), new JsonDeserializer&lt;&gt;(Post.class));
      }
  
      @Bean
      public ConcurrentKafkaListenerContainerFactory&lt;String, Post&gt; postKafkaListenerContainerFactory(KafkaProperties kafkaProperties) {
  
          ConcurrentKafkaListenerContainerFactory&lt;String, Post&gt; factory = new ConcurrentKafkaListenerContainerFactory&lt;&gt;();
          factory.setConsumerFactory(postConsumerFactory(kafkaProperties));
  
          return factory;
      }
  
  }
  </code></pre>
  
  <p>This is my <code>PostConsumer</code>:</p>
  
  <pre><code>@Component
  public class PostConsumer {
  
      @Autowired
      private PostService postService;
  
      @KafkaListener(topics = "${kafka.topic.post.send}", containerFactory = "postKafkaListenerContainerFactory")
      public void sendPost(ConsumerRecord&lt;String, Post&gt; consumerRecord) {
  
          postService.sendPost(consumerRecord.value());
  
      }
  
  }
  </code></pre>
  
  <p>and the application.properties:</p>
  
  <pre><code>spring.kafka.bootstrap-servers=${kafka.host}:${kafka.port}
  spring.kafka.consumer.auto-offset-reset=earliest
  spring.kafka.consumer.group-id=groupname
  spring.kafka.consumer.enable-auto-commit=false
  kafka.topic.post.send=post.send
  kafka.topic.post.sent=post.sent
  kafka.topic.post.error=post.error
  </code></pre>
  
  <p>As you may see, I have added <strong>factory.setConcurrency(10);</strong> but it doesn't work. All of the <code>PostConsumer.sendPost</code> execute in the same Thread with name <code>org.springframework.kafka.KafkaListenerEndpointContainer#1-8-C-1</code> </p>
  
  <p>I'd like to be able to control the number of concurrent <code>PostConsumer.sendPost</code> listeners in order to work in parallel. Please show me how it can be achieved with Spring Boot and Spring Kafka. </p>