代码拉取完成,页面将自动刷新
package rabbitmq
import (
"context"
amqp "github.com/rabbitmq/amqp091-go"
"go.uber.org/zap"
)
// Publisher session
type Publisher struct {
*Producer
}
// NewPublisher create a publisher, channelName is exchange name
func NewPublisher(channelName string, connection *Connection, opts ...ProducerOption) (*Publisher, error) {
o := defaultProducerOptions()
o.apply(opts...)
exchange := NewFanoutExchange(channelName)
// crate a new channel
ch, err := connection.conn.Channel()
if err != nil {
return nil, err
}
// declare the exchange type
err = ch.ExchangeDeclare(
channelName,
exchangeTypeFanout,
o.isPersistent,
o.exchangeDeclare.autoDelete,
o.exchangeDeclare.internal,
o.exchangeDeclare.noWait,
o.exchangeDeclare.args,
)
if err != nil {
_ = ch.Close()
return nil, err
}
deliveryMode := amqp.Persistent
if !o.isPersistent {
deliveryMode = amqp.Transient
}
connection.zapLog.Info("[rabbit producer] initialized", zap.String("channel", channelName), zap.Bool("isPersistent", o.isPersistent))
p := &Producer{
Exchange: exchange,
conn: connection.conn,
ch: ch,
isPersistent: o.isPersistent,
deliveryMode: deliveryMode,
mandatory: o.mandatory,
zapLog: connection.zapLog,
}
return &Publisher{p}, nil
}
func (p *Publisher) Publish(ctx context.Context, body []byte) error {
return p.ch.PublishWithContext(
ctx,
p.Exchange.name,
p.Exchange.routingKey,
p.mandatory,
false,
amqp.Publishing{
DeliveryMode: p.deliveryMode,
ContentType: "text/plain",
Body: body,
},
)
}
// Close publisher
func (p *Publisher) Close() {
if p.ch != nil {
_ = p.ch.Close()
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。