-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathoffset_tracking_receive.go
73 lines (66 loc) · 1.96 KB
/
offset_tracking_receive.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
package main
import (
"errors"
"fmt"
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
"os"
"sync/atomic"
)
func main() {
env, err := stream.NewEnvironment(
stream.NewEnvironmentOptions().
SetHost("localhost").
SetPort(5552).
SetUser("guest").
SetPassword("guest"))
CheckErrReceive(err)
streamName := "stream-offset-tracking-go"
err = env.DeclareStream(streamName,
&stream.StreamOptions{
MaxLengthBytes: stream.ByteCapacity{}.GB(2),
},
)
CheckErrReceive(err)
var firstOffset int64 = -1
var messageCount int64 = -1
var lastOffset atomic.Int64
ch := make(chan bool)
messagesHandler := func(consumerContext stream.ConsumerContext, message *amqp.Message) {
if atomic.CompareAndSwapInt64(&firstOffset, -1, consumerContext.Consumer.GetOffset()) {
fmt.Println("First message received.")
}
if atomic.AddInt64(&messageCount, 1)%10 == 0 {
_ = consumerContext.Consumer.StoreOffset()
}
if string(message.GetData()) == "marker" {
lastOffset.Store(consumerContext.Consumer.GetOffset())
_ = consumerContext.Consumer.StoreOffset()
_ = consumerContext.Consumer.Close()
ch <- true
}
}
var offsetSpecification stream.OffsetSpecification
consumerName := "offset-tracking-tutorial"
storedOffset, err := env.QueryOffset(consumerName, streamName)
if errors.Is(err, stream.OffsetNotFoundError) {
offsetSpecification = stream.OffsetSpecification{}.First()
} else {
offsetSpecification = stream.OffsetSpecification{}.Offset(storedOffset + 1)
}
_, err = env.NewConsumer(streamName, messagesHandler,
stream.NewConsumerOptions().
SetManualCommit().
SetConsumerName(consumerName).
SetOffset(offsetSpecification))
CheckErrReceive(err)
fmt.Println("Started consuming...")
_ = <-ch
fmt.Printf("Done consuming, first offset %d, last offset %d.\n", firstOffset, lastOffset.Load())
}
func CheckErrReceive(err error) {
if err != nil {
fmt.Printf("%s ", err)
os.Exit(1)
}
}