11package nats
22
33import (
4+ "encoding/json"
45 "log"
56 "strings"
67 "sync"
@@ -25,8 +26,8 @@ type Options struct {
2526
2627// Nats will implement Nats subscribe and publish functionality
2728type Nats struct {
28- ec * nats.EncodedConn
29- wg * sync.WaitGroup
29+ conn * nats.Conn
30+ wg * sync.WaitGroup
3031}
3132
3233// New - constructor
@@ -57,55 +58,66 @@ func New(opts Options) (broker.Handler, error) {
5758 return nil , ErrConnect (err )
5859 }
5960
60- ec , err := nats .NewEncodedConn (nc , nats .JSON_ENCODER )
61- if err != nil {
62- return nil , ErrEncodedConn (err )
63- }
64-
65- return & Nats {ec : ec }, nil
61+ return & Nats {conn : nc , wg : & sync.WaitGroup {}}, nil
6662}
63+
6764func (n * Nats ) ConnectedEndpoints () (endpoints []string ) {
68- for _ , server := range n .ec . Conn .Servers () {
65+ for _ , server := range n .conn .Servers () {
6966 endpoints = append (endpoints , strings .TrimPrefix (server , "nats://" ))
7067 }
7168 return
7269}
7370
7471func (n * Nats ) Info () string {
75- if n .ec == nil || n . ec . Conn == nil {
72+ if n .conn == nil {
7673 return broker .NotConnected
7774 }
78- return n .ec . Conn .Opts .Name
75+ return n .conn .Opts .Name
7976}
8077
8178func (n * Nats ) CloseConnection () {
82- n .ec .Close ()
79+ if n .conn != nil {
80+ if err := n .conn .Drain (); err != nil {
81+ log .Printf ("nats: drain error: %v" , err )
82+ }
83+ n .conn .Close ()
84+ }
8385}
8486
87+
8588// Publish - to publish messages
8689func (n * Nats ) Publish (subject string , message * broker.Message ) error {
87- err := n . ec . Publish ( subject , message )
90+ b , err := json . Marshal ( message )
8891 if err != nil {
8992 return ErrPublish (err )
9093 }
91- return nil
94+ return n . conn . Publish ( subject , b )
9295}
9396
9497// PublishWithChannel - to publish messages with channel
9598func (n * Nats ) PublishWithChannel (subject string , msgch chan * broker.Message ) error {
96- err := n .ec .BindSendChan (subject , msgch )
97- if err != nil {
98- return ErrPublish (err )
99- }
100- return nil
99+ go func () {
100+ for msg := range msgch {
101+ b , err := json .Marshal (msg )
102+ if err != nil {
103+ log .Printf ("nats: JSON marshal error: %v" , err )
104+ continue
105+ }
106+ if err := n .conn .Publish (subject , b ); err != nil {
107+ log .Printf ("nats: publish error for subject %s: %v" , subject , err )
108+ }
109+ }
110+ }()
111+ return nil
101112}
102113
114+
103115// Subscribe - for subscribing messages
104116// TODO Ques: Do we want to unsubscribe
105117// TODO will the method-user just subsribe, how will it handle the received messages?
106118func (n * Nats ) Subscribe (subject , queue string , message []byte ) error {
107119 n .wg .Add (1 )
108- _ , err := n .ec .QueueSubscribe (subject , queue , func (msg * nats.Msg ) {
120+ _ , err := n .conn .QueueSubscribe (subject , queue , func (msg * nats.Msg ) {
109121 message = msg .Data
110122 n .wg .Done ()
111123 })
@@ -119,7 +131,12 @@ func (n *Nats) Subscribe(subject, queue string, message []byte) error {
119131
120132// SubscribeWithChannel will publish all the messages received to the given channel
121133func (n * Nats ) SubscribeWithChannel (subject , queue string , msgch chan * broker.Message ) error {
122- _ , err := n .ec .BindRecvQueueChan (subject , queue , msgch )
134+ _ , err := n .conn .QueueSubscribe (subject , queue , func (m * nats.Msg ) {
135+ var msg broker.Message
136+ if err := json .Unmarshal (m .Data , & msg ); err == nil {
137+ msgch <- & msg
138+ }
139+ })
123140 if err != nil {
124141 return ErrQueueSubscribe (err )
125142 }
@@ -153,8 +170,5 @@ func (in *Nats) DeepCopyObject() broker.Handler {
153170// Check if the connection object is empty
154171func (in * Nats ) IsEmpty () bool {
155172 empty := & Nats {}
156- if in == nil || * in == * empty {
157- return true
158- }
159- return false
173+ return in == nil || * in == * empty
160174}
0 commit comments