Search code examples
spring-bootspring-kafka

add kafka autoStartUp false in the ConsumerConfig


how to add kafka property autoStartUp false manualy at property lvl ? what i mean, for example i can do it like that:

@KafkaListener(autoStartUp = "false")

but i wanna do it in the ConsumerProperties, in the own BeanPostProcessor. I could't find this property in the ConsumerProperties for example:

Map<String, Object> props = new HashMap<>();

    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaConfig.getBootstrapServers());
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
    //here i need to add autoStartUp to false

Solution

  • The autoStartup has nothing to do with Apache Kafka. It is native Spring container feature: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-lifecycle-processor. So, even if it would be possible via your props, it would not sound reasonable and felt more like abusing of Apache Kafka config with options which are not going to be parsed by Kafka mechanisms.

    What you need is really pay more attention to that autoStartup option JavaDocs:

    /**
     * Set to true or false, to override the default setting in the container factory. May
     * be a property placeholder or SpEL expression that evaluates to a {@link Boolean} or
     * a {@link String}, in which case the {@link Boolean#parseBoolean(String)} is used to
     * obtain the value.
     * <p>SpEL {@code #{...}} and property place holders {@code ${...}} are supported.
     * @return true to auto start, false to not auto start.
     * @since 2.2
     */
    String autoStartup() default "";
    

    and look more in the Docs: https://docs.spring.io/spring-kafka/docs/2.6.2/reference/html/#kafka-listener-annotation

    So, roughly speaking, if you add an autoStartUp into your props bean, you can configure your @KafkaListener like this:

    @KafkaListener(autoStartUp = "#{props.autoStartUp}")