-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.patch
115 lines (115 loc) · 3.99 KB
/
kafka.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
Index: src/main/java/com/handson/searchengine/config/KafkaTopicConfig.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/searchengine/config/KafkaTopicConfig.java b/src/main/java/com/handson/searchengine/config/KafkaTopicConfig.java
new file mode 100644
--- /dev/null (date 1645314637143)
+++ b/src/main/java/com/handson/searchengine/config/KafkaTopicConfig.java (date 1645314637143)
@@ -0,0 +1,30 @@
+package com.handson.searchengine.config;
+
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.kafka.core.KafkaAdmin;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static com.handson.searchengine.kafka.Producer.APP_TOPIC;
+
+public class KafkaTopicConfig {
+
+ @Value( "${spring.kafka.bootstrap-servers}")
+ private String bootstrapAddress;
+
+ @Bean
+ public KafkaAdmin kafkaAdmin() {
+ Map<String, Object> configs = new HashMap<>();
+ configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
+ return new KafkaAdmin(configs);
+ }
+
+ @Bean
+ public NewTopic topic1() {
+ return new NewTopic(APP_TOPIC, 1, (short) 1);
+ }
+}
Index: src/main/java/com/handson/searchengine/kafka/Consumer.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/searchengine/kafka/Consumer.java b/src/main/java/com/handson/searchengine/kafka/Consumer.java
new file mode 100644
--- /dev/null (date 1645314311859)
+++ b/src/main/java/com/handson/searchengine/kafka/Consumer.java (date 1645314311859)
@@ -0,0 +1,31 @@
+package com.handson.searchengine.kafka;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.handson.searchengine.crawler.Crawler;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.kafka.annotation.KafkaListener;
+import org.springframework.stereotype.Component;
+
+import java.util.Optional;
+
+import static com.handson.searchengine.kafka.Producer.APP_TOPIC;
+
+@Component
+public class Consumer {
+
+
+ @KafkaListener(topics = {APP_TOPIC})
+ public void listen(ConsumerRecord<?, ?> record){
+
+ Optional<?> kafkaMessage = Optional.ofNullable(record.value());
+
+ if (kafkaMessage.isPresent()) {
+
+ Object message = kafkaMessage.get();
+ System.out.println("---->" + record);
+ System.out.println("---->" + message);
+
+ }
+ }
+}
Index: src/main/java/com/handson/searchengine/kafka/Producer.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/searchengine/kafka/Producer.java b/src/main/java/com/handson/searchengine/kafka/Producer.java
new file mode 100644
--- /dev/null (date 1645314311862)
+++ b/src/main/java/com/handson/searchengine/kafka/Producer.java (date 1645314311862)
@@ -0,0 +1,24 @@
+package com.handson.searchengine.kafka;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.stereotype.Component;
+
+
+@Component
+public class Producer {
+
+ public static final String APP_TOPIC = "searchengine";
+ @Autowired
+ ObjectMapper om;
+
+ @Autowired
+ private KafkaTemplate kafkaTemplate;
+
+ public void send(Object message) throws JsonProcessingException {
+ kafkaTemplate.send(APP_TOPIC, om.writeValueAsString(message));
+ }
+
+}