-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (71 loc) · 2.06 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"context"
"fmt"
"log"
"github.com/apache/pulsar/pulsar-client-go/pulsar"
)
func main() {
// Instantiate a Pulsar client
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://localhost:6650",
})
if err != nil {
fmt.Println("Could not create client:", err)
}
defer client.Close()
// Use the client to instantiate a producer
producer, err := client.CreateProducer(pulsar.ProducerOptions{
Topic: "temperature",
})
if err != nil {
fmt.Println("Could not create producer:", err)
}
ctx := context.Background()
// Send 10 messages synchronously and 10 messages asynchronously
for i := 0; i < 10; i++ {
// Create a message
msg := pulsar.ProducerMessage{
Payload: []byte(fmt.Sprintf("message-%d", i)),
}
// Attempt to send the message
if err := producer.Send(ctx, msg); err != nil {
fmt.Println("Could not send message:", err)
}
fmt.Printf("the %s successfully published", string(msg.Payload))
// Create a different message to send asynchronously
asyncMsg := pulsar.ProducerMessage{
Payload: []byte(fmt.Sprintf("async-message-%d", i)),
}
// Attempt to send the message asynchronously and handle the response
producer.SendAsync(ctx, asyncMsg, func(msg pulsar.ProducerMessage, err error) {
if err != nil {
fmt.Println("Could not send message async:", err)
}
fmt.Printf("the %s successfully published", string(msg.Payload))
})
}
defer producer.Close()
// Consumer channel
msgChannel := make(chan pulsar.ConsumerMessage)
consumerOpts := pulsar.ConsumerOptions{
Topic: "temperature",
SubscriptionName: "my-subscription-1",
Type: pulsar.Exclusive,
MessageChannel: msgChannel,
}
consumer, err := client.Subscribe(consumerOpts)
if err != nil {
fmt.Println("Could not create subscription:", err)
}
defer consumer.Close()
for cm := range msgChannel {
msg := cm.Message
fmt.Printf("Message ID: %s", msg.ID())
fmt.Printf("Message value: %s", string(msg.Payload()))
consumer.Ack(msg)
}
if err := consumer.Unsubscribe(); err != nil {
log.Fatal(err)
}
}