-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
92 lines (80 loc) · 1.8 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/sceneryback/kafka-best-practices/consumer"
"github.com/sceneryback/kafka-best-practices/producer"
)
const (
ProduceMode = "produce"
SyncMode = "sync"
BatchMode = "batch"
MultiAsyncMode = "multiAsync"
MultiBatchMode = "multiBatch"
)
var mode string
var broker string
func init() {
flag.StringVar(&mode, "m", "", "cmd mode, 'produce', 'sync', 'batch' or 'multiBatch'")
flag.StringVar(&broker, "h", "127.0.0.1:9092", "kafka broker host:port")
}
func main() {
flag.Parse()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
if mode == "" {
flag.Usage()
return
}
var topic = "test-practice-topic"
var done = make(chan struct{})
defer close(done)
switch mode {
case ProduceMode:
producer, err := producer.NewProducer(broker)
if err != nil {
panic(err)
}
defer producer.Close()
go producer.StartProduce(done, topic)
case SyncMode:
// 1. sync consumer
consumer, err := consumer.StartSyncConsumer(broker, topic)
if err != nil {
panic(err)
}
defer consumer.Close()
case BatchMode:
// 2. batch consumer
consumer, err := consumer.StartBatchConsumer(broker, topic)
if err != nil {
panic(err)
}
defer consumer.Close()
case MultiAsyncMode:
// 3. multi async consumer
consumer, err := consumer.StartMultiAsyncConsumer(broker, topic)
if err != nil {
panic(err)
}
defer consumer.Close()
case MultiBatchMode:
// 4. multi batch consumer
consumer, err := consumer.StartMultiBatchConsumer(broker, topic)
if err != nil {
panic(err)
}
defer consumer.Close()
default:
flag.Usage()
return
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
fmt.Println("received signal", <-c)
}