|
| 1 | +package com.baeldung.spring.kafka; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.apache.kafka.clients.admin.AdminClientConfig; |
| 7 | +import org.apache.kafka.clients.admin.NewTopic; |
| 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.core.KafkaAdmin; |
| 12 | + |
| 13 | +@Configuration |
| 14 | +public class KafkaTopicConfig { |
| 15 | + |
| 16 | + @Value(value = "${kafka.bootstrapAddress}") |
| 17 | + private String bootstrapAddress; |
| 18 | + |
| 19 | + @Value(value = "${message.topic.name}") |
| 20 | + private String topicName; |
| 21 | + |
| 22 | + @Value(value = "${partitioned.topic.name}") |
| 23 | + private String partionedTopicName; |
| 24 | + |
| 25 | + @Value(value = "${filtered.topic.name}") |
| 26 | + private String filteredTopicName; |
| 27 | + |
| 28 | + @Value(value = "${greeting.topic.name}") |
| 29 | + private String greetingTopicName; |
| 30 | + |
| 31 | + @Bean |
| 32 | + public KafkaAdmin kafkaAdmin() { |
| 33 | + Map<String, Object> configs = new HashMap<>(); |
| 34 | + configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress); |
| 35 | + return new KafkaAdmin(configs); |
| 36 | + } |
| 37 | + |
| 38 | + @Bean |
| 39 | + public NewTopic topic1() { |
| 40 | + return new NewTopic(topicName, 1, (short) 1); |
| 41 | + } |
| 42 | + |
| 43 | + @Bean |
| 44 | + public NewTopic topic2() { |
| 45 | + return new NewTopic(partionedTopicName, 6, (short) 1); |
| 46 | + } |
| 47 | + |
| 48 | + @Bean |
| 49 | + public NewTopic topic3() { |
| 50 | + return new NewTopic(filteredTopicName, 1, (short) 1); |
| 51 | + } |
| 52 | + |
| 53 | + @Bean |
| 54 | + public NewTopic topic4() { |
| 55 | + return new NewTopic(greetingTopicName, 1, (short) 1); |
| 56 | + } |
| 57 | +} |
0 commit comments