-
Notifications
You must be signed in to change notification settings - Fork 874
/
Copy pathexample_producerclient_test.go
122 lines (93 loc) · 3.35 KB
/
example_producerclient_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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azeventhubs_test
import (
"context"
"errors"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs"
)
var producerClient *azeventhubs.ProducerClient
func ExampleNewProducerClient() {
defaultAzureCred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}
producerClient, err = azeventhubs.NewProducerClient("<ex: myeventhubnamespace.servicebus.windows.net>", "eventhub-name", defaultAzureCred, nil)
if err != nil {
panic(err)
}
}
func ExampleNewProducerClientFromConnectionString() {
// if the connection string contains an EntityPath
//
connectionString := "Endpoint=sb://<your-namespace>.servicebus.windows.net/;SharedAccessKeyName=<key-name>;SharedAccessKey=<key>;EntityPath=<entity path>"
producerClient, err = azeventhubs.NewProducerClientFromConnectionString(connectionString, "", nil)
// or
// if the connection string does not contain an EntityPath
connectionString = "Endpoint=sb://<your-namespace>.servicebus.windows.net/;SharedAccessKeyName=<key-name>;SharedAccessKey=<key>"
producerClient, err = azeventhubs.NewProducerClientFromConnectionString(connectionString, "eventhub-name", nil)
if err != nil {
panic(err)
}
}
func ExampleProducerClient_SendEventBatch() {
batch, err := producerClient.NewEventDataBatch(context.TODO(), nil)
if err != nil {
panic(err)
}
// See ExampleProducerClient_AddEventData for more information.
err = batch.AddEventData(&azeventhubs.EventData{Body: []byte("hello")}, nil)
if err != nil {
panic(err)
}
err = producerClient.SendEventBatch(context.TODO(), batch, nil)
if err != nil {
panic(err)
}
}
func ExampleEventDataBatch_AddEventData() {
batch, err := producerClient.NewEventDataBatch(context.TODO(), nil)
if err != nil {
panic(err)
}
// can be called multiple times with new messages until you
// receive an azeventhubs.ErrMessageTooLarge
err = batch.AddEventData(&azeventhubs.EventData{
Body: []byte("hello"),
}, nil)
if errors.Is(err, azeventhubs.ErrEventDataTooLarge) {
// Message was too large to fit into this batch.
//
// At this point you'd usually just send the batch (using ProducerClient.SendEventBatch),
// create a new one, and start filling up the batch again.
//
// If this is the _only_ message being added to the batch then it's too big in general, and
// will need to be split or shrunk to fit.
panic(err)
} else if err != nil {
panic(err)
}
err = producerClient.SendEventBatch(context.TODO(), batch, nil)
if err != nil {
panic(err)
}
}
func ExampleProducerClient_GetEventHubProperties() {
eventHubProps, err := producerClient.GetEventHubProperties(context.TODO(), nil)
if err != nil {
panic(err)
}
for _, partitionID := range eventHubProps.PartitionIDs {
fmt.Printf("Partition ID: %s\n", partitionID)
}
}
func ExampleProducerClient_GetPartitionProperties() {
partitionProps, err := producerClient.GetPartitionProperties(context.TODO(), "partition-id", nil)
if err != nil {
panic(err)
}
fmt.Printf("First sequence number for partition ID %s: %d\n", partitionProps.PartitionID, partitionProps.BeginningSequenceNumber)
fmt.Printf("Last sequence number for partition ID %s: %d\n", partitionProps.PartitionID, partitionProps.LastEnqueuedSequenceNumber)
}