-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmq.go
47 lines (39 loc) · 1.28 KB
/
mq.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
// Package mq provides an abstraction layer around concrete Message Queue implementation(s).
// mq is not intended to be a generic interface around Message Queues. It is opinionated in how
// the underlying Message Queue is used and its API provides a very limited subset of potential
// functionality.
package mq
import (
"fmt"
"github.com/c3sr/mq/interfaces"
)
var dialer interfaces.QueueDialer
// SetDialer configures the package with a dialer that provides connections to a specific Message Queue
// implementation.
func SetDialer(d interfaces.QueueDialer) {
dialer = d
}
// NewMessageQueue constructs and returns a new object implementing the interfaces.MessageQueue interface.
// This object uses the dialer provided by calling SetDialer to connect to the message queue server.
// SetDialer *must* be called before calling NewMessageQueue, otherwise an error will be returned.
func NewMessageQueue() (mq interfaces.MessageQueue, err error) {
if dialer == nil {
err = fmt.Errorf("A dialer must be provided using SetDialer()")
return
}
var ch interfaces.QueueChannel
var conn interfaces.QueueConnection
conn, err = dialer.Dial()
if err != nil {
return
}
ch, err = conn.Channel()
if err != nil {
return
}
mq = &messageQueue{
channel: ch,
connection: conn,
}
return
}