-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainSenderApp.go
73 lines (61 loc) · 2.07 KB
/
mainSenderApp.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 (
"fmt"
"github.com/openenergi/go-event-hub/eventhub"
"log"
"os"
"time"
)
var (
ehNamespace = os.Getenv("EH_TEST_NAMESPACE")
ehName = os.Getenv("EH_TEST_NAME")
sasPolicyName = os.Getenv("EH_TEST_SAS_POLICY_NAME")
sasPolicyKey = os.Getenv("EH_TEST_SAS_POLICY_KEY")
)
func main() {
eventhub.Logger = log.New(os.Stdout, "[Event Hub] ", log.LstdFlags)
appLogger := log.New(os.Stdout, "[Eh Sender] ", log.LstdFlags)
ehSender, err := eventhub.NewSender(eventhub.SenderOpts{
EventHubNamespace: ehNamespace,
EventHubName: ehName,
SasPolicyName: sasPolicyName,
SasPolicyKey: sasPolicyKey,
TokenExpiryInterval: 20 * time.Second,
Debug: true,
})
if err != nil {
panic(err)
}
defer ehSender.Close()
go func(s eventhub.Sender) {
appLogger.Printf("Setting up the error channel...\n")
for err := range s.ErrorChan() {
if err != nil {
appLogger.Printf("Just received an error: '%v'\n", err)
panic(err)
}
}
}(ehSender)
appLogger.Printf("Now sending a message!\n")
thisMessage := fmt.Sprintf("Sending just ONE at timestamp %v", time.Now())
// TODO change the sleep with a "quit channel"
time.Sleep(2 * time.Second)
// 1) send sync
// ------------
uniqueID, err := ehSender.Send(thisMessage, map[string]interface{}{})
if err != nil {
appLogger.Printf("!!! error sending '%v', the error message is: %v\n", thisMessage, err)
}
appLogger.Printf("The message was identified by Send with this ID: %d\n", uniqueID)
// TODO change the sleep with a "quit channel"
time.Sleep(2 * time.Second)
// 2) send async (wait forever for ACK)
// ------------------------------------
uniqueID = ehSender.SendAsync(thisMessage, map[string]interface{}{})
appLogger.Printf("The message was identified by SendAsync with this ID: %d\n", uniqueID)
// 3) send async with timeout
// --------------------------
uniqueID = ehSender.SendAsyncTimeout(thisMessage, map[string]interface{}{}, 2*time.Second)
appLogger.Printf("The message was identified by SendAsyncTimeout with this ID: %d\n", uniqueID)
select {}
}