-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (93 loc) · 2.49 KB
/
main.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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/Shopify/sarama"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
fmt.Println("Connected")
}
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
fmt.Printf("Connect lost: %v", err)
}
func main() {
startMqttSub()
startKafka()
}
func startMqttSub() {
var broker = "broker.emqx.io"
var port = 1883
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port))
opts.SetClientID("go_mqtt_client")
opts.SetUsername("emqx")
opts.SetPassword("public")
opts.SetDefaultPublishHandler(messagePubHandler)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
fmt.Println("MQTT connected")
sub(client)
client.Disconnect(250)
}
func sub(client mqtt.Client) {
topic := "temperature"
fmt.Println("my topic is", topic)
token := client.Subscribe(topic, 1, nil)
token.Wait()
fmt.Printf("Subscribed to topic: %s \n", topic)
}
func startKafka() {
topic := "mqtt.temperature"
worker, err := connectConsumer([]string{"localhost:9092"})
if err != nil {
panic(err)
}
consumer, err := worker.ConsumePartition(topic, 0, sarama.OffsetOldest)
if err != nil {
panic(err)
}
fmt.Println("Kafka Consumer started ")
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
msgCount := 0
doneCh := make(chan struct{})
go func() {
for {
select {
case err := <-consumer.Errors():
fmt.Println(err)
case msg := <-consumer.Messages():
msgCount++
fmt.Printf("Received message Count %d: | Topic(%s) | Message(%s) \n", msgCount, string(msg.Topic), string(msg.Value))
case <-sigchan:
fmt.Println("Interrupt is detected")
doneCh <- struct{}{}
}
}
}()
<-doneCh
fmt.Println("Processed", msgCount, "messages")
if err := worker.Close(); err != nil {
fmt.Println("err3", err)
panic(err)
}
}
func connectConsumer(brokersUrl []string) (sarama.Consumer, error) {
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
conn, err := sarama.NewConsumer(brokersUrl, config)
if err != nil {
return nil, err
}
return conn, nil
}