One Developer
One Developer

Reputation: 566

How to disable KAFKA consumer being autostarted without having to make any code changes including setting the autoStartup = "{xyz}" in Spring Boot?

Below is my KAFKA consumer

@Service
public class Consumer {

    private static final Logger LOGGER = Logger.getLogger(Consumer.class.getName());
    public static Queue<ProductKafka> consumeQueue = new LinkedList<>();

    @KafkaListener(topics = "#{'${spring.kafka.topics}'.split('\\\\ ')}", groupId = "#{'${spring.kafka.groupId}'}")
    public void consume(ProductKafka productKafka) throws IOException {
        consumeQueue.add(productKafka);
        LOGGER.info(String.format("#### -> Logger Consumed message -> %s", productKafka.toString()));
        System.out.printf("#### -> Consumed message -> %s", productKafka.toString());
    }
}

and below is my "application.properties" file

spring.kafka.topics=Product
spring.kafka.groupId=Product-Group

My KAFKA consumer is getting started automatically.

However I want to disable KAFKA consumer being autostarted without having to make any changes to the existing code including setting autoStartup = "{xyz}" in the consumer class due to the requirement.

I am looking an existing properties which would disable KAFKA consumer being autostarted, something like this

spring.kafka.consumer.enable=false

Note: I have multiple KAFKA consumers and the above property should disable all the consumers in the project.

do we have any existing properties which would disable KAFKA consumer being autostarted without having to make any changes to the existing code?

Upvotes: 0

Views: 5498

Answers (1)

Gary Russell
Gary Russell

Reputation: 174574

There is no standard out-of-the-box property; you have to provide your own.

autoStartup="${should.start:true}"

will start the container if property should.start is not present.

EDIT

Just add something like this in your application.

@Component
class Customizer {

    Customizer(AbstractKafkaListenerContainerFactory<?, ?, ?> factory,
            @Value("${start.containers:true}") boolean start) {

        factory.setAutoStartup(start);
    }

}
start:
  containers: false

Upvotes: 2

Related Questions