This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
priority_subscription_example_test.go
201 lines (175 loc) · 4.19 KB
/
priority_subscription_example_test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package servicebus_test
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
servicebus "github.com/Azure/azure-service-bus-go"
)
type PrioritySubscription struct {
Name string
Expression string
MessageCount int
}
type PriorityMessage struct {
Body string
Priority int
}
type PriorityPrinter struct {
SubName string
}
func (pp PriorityPrinter) Handle(ctx context.Context, msg *servicebus.Message) error {
i, ok := msg.UserProperties["Priority"].(int64)
if !ok {
fmt.Println("Priority is not an int64")
}
fmt.Println(strings.Join([]string{pp.SubName, string(msg.Data), strconv.Itoa(int(i))}, "_"))
return msg.Complete(ctx)
}
func Example_prioritySubscriptions() {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
connStr := os.Getenv("SERVICEBUS_CONNECTION_STRING")
if connStr == "" {
fmt.Println("FATAL: expected environment variable SERVICEBUS_CONNECTION_STRING not set")
return
}
// Create a client to communicate with a Service Bus Namespace.
ns, err := servicebus.NewNamespace(servicebus.NamespaceWithConnectionString(connStr))
if err != nil {
fmt.Println(err)
return
}
// build the topic for sending priority messages
tm := ns.NewTopicManager()
topicEntity, err := ensureTopic(ctx, tm, "PrioritySubscriptionsTopic")
if err != nil {
fmt.Println(err)
return
}
sm, err := ns.NewSubscriptionManager(topicEntity.Name)
if err != nil {
fmt.Println(err)
return
}
// build each priority subscription providing each with a SQL like expression to filter messages from the topic
prioritySubs := []PrioritySubscription{
{
Name: "Priority1",
Expression: "user.Priority=1",
MessageCount: 1,
},
{
Name: "Priority2",
Expression: "user.Priority=2",
MessageCount: 1,
},
{
Name: "PriorityGreaterThan2",
Expression: "user.Priority>2",
MessageCount: 2,
},
}
for _, s := range prioritySubs {
subEntity, err := ensureSubscription(ctx, sm, s.Name)
if err != nil {
fmt.Println(err)
return
}
// remove the default rule, which is the "TrueFilter" that accepts all messages
err = sm.DeleteRule(ctx, subEntity.Name, "$Default")
if err != nil {
fmt.Println(err)
return
}
_, err = sm.PutRule(ctx, subEntity.Name, s.Name+"Rule", servicebus.SQLFilter{Expression: s.Expression})
if err != nil {
fmt.Println(err)
return
}
}
priorityMessages := []PriorityMessage{
{
Body: "foo",
Priority: 1,
},
{
Body: "bar",
Priority: 2,
},
{
Body: "bazz",
Priority: 3,
},
{
Body: "buzz",
Priority: 4,
},
}
topic, err := ns.NewTopic(topicEntity.Name)
if err != nil {
fmt.Println(err)
return
}
defer func() {
_ = topic.Close(ctx)
}()
for _, pMessage := range priorityMessages {
msg := servicebus.NewMessageFromString(pMessage.Body)
msg.UserProperties = map[string]interface{}{"Priority": pMessage.Priority}
if err := topic.Send(ctx, msg); err != nil {
fmt.Println(err)
return
}
}
for _, s := range prioritySubs {
sub, err := topic.NewSubscription(s.Name)
if err != nil {
fmt.Println(err)
return
}
for i := 0; i < s.MessageCount; i++ {
err := sub.ReceiveOne(ctx, PriorityPrinter{SubName: sub.Name})
if err != nil {
fmt.Println(err)
return
}
}
err = sub.Close(ctx)
if err != nil {
fmt.Println(err)
return
}
}
// Output:
// Priority1_foo_1
// Priority2_bar_2
// PriorityGreaterThan2_bazz_3
// PriorityGreaterThan2_buzz_4
}
func ensureTopic(ctx context.Context, tm *servicebus.TopicManager, name string, opts ...servicebus.TopicManagementOption) (*servicebus.TopicEntity, error) {
_, err := tm.Get(ctx, name)
if err == nil {
_ = tm.Delete(ctx, name)
}
te, err := tm.Put(ctx, name, opts...)
if err != nil {
fmt.Println(err)
return nil, err
}
return te, nil
}
func ensureSubscription(ctx context.Context, sm *servicebus.SubscriptionManager, name string, opts ...servicebus.SubscriptionManagementOption) (*servicebus.SubscriptionEntity, error) {
_, err := sm.Get(ctx, name)
if err == nil {
_ = sm.Delete(ctx, name)
}
subEntity, err := sm.Put(ctx, name, opts...)
if err != nil {
fmt.Println(err)
return nil, err
}
return subEntity, nil
}