mirror of
https://github.com/eventials/goevents.git
synced 2025-04-26 13:48:59 +08:00
48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package amqp
|
|
|
|
import (
|
|
"github.com/eventials/goevents/messaging"
|
|
|
|
amqplib "github.com/streadway/amqp"
|
|
)
|
|
|
|
type Connection struct {
|
|
connection *amqplib.Connection
|
|
}
|
|
|
|
// NewConnection returns an AMQP Connection.
|
|
func NewConnection(url string) (messaging.Connection, error) {
|
|
conn, err := amqplib.Dial(url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Connection{
|
|
conn,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Connection) NotifyConnectionClose() <-chan *amqplib.Error {
|
|
return c.connection.NotifyClose(make(chan *amqplib.Error))
|
|
}
|
|
|
|
// Consumer returns an AMQP Consumer.
|
|
func (c *Connection) Consumer(autoAck bool, exchange, queue string) (messaging.Consumer, error) {
|
|
return NewConsumer(c, autoAck, exchange, queue)
|
|
}
|
|
|
|
// Producer returns an AMQP Producer.
|
|
func (c *Connection) Producer(exchange, queue string) (messaging.Producer, error) {
|
|
return NewProducer(c, exchange, queue)
|
|
}
|
|
|
|
// Close closes the AMQP connection.
|
|
func (c *Connection) Close() {
|
|
c.connection.Close()
|
|
}
|
|
|
|
func (c *Connection) WaitUntilConnectionCloses() {
|
|
<-c.NotifyConnectionClose()
|
|
}
|