-
Notifications
You must be signed in to change notification settings - Fork 20
/
deduplication.go
89 lines (77 loc) · 2.28 KB
/
deduplication.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
package main
import (
"bufio"
"fmt"
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/message"
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
"os"
)
func CheckErr(err error) {
if err != nil {
fmt.Printf("%s ", err)
os.Exit(1)
}
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Deduplication example")
fmt.Println("Connecting to RabbitMQ streaming ...")
env, err := stream.NewEnvironment(
stream.NewEnvironmentOptions().
SetHost("localhost").
SetPort(5552))
CheckErr(err)
streamName := "deduplication"
err = env.DeclareStream(streamName,
stream.NewStreamOptions().SetMaxLengthBytes(stream.ByteCapacity{}.GB(2)))
if err != stream.StreamAlreadyExists {
CheckErr(err)
}
producer, err := env.NewProducer(streamName,
stream.NewProducerOptions().SetProducerName("myProducer")) // producer name is mandatory to handle the deduplication
CheckErr(err)
chConfirm := producer.NotifyPublishConfirmation()
go func(ch stream.ChannelPublishConfirm, p *stream.Producer) {
for messagesStatus := range ch {
for _, messageStatus := range messagesStatus {
if messageStatus.IsConfirmed() {
fmt.Printf("publishingId: %d - Confirmed: %s \n",
/// In this case the PublishingId is the one provided by the user
messageStatus.GetMessage().GetPublishingId(),
messageStatus.GetMessage().GetData()[0])
}
}
}
}(chConfirm, producer)
// In case you need to know which is the last ID for the producer: GetLastPublishingId
lastPublishingId, err := producer.GetLastPublishingId()
CheckErr(err)
fmt.Printf("lastPublishingId: %d\n",
lastPublishingId,
)
data := make(map[int]string)
data[0] = "Piaggio"
data[1] = "Ferrari"
data[2] = "Ducati"
data[3] = "Maserati"
data[4] = "Fiat"
data[5] = "Lamborghini"
data[6] = "Bugatti"
data[7] = "Alfa Romeo"
data[8] = "Aprilia"
data[9] = "Benelli"
for i := 0; i < len(data); i++ {
var msg message.StreamMessage
msg = amqp.NewMessage([]byte(data[i]))
msg.SetPublishingId(int64(i)) // mandatory to handle the deduplication
err := producer.Send(msg)
CheckErr(err)
}
fmt.Println("Press any key to stop ")
_, _ = reader.ReadString('\n')
err = producer.Close()
CheckErr(err)
err = env.DeleteStream(streamName)
CheckErr(err)
}