Add pull subscribe #1

Merged
namhq1989 merged 12 commits from add-pull-subscribe into master 2022-03-18 04:28:50 +00:00
3 changed files with 66 additions and 2 deletions

31
jetstream_consumer.go Normal file
View File

@ -0,0 +1,31 @@
package natsio
import (
"errors"
"fmt"
"github.com/nats-io/nats.go"
)
// GetConsumerInfo ...
func (js JetStream) GetConsumerInfo(stream, name string) (*nats.ConsumerInfo, error) {
return js.instance.ConsumerInfo(stream, name)
}
// AddConsumer ...
func (js JetStream) AddConsumer(stream, subject, name string) error {
channel := combineStreamAndSubjectName(stream, subject)
// Add
_, err := js.instance.AddConsumer(stream, &nats.ConsumerConfig{
Durable: name,
AckPolicy: nats.AckExplicitPolicy,
FilterSubject: channel,
})
if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - add consumer %s for stream #%s error: %s", name, stream, err.Error())
return errors.New(msg)
}
return nil
}

View File

@ -13,7 +13,7 @@ func (js JetStream) Publish(stream, subject string, payload []byte) error {
_, err := js.instance.PublishAsync(channel, payload) _, err := js.instance.PublishAsync(channel, payload)
if err != nil { if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - publish message to subject %s error: %s", channel, err.Error()) msg := fmt.Sprintf("[NATS JETSTREAM] - publish message to subject #%s error: %s", channel, err.Error())
return errors.New(msg) return errors.New(msg)
} }
return nil return nil
@ -30,3 +30,36 @@ func (js JetStream) Subscribe(stream, subject string, cb nats.MsgHandler) (*nats
} }
return sub, nil return sub, nil
} }
// PullSubscribe ...
//
// Example:
//
// js := natsio.GetJetStream()
//
// sub, err := js.PullSubscribe("A_SUBJECT", "A_SUBJECT", "A_CONSUMER")
//
// for {
// messages, err := sub.Fetch(10)
// // process each messages
// }
//
func (js JetStream) PullSubscribe(stream, subject, consumer string) (*nats.Subscription, error) {
channel := combineStreamAndSubjectName(stream, subject)
// Check if consumer existed
con, err := js.GetConsumerInfo(stream, consumer)
if con == nil || err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe consumer %s not existed in stream %s", consumer, stream)
return nil, errors.New(msg)
}
// Pull
sub, err := js.instance.PullSubscribe(channel, consumer)
if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe subject #%s - consumer #%s error: %s", channel, consumer, err.Error())
return nil, errors.New(msg)
}
return sub, nil
}

View File

@ -13,7 +13,7 @@ func (js JetStream) QueueSubscribe(stream, subject, queueName string, cb nats.Ms
_, err := js.instance.QueueSubscribe(channel, queueName, cb) _, err := js.instance.QueueSubscribe(channel, queueName, cb)
if err != nil { if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - queue subscribe with subject %s error: %s", channel, err.Error()) msg := fmt.Sprintf("[NATS JETSTREAM] - queue subscribe with subject #%s error: %s", channel, err.Error())
return errors.New(msg) return errors.New(msg)
} }
return nil return nil