Skip to content

Commit

Permalink
change kafka config
Browse files Browse the repository at this point in the history
  • Loading branch information
xcodeassociated committed Sep 17, 2024
1 parent a582a5b commit 896447f
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.clients.producer.ProducerConfig
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.kafka.common.serialization.StringSerializer
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
Expand All @@ -24,6 +25,12 @@ import org.springframework.kafka.support.serializer.JsonSerializer
@EnableKafka
class KafkaConfig {

@Value("\${spring.kafka.bootstrap-servers}")
private lateinit var bootstrapServer: String

@Value("\${spring.kafka.consumer.group-id}")
private lateinit var groupId: String

@Bean
fun kafkaListenerContainerFactory(consumerFactory: ConsumerFactory<String, JsonNode>) =
ConcurrentKafkaListenerContainerFactory<String, JsonNode>().also { it.consumerFactory = consumerFactory }
Expand All @@ -32,6 +39,8 @@ class KafkaConfig {
fun consumerFactory() = DefaultKafkaConsumerFactory<String, JsonNode>(consumerProps)

val consumerProps = mapOf(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServer,
ConsumerConfig.GROUP_ID_CONFIG to groupId,
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to JsonDeserializer::class.java,
JsonDeserializer.USE_TYPE_INFO_HEADERS to false,
Expand All @@ -44,6 +53,7 @@ class KafkaConfig {
fun producerFactory() = DefaultKafkaProducerFactory<String, KafkaMessage>(senderProps)

val senderProps = mapOf(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServer,
ProducerConfig.LINGER_MS_CONFIG to 10,
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG to StringSerializer::class.java,
ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG to JsonSerializer::class.java
Expand Down

0 comments on commit 896447f

Please sign in to comment.