-
Notifications
You must be signed in to change notification settings - Fork 0
/
pulsar_producer.go
68 lines (56 loc) · 1.52 KB
/
pulsar_producer.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
package eventbus
import (
"context"
"fmt"
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/maoge/paas-metasvr-go/pkg/consts"
"github.com/maoge/paas-metasvr-go/pkg/global"
"github.com/maoge/paas-metasvr-go/pkg/utils"
)
type PulsarProducer struct {
client *pulsar.Client
producer *pulsar.Producer
}
func CreatePulsarProducer() EventBusProducer {
topic := fmt.Sprintf("persistent://public/default/%v", consts.SYS_EVENT_TOPIC)
pulsarClient := global.GLOBAL_RES.PulsarClient
pulsarProducer, err := (*pulsarClient).CreateProducer(pulsar.ProducerOptions{
Topic: topic,
BatchingMaxPublishDelay: 1 * time.Second,
DisableBlockIfQueueFull: false,
MaxPendingMessages: 1000,
})
if err != nil {
errInfo := fmt.Sprintf("CreateProducer error: %v", err.Error())
utils.LOGGER.Fatal(errInfo)
}
return PulsarProducer{
client: pulsarClient,
producer: &pulsarProducer,
}
}
func (m PulsarProducer) Send(data []byte) error {
msg := &pulsar.ProducerMessage{
Value: data,
}
_, err := (*m.producer).Send(context.Background(), msg)
return err
}
func (m PulsarProducer) SendAsync(data []byte) {
msg := &pulsar.ProducerMessage{
Payload: data,
}
(*m.producer).SendAsync(context.Background(), msg, asyncSendCallBack)
}
func (m PulsarProducer) Close() {
(*m.producer).Close()
m.producer = nil
}
func asyncSendCallBack(_ pulsar.MessageID, _ *pulsar.ProducerMessage, err error) {
if err != nil {
errInfo := fmt.Sprintf("SendAsync error: %v", err)
utils.LOGGER.Fatal(errInfo)
}
return
}