Home > Blockchain >  spring boot kafka producer disable topic auto creation
spring boot kafka producer disable topic auto creation

Time:12-11

I need to disable the automatic topic creation for kafka producer when topic is not available. I am using Spring Boot 2.7.6, that means spring-kafka version 2.8.11

I found similar property for Kafka consumer - ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG and that works fine.

I expect Spring to throw exception during message sending if topic is not available. Currently, the topic gets created automatically.

Consumer:

@Bean
fun kafkaConsumerFactory(): ConsumerFactory<String?, String?> {
    val props: MutableMap<String, Any> = HashMap()
    props[ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG] = bootstrapAddress
    props[ConsumerConfig.GROUP_ID_CONFIG] = groupId
    props[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "latest"
    props[ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG] = false
    props[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = ErrorHandlingDeserializer::class.java
    props[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = ErrorHandlingDeserializer::class.java
    props[ErrorHandlingDeserializer.KEY_DESERIALIZER_CLASS] = StringDeserializer::class.java
    props[ErrorHandlingDeserializer.VALUE_DESERIALIZER_CLASS] = StringDeserializer::class.java
    return DefaultKafkaConsumerFactory(props)
}

Producer:

@Bean
fun kafkaProducerFactory(): ProducerFactory<String?, String?> {
    val configProps: MutableMap<String, Any> = HashMap()
    configProps[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = bootstrapAddress
    configProps[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java
    configProps[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java
    return DefaultKafkaProducerFactory(configProps)
}

Is that impossible, or am I missing something? Thanks

CodePudding user response:

There is no such a property for Kafka producer. You have to use a Broker one: auto.create.topics.enable: https://kafka.apache.org/documentation/#brokerconfigs

  • Related