rabbitmq

package
v0.0.0-...-9d44b34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RabbitMq

type RabbitMq struct {
	UserName string              //队列账户名
	PassWord string              //队列密码
	Ip       string              //队列IP
	Port     int                 //队列端口
	Qos      int                 //并发数(准确的意思并不指并发,但常规可以这么理解)
	Heart    func(rmq *RabbitMq) //当队列掉线后触发该函数,函数中请自行重连及重新监听队列等操作
	// contains filtered or unexported fields
}

func (*RabbitMq) BindQueue

func (p *RabbitMq) BindQueue(name, key, exchange string, noWait bool, args map[string]interface{}) error

* 绑定队列

func (*RabbitMq) Close

func (p *RabbitMq) Close()

* 关闭连接

func (*RabbitMq) Connect

func (p *RabbitMq) Connect() error

* 连接队列 返回:

返回error对象,如果error对象值为nil表示成功,否则为失败

func (*RabbitMq) DelExchange

func (p *RabbitMq) DelExchange(name string, ifUnused bool, noWait bool) error

* 删除交换机

func (*RabbitMq) DelQueue

func (p *RabbitMq) DelQueue(name string, ifUnused bool, ifEmpty bool, noWait bool) (int, error)

* 删除队列 传参:

name:队列名
ifUnused:仅删除未使用的队列
ifEmpty:仅删除空消息的队列
noWait:不等待响应直接删除

返回:

删除条件和错误信息

func (*RabbitMq) ListenMsg

func (p *RabbitMq) ListenMsg(queueName string, consumer string, autoAck bool, exclusive bool, noWait bool, backcall func(rmq *RabbitMq, d amqp.Delivery), args map[string]interface{}) error

* 监听消费消息(本注释中的消费者和监听者是一个意思) 传参:

queueName:欲监听的队列名称
consumer:监听者标识符,可随意填写,初始值为空字符串。
autoAck:自动回执,如果为true,则收到消息后自动确认消息已处理。如果为false,则收到消息不自动确认消息处理情况。您需要手动调用 发送接受回执/发送拒绝回执 做消息手动回执。
exclusive:是否独占,如果为true,则同时只允许当前监听者进行订阅,除此以外所有客户端都不能订阅此队列消息,直到客户端被关闭。
noWait:是否等待消息ACK,为true则不等待,为false为等待。
backcall:传入一个回调函数,当有新消息时调用该函数,函数定义格式:func demo(rmq *rabbitmq.RabbitMq, d amqp.Delivery){}
args:其他参数,可以传nil

返回:

返回error对象,如果error对象值为nil表示成功,否则为失败

func (*RabbitMq) NewExchange

func (p *RabbitMq) NewExchange(name, kind string, durable bool, autoDelete bool, internal bool, noWait bool, args map[string]interface{}) error

* 创建交换机

func (*RabbitMq) NewQueue

func (p *RabbitMq) NewQueue(name string, durable bool, autoDelete bool, exclusive bool, noWait bool, args map[string]interface{}) (amqp.Queue, error)

* 创建一个新的队列 传参:

name:队列名称
durable:是否持久化,持久化服务重启不会丢失数据,但会影响性能
autoDelete:是否自动删除,如果为true,则没有任何客户端连接时,将自动删除队列。否则无论是否有客户端访问,队列和未处理的消息都将被永久保留
exclusive:是否独占,如果为true,则队列只能被当前连接独占,连接断开则队列自动删除。否则队列将一直保持(持久化),其它连接都可以访问此队列
noWait:是否非阻塞
args:其他参数,可以传nil

返回:

队列对象,如果失败则会返回error错误对象

func (*RabbitMq) PushMsg

func (p *RabbitMq) PushMsg(exchangeName string, routingKey string, body []byte, headers map[string]interface{}, properties map[string]interface{}) error

* 推送消息 传参:

exchangeName:交换机名称
routingKey:路由key
body:消息主体
headers:消息头
properties:key只能填下列列举的key,value格式请百度,没有则不填。
			ContentType
			ContentEncoding
			Priority
			CorrelationId
			ReplyTo
			Expiration
			MessageId
			Timestamp
			Type
			UserId
			AppId
			ClusterId

返回:

返回error对象,如果error对象值为nil表示成功,否则为失败

func (*RabbitMq) PushMsgAndWaitRes

func (p *RabbitMq) PushMsgAndWaitRes(exchangeName string, routingKey string, body []byte, headers map[string]interface{}, properties map[string]interface{}, timeout int) (amqp.Delivery, error)

* 推送消息并等待响应 消费者应该从Properties属性中获取reply_to属性值,reply_to值为响应的监听队列,请将响应结果推送至该队列中 传参:

exchangeName:交换机名称
routingKey:路由key
body:消息主体
headers:消息头
properties:key只能填下列列举的key,value格式请百度,没有则不填。
			ContentType
			ContentEncoding
			Priority
			CorrelationId
			ReplyTo
			Expiration
			MessageId
			Timestamp
			Type
			UserId
			AppId
			ClusterId
timeout:等待响应超时时间,单位秒

返回:

amqp.Delivery消息对象,如果有错误则会返回error对象

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL