2016-12-01 10:52:22 -02:00
|
|
|
package amqp
|
|
|
|
|
|
|
|
import (
|
2016-12-01 11:40:58 -02:00
|
|
|
"github.com/eventials/goevents/messaging"
|
|
|
|
|
2016-12-01 10:52:22 -02:00
|
|
|
amqplib "github.com/streadway/amqp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
|
|
|
connection *amqplib.Connection
|
|
|
|
}
|
|
|
|
|
2016-12-01 16:17:55 -02:00
|
|
|
// NewConnection returns an AMQP Connection.
|
2016-12-28 09:20:29 -02:00
|
|
|
func NewConnection(url string) (messaging.Connection, error) {
|
2016-12-01 10:52:22 -02:00
|
|
|
conn, err := amqplib.Dial(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Connection{
|
|
|
|
conn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-12-28 09:20:29 -02:00
|
|
|
func (c *Connection) NotifyConnectionClose() <-chan *amqplib.Error {
|
|
|
|
return c.connection.NotifyClose(make(chan *amqplib.Error))
|
|
|
|
}
|
|
|
|
|
2016-12-01 16:17:55 -02:00
|
|
|
// Consumer returns an AMQP Consumer.
|
2016-12-28 09:20:29 -02:00
|
|
|
func (c *Connection) Consumer(autoAck bool, exchange, queue string) (messaging.Consumer, error) {
|
|
|
|
return NewConsumer(c, autoAck, exchange, queue)
|
2016-12-01 10:52:22 -02:00
|
|
|
}
|
|
|
|
|
2016-12-01 16:17:55 -02:00
|
|
|
// Producer returns an AMQP Producer.
|
2016-12-28 09:20:29 -02:00
|
|
|
func (c *Connection) Producer(exchange, queue string) (messaging.Producer, error) {
|
|
|
|
return NewProducer(c, exchange, queue)
|
2016-12-01 10:52:22 -02:00
|
|
|
}
|
|
|
|
|
2016-12-01 16:17:55 -02:00
|
|
|
// Close closes the AMQP connection.
|
2016-12-01 10:52:22 -02:00
|
|
|
func (c *Connection) Close() {
|
|
|
|
c.connection.Close()
|
|
|
|
}
|
2016-12-28 09:20:29 -02:00
|
|
|
|
2016-12-28 09:33:59 -02:00
|
|
|
func (c *Connection) WaitUntilConnectionCloses() {
|
|
|
|
<-c.NotifyConnectionClose()
|
2016-12-28 09:20:29 -02:00
|
|
|
}
|