-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
59 lines (48 loc) · 1.4 KB
/
consumer.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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <config-file-path>\n",
os.Args[0])
os.Exit(1)
}
configFile := os.Args[1]
conf := ReadConfig(configFile)
conf["group.id"] = "kafka-go-getting-started"
conf["auto.offset.reset"] = "earliest"
c, err := kafka.NewConsumer(&conf)
if err != nil {
fmt.Printf("Failed to create consumer: %s", err)
os.Exit(1)
}
topic := "purchases"
err = c.SubscribeTopics([]string{topic}, nil)
// Set up a channel for handling Ctrl-C, etc
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
// Process messages
run := true
for run {
select {
case sig := <-sigchan:
fmt.Printf("Caught signal %v: terminating\n", sig)
run = false
default:
ev, err := c.ReadMessage(100 * time.Millisecond)
if err != nil {
// Errors are informational and automatically handled by the consumer
continue
}
fmt.Printf("Consumed event from topic %s: key = %-10s value = %s\n",
*ev.TopicPartition.Topic, string(ev.Key), string(ev.Value))
}
}
c.Close()
}