-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.go
124 lines (109 loc) · 3.76 KB
/
kafka.go
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
116
117
118
119
120
121
122
123
124
package abango
import (
"fmt"
"strings"
"sync"
"github.com/IBM/sarama"
e "github.com/dabory/abango-rest/etc"
)
var (
KAFKA_CONN string
COMSUMER_TOPICS []string
KAFKA_TIMEOUT string
)
func KafkaInit() {
KAFKA_CONN = XConfig["KafkaConnString"]
COMSUMER_TOPICS = strings.Split(strings.Replace(XConfig["ConsumerTopics"], " ", "", -1), ",")
KAFKA_TIMEOUT = XConfig["KafkaTimeout"]
e.OkLog("== KAFKA_CONN is : " + KAFKA_CONN + " ==")
e.OkLog("== COMSUMER_TOPICS is : " + XConfig["ConsumerTopics"] + " ==")
e.OkLog("== KAFKA_TIMEOUT is : " + KAFKA_TIMEOUT + " ==")
}
func KafkaProducer(key string, headers []*sarama.RecordHeader, message []byte, conCurr string, topic string) (int32, int64, error) {
kfcf := sarama.NewConfig()
kfcf.Producer.Retry.Max = 5
kfcf.Producer.RequiredAcks = sarama.WaitForAll
kfcf.Producer.Return.Successes = true
conHeaders := e.ConvertKafkaHeaders(headers)
if conCurr == "async" {
if prd, err := sarama.NewAsyncProducer([]string{KAFKA_CONN}, kfcf); err == nil {
prd.Input() <- &sarama.ProducerMessage{
Topic: topic,
Key: sarama.StringEncoder(key),
Headers: conHeaders,
Value: sarama.ByteEncoder(message),
}
return 0, 0, nil
} else {
return 0, 0, e.MyErr("QEJHDRTTRRW-Kafka-NewAsyncProducer-End", err, true)
}
} else if conCurr == "sync" {
fmt.Println("conCurr:", conCurr)
if prd, err := sarama.NewSyncProducer([]string{KAFKA_CONN}, kfcf); err == nil {
msg := &sarama.ProducerMessage{
Topic: topic,
Key: sarama.StringEncoder(key),
Headers: conHeaders,
Value: sarama.ByteEncoder(message),
}
if part, offset, err := prd.SendMessage(msg); err == nil {
return part, offset, nil
} else {
return 0, 0, e.MyErr("QEJIOPRTRRTRRW-Kafka-Sync-SendMessage", err, true)
}
} else {
return 0, 0, e.MyErr("QEJHGTRSDRTTRRW-Kafka-NewSyncProducer-End", err, true)
}
} else {
return 0, 0, e.MyErr("QEJHGTRSW-Kafka-ApiMethod Not available-End", nil, true)
}
}
func KafkaConsumer(ConsumeHandler func(msg *sarama.ConsumerMessage), topic string) {
// Create a new configuration for the consumer
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
// Specify the list of brokers
brokers := []string{KAFKA_CONN}
// Create a new consumer
consumer, err := sarama.NewConsumer(brokers, config)
if err != nil {
e.OkLog("Failed to create consumer of topic : " + topic + " == : " + err.Error())
}
defer func() {
if err := consumer.Close(); err != nil {
e.OkLog("Error closing consumer: of topic : " + topic + " == : " + err.Error())
}
}()
// Consume messages from each partition asynchronously
partitions, err := consumer.Partitions(topic)
if err != nil {
e.OkLog("Failed to get partitions: " + topic + " == : " + err.Error())
}
var wg sync.WaitGroup
wg.Add(len(partitions))
for _, partition := range partitions {
go func(partition int32) {
defer wg.Done()
// Create a new partition consumer
partitionConsumer, err := consumer.ConsumePartition(topic, partition, sarama.OffsetNewest)
if err != nil {
e.OkLog("Failed to create partition for consumer: " + topic + " partition: " + e.NumToStr(partition) + " " + err.Error())
return
}
defer func() {
if err := partitionConsumer.Close(); err != nil {
e.OkLog("Error closing partition for consumer: " + topic + " partition: " + e.NumToStr(partition) + " " + err.Error())
}
}()
// Process messages
for msg := range partitionConsumer.Messages() {
ConsumeHandler(msg)
e.OkLog("Consuming topic: " + topic + " partition: " + e.NumToStr(partition))
// log.Printf("Partition-kk %d | Offset %d | Key: %s | Value: %s", message.Partition, message.Offset, string(message.Key), string(message.Value))
}
}(partition)
}
// Wait for the consumer to finish
wg.Wait()
// }
}