add check consumer

This commit is contained in:
Nam Huynh 2022-03-18 11:19:01 +07:00
parent f831591266
commit 095e12a241
1 changed files with 9 additions and 18 deletions

View File

@ -37,36 +37,27 @@ func (js JetStream) Subscribe(stream, subject string, cb nats.MsgHandler) (*nats
// //
// js := natsio.GetJetStream() // js := natsio.GetJetStream()
// //
// sub, err := js.PullSubscribe("A_SUBJECT", "A_DURABLE") // sub, err := js.PullSubscribe("A_SUBJECT", "A_SUBJECT", "A_CONSUMER")
// //
// for { // for {
// messages, err := sub.Fetch(10) // messages, err := sub.Fetch(10)
// // process each messages // // process each messages
// } // }
// //
func (js JetStream) PullSubscribe(stream, subject, durable, consumer string) (*nats.Subscription, error) { func (js JetStream) PullSubscribe(stream, subject, consumer string) (*nats.Subscription, error) {
channel := combineStreamAndSubjectName(stream, subject) channel := combineStreamAndSubjectName(stream, subject)
// Check if consumer existed // Check if consumer existed
con, err := js.instance.ConsumerInfo(stream, consumer) con, err := js.GetConsumerInfo(stream, consumer)
fmt.Println("con", con) if con == nil || err != nil {
fmt.Println("err", err) msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe consumer %s not existed in stream %s", consumer, stream)
if con == nil { return nil, errors.New(msg)
info, err := js.instance.AddConsumer(stream, &nats.ConsumerConfig{
Durable: durable,
AckPolicy: nats.AckExplicitPolicy,
FilterSubject: subject,
})
if err == nil {
fmt.Println("CONSUMER INFO", info)
} else {
fmt.Println("ADD CONSUMER ERROR", err)
}
} }
sub, err := js.instance.PullSubscribe(subject, durable) // Pull
sub, err := js.instance.PullSubscribe(channel, consumer)
if err != nil { if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe subject #%s - durable #%s error: %s", channel, durable, err.Error()) msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe subject #%s - consumer #%s error: %s", channel, consumer, err.Error())
return nil, errors.New(msg) return nil, errors.New(msg)
} }