-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathmain.go
62 lines (52 loc) · 1.55 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
56
57
58
59
60
61
62
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"fmt"
"log"
cestan "github.com/cloudevents/sdk-go/protocol/stan/v2"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
func main() {
protocol, err := cestan.NewProtocol("test-cluster", "test-client", "send-test-subject", "receiver-test-subject",
cestan.StanOptions())
if err != nil {
log.Fatalf("failed to create protocol: %v", err)
}
defer protocol.Close(context.Background())
c, err := cloudevents.NewClient(protocol, cloudevents.WithTimeNow(), cloudevents.WithUUIDs())
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
// Start the receiver
go func() {
log.Printf("will listen consuming topic test-topic\n")
err = c.StartReceiver(context.TODO(), receive)
if err != nil {
log.Fatalf("failed to start receiver: %s", err)
} else {
log.Printf("receiver stopped\n")
}
}()
// Start sending the events
for i := 0; i < 10; i++ {
e := cloudevents.NewEvent()
e.SetType("com.cloudevents.sample.sent")
e.SetSource("https://github.com/cloudevents/sdk-go/v2/samples/stan/sender_and_receiver")
_ = e.SetData(cloudevents.ApplicationJSON, map[string]interface{}{
"id": i,
"message": "Hello, World!",
})
if result := c.Send(context.Background(), e); cloudevents.IsUndelivered(result) {
log.Printf("failed to send: %v", err)
} else {
log.Printf("sent: %d, accepted: %t", i, cloudevents.IsACK(result))
}
}
}
func receive(ctx context.Context, event cloudevents.Event) {
fmt.Printf("%s", event)
}