rabbitmq channel接口常用方法详解

转载自:https://blog.csdn.net/leisure_life/article/details/78663244

Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments) throws IOException;
  • 1
  • 2

解释:


方法作用:
声明一个队列
参数:queue
含义:队列名称

参数:durable
含义:是否持久化,如果设置为true,服务器重启了队列仍然存在

参数:exclusive
含义:是否为独享队列(排他性队列),只有自己可见的队列,即不允许其它用户访问

如果exclusive声明为true,则该队列的特点是:

1、只对首次声明它的连接(Connection)可见
2、会在其连接断开的时候自动删除。

此参数详情请移步:RabbitMQ:排他性队列(Exclusive Queue)

参数:autoDelete
含义:当没有任何消费者使用时,自动删除该队列

参数:arguments
含义:其他参数

api解释

这里写图片描述

void basicQos(int prefetchCount) throws IOException;
  • 1

解释:


方法作用:
一次获取多少个消息
参数:prefetchCount
含义:会告诉RabbitMQ不要同时给一个消费者推送多于prefetchCount个消息
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
  • 1

解释:


方法作用:
订阅消息并消费
参数:queue
含义:所订阅的队列
参数:autoAck
含义:是否开启自动应答,默认是开启的,如果需要手动应答应该设置为false

注意:为了确保消息一定被消费者处理,rabbitMQ提供了消息确认功能,

就是在消费者处理完任务之后,就给服务器一个回馈,服务器就会将该消息删除,

如果消费者超时不回馈,那么服务器将就将该消息重新发送给其他消费者,

当autoAck设置为true时,只要消息被消费者处理,不管成功与否,服务器都会删除该消息,

而当autoAck设置为false时,只有消息被处理,且反馈结果后才会删除。
参数:callback
含义:接收到消息之后执行的回调方法
看一下回调方法源码:
 void handleDelivery(String consumerTag,
                        Envelope envelope,
                        AMQP.BasicProperties properties,
                        byte[] body)
        throws IOException;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

额,看不太懂,反正就是接到消息之后你对消息的处理都要写在这里!

之前我有一个RPC的例子,可以参考一下那里的这个方法如何实现的
http://blog.csdn.net/leisure_life/article/details/78657935
void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
  • 1

解释:


方法作用:
发布一个消息
参数:exchange
含义:指定转发器名称—-ExchangeName,这里用空字符串,就表示消息会交给默认的Exchange
参数:routingKey
含义:发布到哪个队列
参数:props
含义:和消息有关的其他配置参数,路由报头等
参数:body
含义:消息体

源码:
/**
     * Publish a message.
     *
     * Publishing to a non-existent exchange will result in a channel-level
     * protocol exception, which closes the channel.
     *
     * Invocations of <code>Channel#basicPublish</code> will eventually block if a
     * <a href="http://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
     *
     * @see com.rabbitmq.client.AMQP.Basic.Publish
     * @see <a href="http://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
     * @param exchange the exchange to publish the message to
     * @param routingKey the routing key
     * @param props other properties for the message - routing headers etc
     * @param body the message body
     * @throws java.io.IOException if an error is encountered
     */
    void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
 void basicAck(long deliveryTag, boolean multiple) throws IOException;
  • 1

解释:


方法作用:
另外需要在每次处理完成一个消息后,手动向服务端发送一次应答。
参数:deliveryTag
含义:当前消息的类似编号的号码,服务端为每一个消息生成的类似编号的号码
参数:multiple
含义:是否把小于当前deliveryTag的小于都应答了
注意:这个要在打开应答机制后使用,
 boolean ack = false ; //打开应答机制  
 channel.basicConsume(QUEUE_NAME, ack, consumer);  
  • 1
  • 2

当multiple设置为false时,只会为deliveryTag所对应的消息进行应答,服务端收到应答后将该消息删除

源码:

 /**
     * Acknowledge one or several received
     * messages. Supply the deliveryTag from the {@link com.rabbitmq.client.AMQP.Basic.GetOk}
     * or {@link com.rabbitmq.client.AMQP.Basic.Deliver} method
     * containing the received message being acknowledged.
     * @see com.rabbitmq.client.AMQP.Basic.Ack
     * @param deliveryTag the tag from the received {@link com.rabbitmq.client.AMQP.Basic.GetOk} or {@link com.rabbitmq.client.AMQP.Basic.Deliver}
     * @param multiple true to acknowledge all messages up to and
     * including the supplied delivery tag; false to acknowledge just
     * the supplied delivery tag.
     * @throws java.io.IOException if an error is encountered
     */
    void basicAck(long deliveryTag, boolean multiple) throws IOException;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值