-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (57 loc) · 1.35 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
package main
import (
"fmt"
"os"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
type TransactionPlacer struct {
producer *kafka.Producer
topic string
deliverch chan kafka.Event
}
func NewTransactionPlacer(p *kafka.Producer, topic string) *TransactionPlacer {
return &TransactionPlacer{
producer: p,
topic: topic,
deliverch: make(chan kafka.Event, 10000),
}
}
func (r *TransactionPlacer) placeTransaction(orderType string, size int) error {
var (
format = fmt.Sprintf("%s - %d", orderType, size)
payload = []byte(format)
)
err := r.producer.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &r.topic,
Partition: kafka.PartitionAny,
},
Value: payload,
}, r.deliverch)
if err != nil {
fmt.Printf("Err: %v\n", err)
}
<-r.deliverch
fmt.Printf("placed transaction on the queue %s\n", format)
return nil
}
func main() {
p, err := kafka.NewProducer(&kafka.ConfigMap{
"bootstrap.servers": "localhost:9092",
"client.id": "blackboard",
"acks": "all",
})
if err != nil {
fmt.Printf("Failed to create producer: %v\n", err)
os.Exit(1)
}
const topic = "TRANSACTION"
t := NewTransactionPlacer(p, topic)
for i := 0; i < 100; i++ {
if err := t.placeTransaction("transaction", i+1); err != nil {
fmt.Printf("Err: %v\n", err)
}
time.Sleep(time.Second * 1)
}
}