Skip to content

Commit 12a582f

Browse files
committed
correctly initialize messages chan when subscribing to channel messages, add rmq subscriber
1 parent 612da12 commit 12a582f

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

events/subscribers/rmq/rmq.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type RMQSubscriber struct {
99
connection *amqp.Connection
1010
channel *amqp.Channel
1111
queueName string
12+
13+
Messages chan interface{}
1214
}
1315

1416
func NewSubscriber() (*RMQSubscriber, error) {
@@ -36,15 +38,17 @@ func NewSubscriber() (*RMQSubscriber, error) {
3638
return nil, err
3739
}
3840

41+
msgs := make(chan interface{})
42+
3943
return &RMQSubscriber{
4044
connection: conn,
4145
channel: ch,
4246
queueName: queueName,
47+
Messages: msgs,
4348
}, nil
4449
}
4550

4651
func (r *RMQSubscriber) Start() chan bool {
47-
// Start consuming messages
4852
msgs, err := r.channel.Consume(
4953
r.queueName,
5054
"",
@@ -61,42 +65,23 @@ func (r *RMQSubscriber) Start() chan bool {
6165
done := make(chan bool)
6266
go func() {
6367
for d := range msgs {
64-
// Process message
65-
// Placeholder: Print message to console
66-
println("Received message: ", string(d.Body))
68+
if r.Messages == nil {
69+
close(done)
70+
break
71+
}
72+
r.Messages <- d.Body
6773
}
68-
done <- true
6974
}()
7075
return done
7176
}
7277

78+
func (r *RMQSubscriber) Listen() <-chan interface{} {
79+
return r.Messages
80+
}
81+
7382
func (r *RMQSubscriber) Detach() error {
7483
if err := r.channel.Close(); err != nil {
7584
return err
7685
}
7786
return r.connection.Close()
7887
}
79-
80-
func (r *RMQSubscriber) Listen() <-chan interface{} {
81-
msgs, err := r.channel.Consume(
82-
r.queueName,
83-
"",
84-
config.Read().RabbitMQ.AutoAck,
85-
config.Read().RabbitMQ.Exclusive,
86-
false,
87-
config.Read().RabbitMQ.NoWait,
88-
nil,
89-
)
90-
if err != nil {
91-
panic(err)
92-
}
93-
94-
output := make(chan interface{})
95-
go func() {
96-
for d := range msgs {
97-
output <- d.Body
98-
}
99-
close(output)
100-
}()
101-
return output
102-
}

0 commit comments

Comments
 (0)