1 Star 0 Fork 0

lipore/plume

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
publisher.go 1017 Bytes
一键复制 编辑 原始数据 按行查看 历史
lipore 提交于 2024-03-21 19:58 +08:00 . bug fix
package rabbitmq
import (
"context"
"gitee.com/lipore/plume/mq"
"github.com/pkg/errors"
"github.com/rabbitmq/amqp091-go"
)
type Publisher struct {
channel *amqp091.Channel
Exchange string
Key string
}
func (p *Publisher) Publish(ctx context.Context, body []byte) error {
err := p.channel.PublishWithContext(ctx, p.Exchange, p.Key, false, false, amqp091.Publishing{
ContentEncoding: "text/plain",
Body: body,
})
if err != nil {
return errors.WithMessage(err, "failed to publish message")
}
return nil
}
func NewPublisherFactory(connection *amqp091.Connection) mq.PublisherFactory {
return &PublisherFactory{
conn: connection,
}
}
type PublisherFactory struct {
conn *amqp091.Connection
}
func (p *PublisherFactory) NewPublisher(topic string) (mq.Publisher, error) {
channel, err := p.conn.Channel()
if err != nil {
return nil, errors.WithMessage(err, "failed to create channel")
}
return &Publisher{
channel: channel,
Exchange: topic,
Key: "",
}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lipore/plume.git
git@gitee.com:lipore/plume.git
lipore
plume
plume
v1.7.10

搜索帮助