-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathoffset_tracking_send.go
74 lines (65 loc) · 1.54 KB
/
offset_tracking_send.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
package main
import (
"fmt"
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
"os"
)
func main() {
env, err := stream.NewEnvironment(
stream.NewEnvironmentOptions().
SetHost("localhost").
SetPort(5552).
SetUser("guest").
SetPassword("guest"))
CheckErrSend(err)
streamName := "stream-offset-tracking-go"
err = env.DeclareStream(streamName,
&stream.StreamOptions{
MaxLengthBytes: stream.ByteCapacity{}.GB(2),
},
)
CheckErrSend(err)
producer, err := env.NewProducer(streamName, stream.NewProducerOptions())
CheckErrSend(err)
messageCount := 100
chPublishConfirm := producer.NotifyPublishConfirmation()
ch := make(chan bool)
handlePublishConfirm(chPublishConfirm, messageCount, ch)
fmt.Printf("Publishing %d messages...\n", messageCount)
for i := 0; i < messageCount; i++ {
var body string
if i == messageCount-1 {
body = "marker"
} else {
body = "hello"
}
err = producer.Send(amqp.NewMessage([]byte(body)))
CheckErrSend(err)
}
_ = <-ch
fmt.Println("Messages confirmed.")
err = producer.Close()
CheckErrSend(err)
}
func handlePublishConfirm(confirms stream.ChannelPublishConfirm, messageCount int, ch chan bool) {
go func() {
confirmedCount := 0
for confirmed := range confirms {
for _, msg := range confirmed {
if msg.IsConfirmed() {
confirmedCount++
if confirmedCount == messageCount {
ch <- true
}
}
}
}
}()
}
func CheckErrSend(err error) {
if err != nil {
fmt.Printf("%s ", err)
os.Exit(1)
}
}