-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKafkaConsumer.java
59 lines (47 loc) · 1.93 KB
/
KafkaConsumer.java
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
package com.test.spring.boot.service;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.common.PartitionInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;
@Service
public class KafkaConsumer {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private ConcurrentKafkaListenerContainerFactory<String, String> listnerContainerFactory;
//
/**
* Consumes from <TopicName>
*
* @Header refers to the custom header of the message. Since, Kafka is agnostic
* to the message types. Means, what ever you send into the topic, it
* sends as String/JSON/AVRO messages. but for handler/Listner/consumer,
* it may important to know what kind of message its going to handle. In
* order to specify, the message type, we can use {@link RecordHeader}
* to add custom Headers. Here, EVENT_TYPE is used as the custom header
* and the value would bet event type class.
*
*
* @param message
* @param payloadType
*/
@KafkaListener(topics = <TopicName String>)
public void consume(@Payload String message, @Header("EVENT_TYPE") String payloadType) {
System.out.println("payload Type : [" + payloadType + "] Consumed message: " + message);
}
public Set<String> getAllTopics() {
try (Consumer<String, String> consumer = (Consumer<String, String>) listnerContainerFactory.getConsumerFactory()
.createConsumer()) {
Map<String, List<PartitionInfo>> map = consumer.listTopics();
return map.keySet();
}
}
}