-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitter.go
59 lines (50 loc) · 993 Bytes
/
emitter.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
package rmqtools
import (
"context"
amqp "github.com/rabbitmq/amqp091-go"
)
type Emitter struct {
connection *amqp.Connection
exchangeName string
}
func (e *Emitter) Setup() error {
channel, err := e.connection.Channel()
if err != nil {
return err
}
defer channel.Close()
return DeclareExchange(channel, e.exchangeName)
}
func (e *Emitter) Push(ctx context.Context, routingKey string, data []byte) error {
channel, err := e.connection.Channel()
if err != nil {
return err
}
defer channel.Close()
err = channel.PublishWithContext(
ctx,
e.exchangeName,
routingKey,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: data,
},
)
if err != nil {
return err
}
return nil
}
func NewEventEmitter(conn *amqp.Connection, exchangeName string) (Emitter, error) {
emitter := Emitter{
connection: conn,
exchangeName: exchangeName,
}
err := emitter.Setup()
if err != nil {
return Emitter{}, err
}
return emitter, nil
}