-
Notifications
You must be signed in to change notification settings - Fork 0
/
PulsarLib.go
165 lines (145 loc) · 3.65 KB
/
PulsarLib.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package PulsarLib
import (
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"strconv"
"time"
"github.com/apache/pulsar-client-go/pulsar"
)
type MessageResponse struct {
Id string `json:"id"`
Msg string `json:"msg"`
}
func NewRequest(msg string) *MessageResponse {
m := MessageResponse{Msg: msg}
m.Id = ""
return &m
}
//Default configuration URL: "pulsar://localhost:6650"
func InitClient(URL string) *pulsar.Client {
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: URL,
OperationTimeout: 30 * time.Second,
ConnectionTimeout: 30 * time.Second,
Authentication: pulsar.NewAuthenticationToken("yJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"),
})
if err != nil {
log.Fatalf("Could not instantiate Pulsar client: %v", err)
}
return &client
}
func WaitEvent(client *pulsar.Client, topic string, foo func([]byte)) {
go func() {
consumer, err := (*client).Subscribe(pulsar.ConsumerOptions{
Topic: topic,
SubscriptionName: topic,
Type: pulsar.Exclusive,
})
if err != nil {
log.Fatal(err)
}
defer consumer.Close()
for {
msg, err := consumer.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
go foo(msg.Payload())
consumer.Ack(msg)
}
//Destroy consumer
if err := consumer.Unsubscribe(); err != nil {
log.Fatal(err)
}
}()
}
func SendRequestSync(client *pulsar.Client, topic string, message *MessageResponse) []byte {
id := rand.Intn(10000)
topicId := topic + "." + strconv.Itoa(id)
message.Id = topicId
msg, _ := json.Marshal(message)
consumer, err := (*client).Subscribe(pulsar.ConsumerOptions{
Topic: topicId,
SubscriptionName: topicId,
Type: pulsar.Exclusive,
})
if err != nil {
log.Fatal(err)
}
producer, err := (*client).CreateProducer(pulsar.ProducerOptions{
Topic: topic,
})
if err != nil {
log.Fatal(err)
}
_, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
Payload: msg,
})
if err != nil {
fmt.Println("Failed to publish message", err)
}
fmt.Println("Published message" + string(msg))
res, err := consumer.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
//Destroy consumer
if err := consumer.Unsubscribe(); err != nil {
log.Fatal(err)
}
return res.Payload()
}
func SendRequestAsync(client *pulsar.Client, topic string, message *MessageResponse, foo func([]byte)) {
id := rand.Intn(10000)
topicId := topic + "." + strconv.Itoa(id)
message.Id = topicId
msg, _ := json.Marshal(message)
consumer, err := (*client).Subscribe(pulsar.ConsumerOptions{
Topic: topicId,
SubscriptionName: topicId,
Type: pulsar.Exclusive,
})
if err != nil {
log.Fatal(err)
}
producer, err := (*client).CreateProducer(pulsar.ProducerOptions{
Topic: topic,
})
if err != nil {
log.Fatal(err)
}
_, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
Payload: msg,
})
if err != nil {
fmt.Println("Failed to publish message", err)
}
fmt.Println("Published message " + string(msg))
res, err := consumer.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
//Destroy consumer
if err := consumer.Unsubscribe(); err != nil {
log.Fatal(err)
}
go foo(res.Payload())
}
func SendMessage(client *pulsar.Client, topic string, message []byte) {
producer, err := (*client).CreateProducer(pulsar.ProducerOptions{
Topic: topic,
})
if err != nil {
log.Fatal(err)
}
_, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
Payload: message,
})
if err != nil {
fmt.Println("Failed to publish message", err)
}
fmt.Println("Published message" + string(message))
}