forked from Azure/azure-service-bus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (48 loc) · 1.15 KB
/
main.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
package main
import (
"bufio"
"context"
"fmt"
"os"
"time"
"github.com/Azure/azure-service-bus-go"
)
func main() {
// Connect
connStr := mustGetenv("SERVICEBUS_CONNECTION_STRING")
ns, err := servicebus.NewNamespace(servicebus.NamespaceWithConnectionString(connStr))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
queueName := "helloworld"
q, err := getQueue(ns, queueName)
if err != nil {
fmt.Printf("failed to build a new queue named %q\n", queueName)
os.Exit(1)
}
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
q.Send(ctx, servicebus.NewMessageFromString(text))
if text == "exit\n" {
break
}
cancel()
}
}
func getQueue(ns *servicebus.Namespace, queueName string) (*servicebus.Queue, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
q, err := ns.NewQueue(ctx, queueName)
return q, err
}
func mustGetenv(key string) string {
v := os.Getenv(key)
if v == "" {
panic("Environment variable '" + key + "' required for integration tests.")
}
return v
}