-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqp_backend.go
75 lines (65 loc) · 1.56 KB
/
amqp_backend.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
package eventcore
import (
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-amqp/pkg/amqp"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/go-kit/kit/log/level"
)
type AmqpBackend struct {
suffix string
Url string
configuaration amqp.Config
publisher *amqp.Publisher
subscriber *amqp.Subscriber
}
func NewAmqpBackend(connectionString string) *AmqpBackend {
return &AmqpBackend{
Url: connectionString,
}
}
func (a *AmqpBackend) Close() error {
if a.publisher != nil {
err := a.publisher.Close()
if err != nil {
level.Error(Logger).Log(err)
}
}
if a.subscriber != nil {
err := a.subscriber.Close()
if err != nil {
level.Error(Logger).Log(err)
}
}
return nil
}
func (a *AmqpBackend) SetConsumerGroupName(name string) {
a.suffix = name
}
func (a *AmqpBackend) config() amqp.Config {
level.Debug(Logger).Log("connection", a.Url, "consummer_group", a.suffix)
a.configuaration = amqp.NewDurablePubSubConfig(
a.Url,
amqp.GenerateQueueNameTopicNameWithSuffix(a.suffix),
)
return a.configuaration
}
func (a *AmqpBackend) Subscriber() message.Subscriber {
if a.subscriber == nil {
var err error
a.subscriber, err = amqp.NewSubscriber(a.config(), watermill.NewStdLogger(false, false))
if err != nil {
panic(err)
}
}
return a.subscriber
}
func (a *AmqpBackend) Publisher() message.Publisher {
if a.publisher == nil {
var err error
a.publisher, err = amqp.NewPublisher(a.config(), watermill.NewStdLogger(false, false))
if err != nil {
panic(err)
}
}
return a.publisher
}