-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
92 lines (83 loc) · 2.21 KB
/
consumer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package rabbitmq
import (
"fmt"
"github.com/reactivex/rxgo/observable"
"github.com/reactivex/rxgo/observer"
"github.com/reactivex/rxgo/subscription"
"github.com/streadway/amqp"
"sync"
)
/**
* @author : Donald Trieu
* @created : 7/31/21, Saturday
**/
type IRabbitMessageHandler interface {
HandleMessage(queue string, body []byte) observable.Observable
}
type Consumer struct {
rabbitService IRabbitService
rabbitMessageHandler IRabbitMessageHandler
pulling bool
}
func NewRabbitConsumer(rabbitService IRabbitService, queues []string, bindingKeyQueue map[string][]string, handler IRabbitMessageHandler) *Consumer {
p := Consumer{}
p.rabbitService = rabbitService
p.rabbitMessageHandler = handler
if !rabbitService.IsReady() {
fmt.Printf("%s : \n", errNotConnected)
}
go p.Subscribe(queues, bindingKeyQueue)
return &p
}
func (c Consumer) Subscribe(queues []string, bindingKeyQueue map[string][]string) {
for {
if !c.rabbitService.IsReady() {
continue
}
for _, queue := range queues {
bindingKey := bindingKeyQueue[queue]
fmt.Printf("%s\n", bindingKey)
c.rabbitService.Subscribe(queue, bindingKey, false)
}
go c.StartPull()
break
}
}
func (c *Consumer) StartPull() {
/*
create 3 goroutine
- one listen for local queue
- one for re push message queue
- the last one for normal message queue
while receiving from re push queue, stop listen on normal queue. when all message on re push queue done, listen normal queue again
*/
var wg sync.WaitGroup
defer c.rabbitService.Close()
c.pulling = true
wg.Add(2)
go func() {
defer wg.Done()
fmt.Println("[PullerRabbit] Start Fetch Message From Rabbit")
for c.pulling {
if !c.rabbitService.IsReady() {
continue
}
delivery, message, ok := c.rabbitService.FetchMessage()
if ok {
c.handleMessage(delivery, message)
}
}
}()
wg.Wait() // wait forever
}
func (c *Consumer) handleMessage(delivery amqp.Delivery, message RabbitMessage) <-chan subscription.Subscription {
return c.rabbitMessageHandler.HandleMessage(delivery.RoutingKey, message.Body).
Subscribe(observer.Observer{
ErrHandler: func(e error) {
delivery.Ack(false)
},
DoneHandler: func() {
delivery.Ack(false)
},
})
}