-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_pull.go
More file actions
55 lines (46 loc) · 1.48 KB
/
example_pull.go
File metadata and controls
55 lines (46 loc) · 1.48 KB
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 example
import (
"context"
"log"
"github.com/google/uuid"
"github.com/stackitcloud/pubsub-sdk-go/pkg/pubsub"
"github.com/stackitcloud/stackit-sdk-go/core/auth"
"github.com/stackitcloud/stackit-sdk-go/core/config"
)
//nolint:all
func pull() {
// Authentication with STACKIT SDK - Returns a Round-Tripper
rt, err := auth.DefaultAuth(&config.Configuration{
ServiceAccountKeyPath: "./service-account-key.json",
})
if err != nil {
log.Fatalf("Error creating authentication token: %v", err)
}
// Setup your TopicID and Subscription ID
//TODO: ID's needs to be replaced here
topicID := uuid.MustParse("00000000-0000-0000-0000-000000000000")
subscriptionID := uuid.MustParse("00000000-0000-0000-0000-000000000000")
// Declare subscriber
subscriber := pubsub.NewSubscriber(topicID,
subscriptionID,
pubsub.WithHTTPRoundTripper(rt),
)
// Pull messages via subscription
pulledMessages, err := subscriber.Pull(context.Background(), pubsub.WithMaxMessages(10))
if err != nil {
log.Fatalf("Error pulling messages: %v", err)
}
log.Printf("Successfully pulled message: %v", pulledMessages)
// Get your AckIDs and acknowledge them
ackIDs := pulledMessages.GetAckIDs()
err = subscriber.Ack(context.Background(), ackIDs)
if err != nil {
log.Fatalf("Error ack ids: %v", err)
}
// Get your NackIDs and not acknowledge them
nackIDs := pulledMessages.GetAckIDs()
err = subscriber.Nack(context.Background(), nackIDs)
if err != nil {
log.Fatalf("Error nack ids: %v", err)
}
}