【RabbitMQ.Client笔记】IBus
发布消息(BasicPublish)
void BasicPublish(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, byte[] body);
void BasicPublish(this IModel model, PublicationAddress addr, IBasicProperties basicProperties, byte[] body);
void BasicPublish(this IModel model, string exchange, string routingKey, IBasicProperties basicProperties, byte[] body);
void BasicPublish(this IModel model, string exchange, string routingKey, bool mandatory = false, IBasicProperties basicProperties = null, byte[] body = null);
BasicPublish方法参数:
exchange
:要将消息发送到的Exchange(交换器)routingKey
:路由Keymandatory
:如果为true, 消息不能路由到指定的队列时,会触发channel.BasicReturn事件,如果为false,则broker会直接将消息丢弃。(channel.BasicReturn += Channel_BasicReturn;)basicProperties
:消息附加属性body
:消息内容
Properties
发送消息附带Properties
属性字段详解:
contentType
:消息的内容类型,如:text/plain
contentEncoding:
消息内容编码headers
:设置消息的header,类型为Map<String,Object>deliveryMode
:1(nopersistent)非持久化,2(persistent)持久化priority
:消息的优先级correlationId
:关联IDreplyTo
:用于指定回复的队列的名称expiration
:消息的失效时间messageId
:消息IDtimestamp
:消息的时间戳type
:类型userId
:用户IDappId
:应用程序IDcusterId
:集群ID
主动拉消息(basicGet)
主动拉取队列中的一条消息
BasicGetResult basicGet(String queue, boolean autoAck)
basicConsume
由服务端主动PUSH消息过来,方法接收到消息后进行处理
string BasicConsume(string queue, bool autoAck, string consumerTag, bool noLocal, bool exclusive, IDictionary<string, object> arguments, IBasicConsumer consumer);
public static string BasicConsume(this IModel model, IBasicConsumer consumer, string queue, bool autoAck = false, string consumerTag = "", bool noLocal = false, bool exclusive = false, IDictionary<string, object> arguments = null);
public static string BasicConsume(this IModel model, string queue, bool autoAck, IBasicConsumer consumer);
public static string BasicConsume(this IModel model, string queue, bool autoAck, string consumerTag, IBasicConsumer consumer);
public static string BasicConsume(this IModel model, string queue, bool autoAck, string consumerTag, IDictionary<string, object> arguments, IBasicConsumer consumer);
basicConsume方法参数说明:
queue
:队列名autoAck
:是否自动回复AckconsumerTag
:消费者标识arguments
:其他参数,比如可以设置消费者优先级(x-priority)等参数noLocal
:如果服务器不应将在此通道连接上发布的消息传递给此使用者,则为trueexclusive
:IBasicConsumer
:
设置消费者优先级(x-priority)
消费者优先级允许你确保高优先级消费者活动(非阻塞)的时候接收消息,低优先消费者只有在高优先级消费者阻塞的时候接收消息。
通常,连接到队列上的活动消费者以循环的方式从队列接收消息。当消费者使用优先级,如果多个活动消费者存在,且具有相同的优先级,则消息轮流传递。
Dictionary<String, Object> args = new Dictionary<String, Object>();
args.put("x-priority", 10);
channel.BasicConsume("my-queue", false,"", args, consumer);
basicCancel
取消消费者订阅
/**
* 取消消费者对队列的订阅关系
* consumerTag:服务器端生成的消费者标识
**/
void basicCancel(String consumerTag)