|
| 1 | +package com.wellmeet.config; |
| 2 | + |
| 3 | +import com.wellmeet.notification.consumer.dto.NotificationMessage; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 7 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.kafka.annotation.EnableKafka; |
| 12 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 13 | +import org.springframework.kafka.core.ConsumerFactory; |
| 14 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 15 | +import org.springframework.kafka.support.serializer.JsonDeserializer; |
| 16 | + |
| 17 | +@EnableKafka |
| 18 | +@Configuration |
| 19 | +public class KafkaConfig { |
| 20 | + |
| 21 | + @Value("${spring.kafka.bootstrap-servers}") |
| 22 | + private String bootstrapServers; |
| 23 | + |
| 24 | + @Value("${spring.kafka.consumer.group-id}") |
| 25 | + private String groupId; |
| 26 | + |
| 27 | + @Bean |
| 28 | + public ConsumerFactory<String, NotificationMessage> consumerFactory() { |
| 29 | + Map<String, Object> props = new HashMap<>(); |
| 30 | + props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); |
| 31 | + props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId); |
| 32 | + props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 33 | + props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class); |
| 34 | + props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); |
| 35 | + |
| 36 | + props.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false); |
| 37 | + props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, NotificationMessage.class); |
| 38 | + return new DefaultKafkaConsumerFactory<>(props); |
| 39 | + } |
| 40 | + |
| 41 | + @Bean |
| 42 | + public ConcurrentKafkaListenerContainerFactory<String, NotificationMessage> kafkaListenerContainerFactory() { |
| 43 | + ConcurrentKafkaListenerContainerFactory<String, NotificationMessage> factory = |
| 44 | + new ConcurrentKafkaListenerContainerFactory<>(); |
| 45 | + factory.setConsumerFactory(consumerFactory()); |
| 46 | + return factory; |
| 47 | + } |
| 48 | +} |
0 commit comments