Skip to content

Instantly share code, notes, and snippets.

@Daniel-Jacob
Last active March 4, 2022 15:09
Show Gist options
  • Save Daniel-Jacob/f5ba093aac6d05602ddc198151c6c0ba to your computer and use it in GitHub Desktop.
Save Daniel-Jacob/f5ba093aac6d05602ddc198151c6c0ba to your computer and use it in GitHub Desktop.
Kafka config for retryable topic
import org.springframework.boot.autoconfigure.kafka.KafkaProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.kafka.annotation.EnableKafka
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
import org.springframework.kafka.core.ConsumerFactory
import org.springframework.kafka.core.DefaultKafkaConsumerFactory
import org.springframework.kafka.core.DefaultKafkaProducerFactory
import org.springframework.kafka.core.KafkaTemplate
@EnableKafka
@Configuration
class KafkaConfig {
@Bean
fun consumerFactory(properties: KafkaProperties): ConsumerFactory<String?, String?> =
DefaultKafkaConsumerFactory(properties.buildConsumerProperties())
@Bean
fun kafkaListenerContainerFactory(properties: KafkaProperties): ConcurrentKafkaListenerContainerFactory<String?, String?> =
ConcurrentKafkaListenerContainerFactory<String?, String?>()
.apply { consumerFactory = consumerFactory(properties) }
@Bean
fun kafkaErrorProducerFactory(kafkaProperties: KafkaProperties): DefaultKafkaProducerFactory<String, String> =
DefaultKafkaProducerFactory(kafkaProperties.buildProducerProperties())
@Bean
fun kafkaTemplate(kafkaProperties: KafkaProperties): KafkaTemplate<String, String> =
KafkaTemplate(kafkaErrorProducerFactory(kafkaProperties))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment