Home > OS >  Best way to disable @KafkaListeners
Best way to disable @KafkaListeners

Time:08-04

My application contains several @KafkaListeners. When I run the app locally or run some @SpringBootTests (without kafka), I got my log polluted with

[org.apache.kafka.clients.NetworkClient] - [Consumer clientId=consumer-app-1, groupId=app] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected

For such cases, I would like to disable listeners, as they are useless anyways. I know I can do it with

@KafkaListener(... autoStartup = "${consumer.auto.start:false}")

but adding this property to all of the consumers feels cumbersome.

Unfortunately, there is no general property like spring.kafka.consumer.group-id that would affect all the consumers.

Is there a better way to achieve the same?

Thanks

CodePudding user response:

If you are not interested in Spring for Apache Kafka activity in some of your Spring Boot tests, just consider to exclude its auto-configuration:

@SpringBootTest
@EnableAutoConfiguration(exclude=KafkaAutoConfiguration.class)

This way the @EnableKafka won't be applied to your application context and no KafkaListenerContainers are going to be registered to start on context refresh.

UPDATE

Another way, for the reason you explained, a respective configuration property, which you can add to application.properties, which could be a test scope-specific, or profile-based. See more info in Spring Boot docs: https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/using-boot-auto-configuration.html#using-boot-disabling-specific-auto-configuration

The property name is spring.autoconfigure.exclude - you have to specify fully-qualified class name. Therefore for your use-case it is:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.kafka.org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration

See also how to have test-specific application.properties or divided by the profile: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.profiles

  • Related