-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (67 loc) · 1.67 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
81
82
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/apache/pulsar-client-go/pulsar"
pulsarLog "github.com/apache/pulsar-client-go/pulsar/log"
)
type (
Config struct {
PulsarURI string
Topic string
}
)
func main() {
var config Config
pulsarURI, isPulsarURISet := os.LookupEnv("PULSAR_URI")
if !isPulsarURISet {
log.Fatal("Pulsar URI not set")
}
topic, isTopicSet := os.LookupEnv("TOPIC")
if !isTopicSet {
log.Fatal("Pulsar Topic not set")
}
config.PulsarURI = pulsarURI
config.Topic = topic
var client pulsar.Client
var clientErr error
// Open Pulsar cluster
client, clientErr = pulsar.NewClient(pulsar.ClientOptions{
URL: config.PulsarURI,
OperationTimeout: 30 * time.Second,
ConnectionTimeout: 30 * time.Second,
Logger: pulsarLog.DefaultNopLogger(),
})
if clientErr != nil {
log.Fatal("Error creating client - TERMINATING", clientErr)
}
defer client.Close()
producer, err := client.CreateProducer(pulsar.ProducerOptions{
Topic: config.Topic,
})
if err != nil {
log.Fatal("Error creating producer - TERMINATING", err)
}
defer producer.Close()
log.Println("Sending messages")
for i := 0; i < 50; i++ {
producer.SendAsync(
context.Background(),
&pulsar.ProducerMessage{
Payload: []byte(fmt.Sprintf("{\"name\":\"cgiacomi\",\"timestamp\":%d}", time.Now().Unix())),
},
func(id pulsar.MessageID, message *pulsar.ProducerMessage, err error) {
if err != nil {
log.Printf("error producing message: %v\n", err)
}
fmt.Printf("successfully produced message: %s\n", message.Payload)
})
}
err = producer.Flush()
if err != nil {
log.Fatal("Error flushing producer")
}
}