-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathproducer.go
57 lines (48 loc) · 1.01 KB
/
producer.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/signal"
"qbus"
)
func main() {
if len(os.Args) < 4 {
fmt.Printf("Usage: ./producer config_path topic_name cluster_name\n")
return
}
configPath := os.Args[1]
topicName := os.Args[2]
clusterName := os.Args[3]
producer := qbus.NewQbusProducer()
defer qbus.DeleteQbusProducer(producer)
if !producer.Init(clusterName, "producer.log", configPath, topicName) {
fmt.Println("Init failed")
os.Exit(1)
}
defer producer.Uninit()
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt)
go func() {
reader := bufio.NewReader(os.Stdin)
running := true
fmt.Println("%% Please input messages (Press Ctrl+C or Ctrl+D to exit):")
for running {
msg, err := reader.ReadString('\n')
switch err {
case nil:
if !producer.Produce(msg, int64(len(msg))-1, "") {
fmt.Println("Failed to Produce")
}
case io.EOF:
done <- os.Interrupt
running = false
default:
panic(err)
}
}
}()
<-done
fmt.Println("%% Done.")
}