1 回答
TA贡献1834条经验 获得超8个赞
给定以下类型:
type AmqpChannel interface {
ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error
QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error)
QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error
Consume(queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args amqp.Table) (<-chan amqp.Delivery, error)
Publish(exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error
}
type AmqpConnection interface {
Channel() (AmqpChannel, error)
Close() error
}
type AmqpDial func(url string) (AmqpConnection, error)
您可以创建委托给实际代码的简单包装器:
func AmqpDialWrapper(url string) (AmqpConnection, error) {
conn, err := amqp.Dial(url)
if err != nil {
return nil, err
}
return AmqpConnectionWrapper{conn}, nil
}
type AmqpConnectionWrapper struct {
conn *amqp.Connection
}
// If *amqp.Channel does not satisfy the consumer.AmqpChannel interface
// then you'll need another wrapper, a AmqpChannelWrapper, that implements
// the consumer.AmqpChannel interface and delegates to *amqp.Channel.
func (w AmqpConnectionWrapper) Channel() (AmqpChannel, error) {
return w.conn.Channel()
}
func (w AmqpConnectionWrapper) Close() error {
return w.conn.Close()
}
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报