|
| 1 | +package io.tpd.kafkaexample; |
| 2 | + |
| 3 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 4 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 5 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 6 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.SpringApplication; |
| 9 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 10 | +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 13 | +import org.springframework.kafka.core.*; |
| 14 | +import org.springframework.kafka.support.serializer.JsonDeserializer; |
| 15 | +import org.springframework.kafka.support.serializer.JsonSerializer; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +@SpringBootApplication |
| 21 | +public class KafkaExampleApplication { |
| 22 | + |
| 23 | + public static void main(String[] args) { |
| 24 | + SpringApplication.run(KafkaExampleApplication.class, args); |
| 25 | + } |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private KafkaProperties kafkaProperties; |
| 29 | + |
| 30 | + // Configuration based on https://www.codenotfound.com/spring-kafka-json-serializer-deserializer-example.html |
| 31 | + |
| 32 | + // Producer configuration |
| 33 | + |
| 34 | + @Bean |
| 35 | + public Map<String, Object> producerConfigs() { |
| 36 | + Map<String, Object> props = new HashMap<>(kafkaProperties.buildProducerProperties()); |
| 37 | + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); |
| 38 | + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); |
| 39 | + |
| 40 | + return props; |
| 41 | + } |
| 42 | + |
| 43 | + @Bean |
| 44 | + public ProducerFactory<String, PracticalAdvice> producerFactory() { |
| 45 | + return new DefaultKafkaProducerFactory<>(producerConfigs()); |
| 46 | + } |
| 47 | + |
| 48 | + @Bean |
| 49 | + public KafkaTemplate<String, PracticalAdvice> kafkaTemplate() { |
| 50 | + return new KafkaTemplate<>(producerFactory()); |
| 51 | + } |
| 52 | + |
| 53 | + // Consumer configuration |
| 54 | + |
| 55 | + @Bean |
| 56 | + public Map<String, Object> consumerConfigs() { |
| 57 | + Map<String, Object> props = new HashMap<>(kafkaProperties.buildConsumerProperties()); |
| 58 | + props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 59 | + props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class); |
| 60 | + props.put(ConsumerConfig.GROUP_ID_CONFIG, "json"); |
| 61 | + |
| 62 | + return props; |
| 63 | + } |
| 64 | + |
| 65 | + @Bean |
| 66 | + public ConsumerFactory<String, PracticalAdvice> consumerFactory() { |
| 67 | + return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(), |
| 68 | + new JsonDeserializer<>(PracticalAdvice.class)); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + public ConsumerFactory<String, String> stringConsumerFactory() { |
| 73 | + return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(), |
| 74 | + new StringDeserializer()); |
| 75 | + } |
| 76 | + |
| 77 | + @Bean |
| 78 | + public ConcurrentKafkaListenerContainerFactory<String, PracticalAdvice> kafkaListenerContainerFactory() { |
| 79 | + ConcurrentKafkaListenerContainerFactory<String, PracticalAdvice> factory = |
| 80 | + new ConcurrentKafkaListenerContainerFactory<>(); |
| 81 | + factory.setConsumerFactory(consumerFactory()); |
| 82 | + |
| 83 | + return factory; |
| 84 | + } |
| 85 | + |
| 86 | + @Bean |
| 87 | + public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerStringContainerFactory() { |
| 88 | + ConcurrentKafkaListenerContainerFactory<String, String> factory = |
| 89 | + new ConcurrentKafkaListenerContainerFactory<>(); |
| 90 | + factory.setConsumerFactory(stringConsumerFactory()); |
| 91 | + |
| 92 | + return factory; |
| 93 | + } |
| 94 | +} |
0 commit comments