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
/
message_browse_example_test.go
163 lines (145 loc) · 3.3 KB
/
message_browse_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
package servicebus_test
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/Azure/azure-service-bus-go"
)
type (
Scientist struct {
Surname string `json:"surname,omitempty"`
FirstName string `json:"firstname,omitempty"`
}
)
func Example_messageBrowse() {
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
}
qm := ns.NewQueueManager()
qEntity, err := ensureQueue(ctx, qm, "MessageBrowseExample")
if err != nil {
fmt.Println(err)
return
}
q, err := ns.NewQueue(qEntity.Name)
if err != nil {
fmt.Println(err)
return
}
txRxCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
go sendMessages(txRxCtx, q)
time.Sleep(1 * time.Second) // wait a second to ensure a message has landed in the queue
go peekMessages(txRxCtx, q)
<-txRxCtx.Done() // wait for the context to finish
// Output:
// Firstname: Albert, Surname: Einstein
// Firstname: Werner, Surname: Heisenberg
// Firstname: Marie, Surname: Curie
// Firstname: Steven, Surname: Hawking
// Firstname: Isaac, Surname: Newton
// Firstname: Niels, Surname: Bohr
// Firstname: Michael, Surname: Faraday
// Firstname: Galileo, Surname: Galilei
// Firstname: Johannes, Surname: Kepler
// Firstname: Nikolaus, Surname: Kopernikus
}
func sendMessages(ctx context.Context, q *servicebus.Queue) {
scientists := []Scientist{
{
Surname: "Einstein",
FirstName: "Albert",
},
{
Surname: "Heisenberg",
FirstName: "Werner",
},
{
Surname: "Curie",
FirstName: "Marie",
},
{
Surname: "Hawking",
FirstName: "Steven",
},
{
Surname: "Newton",
FirstName: "Isaac",
},
{
Surname: "Bohr",
FirstName: "Niels",
},
{
Surname: "Faraday",
FirstName: "Michael",
},
{
Surname: "Galilei",
FirstName: "Galileo",
},
{
Surname: "Kepler",
FirstName: "Johannes",
},
{
Surname: "Kopernikus",
FirstName: "Nikolaus",
},
}
for _, scientist := range scientists {
bits, err := json.Marshal(scientist)
if err != nil {
fmt.Println(err)
return
}
ttl := 2 * time.Minute
msg := servicebus.NewMessage(bits)
msg.ContentType = "application/json"
msg.TTL = &ttl
if err := q.Send(ctx, msg); err != nil {
fmt.Println(err)
return
}
}
}
func peekMessages(ctx context.Context, q *servicebus.Queue) {
var opts []servicebus.PeekOption
for {
select {
case <-ctx.Done():
return
default:
msg, err := q.PeekOne(ctx, opts...)
if err != nil {
switch err.(type) {
case servicebus.ErrNoMessages:
// all done
return
default:
fmt.Println(err)
return
}
}
var scientist Scientist
if err := json.Unmarshal(msg.Data, &scientist); err != nil {
fmt.Println(err)
return
}
opts = []servicebus.PeekOption{servicebus.PeekFromSequenceNumber(*msg.SystemProperties.SequenceNumber)}
fmt.Printf("Firstname: %s, Surname: %s\n", scientist.FirstName, scientist.Surname)
}
}
}