From 1ef02e92025b3c9147e85c737590febe5eb672d7 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Sun, 9 Oct 2022 23:07:25 +0800 Subject: [PATCH] update --- rabbit/pool.go | 66 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/rabbit/pool.go b/rabbit/pool.go index 909e4ef..ca95f59 100644 --- a/rabbit/pool.go +++ b/rabbit/pool.go @@ -284,14 +284,27 @@ func (ch *Channel) QueueDelete(queue string) { } //初始化队列 -func (ch *Channel) NewQueue(name string, typename string) { +func (ch *Channel) NewQueue(name string, args ...bool) { + var durable, autoDelete, exclusive, noWait = true, false, false, false + if len(args) > 0 { + durable = args[0] + } + if len(args) > 1 { + autoDelete = args[1] + } + if len(args) > 2 { + exclusive = args[2] + } + if len(args) > 3 { + noWait = args[3] + } q, e := ch.Channel.QueueDeclare( - name, //队列名 - false, //是否开启持久化 - false, //不使用时删除 - false, //排他 - false, //不等待 - nil, //参数 + name, //队列名 + durable, //是否开启持久化 + autoDelete, //不使用时删除 + exclusive, //排他 + noWait, //不等待 + nil, //参数 ) failOnError(e, "初始化队列失败!") ch.Name = q.Name @@ -299,23 +312,30 @@ func (ch *Channel) NewQueue(name string, typename string) { // NewExchange 初始化交换机 //s:rabbitmq服务器的链接,name:交换机名字,typename:交换机类型 -func NewExchange(s string, name string, typename string) { - //连接rabbitmq - conn, e := amqp.Dial(s) - failOnError(e, "连接Rabbitmq服务器失败!") - ch, e := conn.Channel() - failOnError(e, "无法打开频道!") - e = ch.ExchangeDeclare( - name, // name - typename, // type - true, // durable - false, // auto-deleted - false, // internal - false, // no-wait - nil, // arguments +func (ch *Channel) NewExchange(name string, typename string, args ...bool) { + var durable, autoDelete, internal, noWait = true, false, false, false + if len(args) > 0 { + durable = args[0] + } + if len(args) > 1 { + autoDelete = args[1] + } + if len(args) > 2 { + internal = args[2] + } + if len(args) > 3 { + noWait = args[3] + } + e := ch.ExchangeDeclare( + name, // name + typename, // type + durable, // durable + autoDelete, // auto-deleted + internal, // 是否只在rabbitmq server内部使用 + noWait, // no-wait + nil, // arguments ) failOnError(e, "初始化交换机失败!") - } // ExchangeDelete 删除交换机 @@ -349,7 +369,7 @@ func (ch *Channel) Send(queue string, body interface{}) { str, e := json.Marshal(body) failOnError(e, "消息序列化失败!") e = ch.Channel.Publish( - "", //交换 + "", //交换机 queue, //路由键 false, //必填 false, //立即