rabbitmq channel参数详解

https://www.cnblogs.com/piaolingzxh/p/5448927.html

1、Channel

1.1 channel.exchangeDeclare():

type:有direct、fanout、topic三种

durable:true、false true:服务器重启会保留下来Exchange。警告:仅设置此选项,不代表消息持久化。即不保证重启后消息还在。原文:true if we are declaring a durable exchange (the exchange will survive a server restart)

autoDelete:true、false.true:当已经没有消费者时,服务器是否可以删除该Exchange。原文1:true if the server should delete the exchange when it is no longer in use。

/**    * Declare an exchange.

    * @see com.rabbitmq.client.AMQP.Exchange.Declare

    * @see com.rabbitmq.client.AMQP.Exchange.DeclareOk

    * @param exchange the name of the exchange

    * @param type the exchange type

    * @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)

    * @param autoDelete true if the server should delete the exchange when it is no longer in use

    * @param arguments other properties (construction arguments) for the exchange

    * @return a declaration-confirm method to indicate the exchange was successfully declared

    * @throws java.io.IOException if an error is encountered

    */    Exchange.DeclareOk exchangeDeclare(String exchange, String type, booleandurable,boolean autoDelete,

                                      Map arguments)throwsIOException;

1.2 chanel.basicQos()

prefetchSize:0

prefetchCount:会告诉RabbitMQ不要同时给一个消费者推送多于N个消息,即一旦有N个消息还没有ack,则该consumer将block掉,直到有消息ack

global:true\false 是否将上面设置应用于channel,简单点说,就是上面限制是channel级别的还是consumer级别

备注:据说prefetchSize 和global这两项,rabbitmq没有实现,暂且不研究

/**    * Request specific "quality of service" settings.

    *

    * These settings impose limits on the amount of data the server

    * will deliver to consumers before requiring acknowledgements.

    * Thus they provide a means of consumer-initiated flow control.

    * @see com.rabbitmq.client.AMQP.Basic.Qos

    * @param prefetchSize maximum amount of content (measured in

    * octets) that the server will deliver, 0 if unlimited

    * @param prefetchCount maximum number of messages that the server

    * will deliver, 0 if unlimited

    * @param global true if the settings should be applied to the

    * entire channel rather than each consumer

    * @throws java.io.IOException if an error is encountered

    */voidbasicQos(intprefetchSize,intprefetchCount,booleanglobal)throwsIOException;

1.3 channel.basicPublish()

routingKey:路由键,#匹配0个或多个单词,*匹配一个单词,在topic exchange做消息转发用

mandatory:true:如果exchange根据自身类型和消息routeKey无法找到一个符合条件的queue,那么会调用basic.return方法将消息返还给生产者。false:出现上述情形broker会直接将消息扔掉

immediate:true:如果exchange在将消息route到queue(s)时发现对应的queue上没有消费者,那么这条消息不会放入队列中。当与消息routeKey关联的所有queue(一个或多个)都没有消费者时,该消息会通过basic.return方法返还给生产者。

BasicProperties :需要注意的是BasicProperties.deliveryMode,0:不持久化 1:持久化 这里指的是消息的持久化,配合channel(durable=true),queue(durable)可以实现,即使服务器宕机,消息仍然保留

简单来说:mandatory标志告诉服务器至少将该消息route到一个队列中,否则将消息返还给生产者;immediate标志告诉服务器如果该消息关联的queue上有消费者,则马上将消息投递给它,如果所有queue都没有消费者,直接把消息返还给生产者,不用将消息入队列等待消费者了。

/**    * Publish a message.

    *

    * Publishing to a non-existent exchange will result in a channel-level

    * protocol exception, which closes the channel.

    *

    * Invocations of Channel#basicPublish will eventually block if a

    * resource-driven alarm is in effect.

    *

    * @see com.rabbitmq.client.AMQP.Basic.Publish

    * @see Resource-driven alarms.

    * @param exchange the exchange to publish the message to

    * @param routingKey the routing key

    * @param mandatory true if the 'mandatory' flag is to be set

    * @param immediate true if the 'immediate' flag is to be

    * set. Note that the RabbitMQ server does not support this flag.

    * @param props other properties for the message - routing headers etc

    * @param body the message body

    * @throws java.io.IOException if an error is encountered

    */voidbasicPublish(String exchange, String routingKey,booleanmandatory,booleanimmediate, BasicProperties props,byte[] body)

            throwsIOException;

1.4channel.basicAck();

deliveryTag:该消息的index

multiple:是否批量.true:将一次性ack所有小于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

    */voidbasicAck(longdeliveryTag,booleanmultiple)throwsIOException;


1.5channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);

deliveryTag:该消息的index

multiple:是否批量.true:将一次性拒绝所有小于deliveryTag的消息。

requeue:被拒绝的是否重新入队列


/**    * Reject one or several received messages.

    *

    * Supply the deliveryTag from the {@link com.rabbitmq.client.AMQP.Basic.GetOk}

    * or {@link com.rabbitmq.client.AMQP.Basic.GetOk} method containing the message to be rejected.

    * @see com.rabbitmq.client.AMQP.Basic.Nack

    * @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 reject all messages up to and including

    * the supplied delivery tag; false to reject just the supplied

    * delivery tag.

    * @param requeue true if the rejected message(s) should be requeued rather

    * than discarded/dead-lettered

    * @throws java.io.IOException if an error is encountered

    */voidbasicNack(longdeliveryTag,booleanmultiple,boolean requeue)

            throwsIOException;

1.5channel.basicReject(delivery.getEnvelope().getDeliveryTag(), false);

deliveryTag:该消息的index

requeue:被拒绝的是否重新入队列

channel.basicNack 与 channel.basicReject 的区别在于basicNack可以拒绝多条消息,而basicReject一次只能拒绝一条消息

/**    * Reject a message. 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 rejected.

    * @see com.rabbitmq.client.AMQP.Basic.Reject

    * @param deliveryTag the tag from the received {@link com.rabbitmq.client.AMQP.Basic.GetOk} or {@link com.rabbitmq.client.AMQP.Basic.Deliver}

    * @param requeue true if the rejected message should be requeued rather than discarded/dead-lettered

    * @throws java.io.IOException if an error is encountered

    */voidbasicReject(longdeliveryTag,booleanrequeue)throwsIOException;

1.6 channel.basicConsume(QUEUE_NAME, true, consumer);

autoAck:是否自动ack,如果不自动ack,需要使用channel.ack、channel.nack、channel.basicReject 进行消息应答

/**    * Start a non-nolocal, non-exclusive consumer, with

    * a server-generated consumerTag.

    * @param queue the name of the queue

    * @param autoAck true if the server should consider messages

    * acknowledged once delivered; false if the server should expect

    * explicit acknowledgements

    * @param callback an interface to the consumer object

    * @return the consumerTag generated by the server

    * @throws java.io.IOException if an error is encountered

    * @see com.rabbitmq.client.AMQP.Basic.Consume

    * @see com.rabbitmq.client.AMQP.Basic.ConsumeOk

    * @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer)

    */    String basicConsume(String queue, booleanautoAck, Consumer callback)throwsIOException;

1.7 chanel.exchangeBind()


channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

用于通过绑定bindingKey将queue到Exchange,之后便可以进行消息接收


/**    * Bind an exchange to an exchange, with no extra arguments.

    * @see com.rabbitmq.client.AMQP.Exchange.Bind

    * @see com.rabbitmq.client.AMQP.Exchange.BindOk

    * @param destination the name of the exchange to which messages flow across the binding

    * @param source the name of the exchange from which messages flow across the binding

    * @param routingKey the routine key to use for the binding

    * @return a binding-confirm method if the binding was successfully created

    * @throws java.io.IOException if an error is encountered

    */    Exchange.BindOk exchangeBind(String destination, String source, String routingKey) throwsIOException;

1.8 channel.queueDeclare(QUEUE_NAME, false, false, false, null);


durable:true、false true:在服务器重启时,能够存活

exclusive :是否为当前连接的专用队列,在连接断开后,会自动删除该队列,生产环境中应该很少用到吧。

autodelete:当没有任何消费者使用时,自动删除该队列。this means that the queue will be deleted when there are no more processes consuming messages from it.


/**    * Declare a queue

    * @see com.rabbitmq.client.AMQP.Queue.Declare

    * @see com.rabbitmq.client.AMQP.Queue.DeclareOk

    * @param queue the name of the queue

    * @param durable true if we are declaring a durable queue (the queue will survive a server restart)

    * @param exclusive true if we are declaring an exclusive queue (restricted to this connection)

    * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)

    * @param arguments other properties (construction arguments) for the queue

    * @return a declaration-confirm method to indicate the queue was successfully declared

    * @throws java.io.IOException if an error is encountered

    */    Queue.DeclareOk queueDeclare(String queue, booleandurable,booleanexclusive,boolean autoDelete,

                                Map arguments)throwsIOException;

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,657评论 18 139
  • 来源 RabbitMQ是用Erlang实现的一个高并发高可靠AMQP消息队列服务器。支持消息的持久化、事务、拥塞控...
    jiangmo阅读 10,361评论 2 34
  • 为了一些初学习者更好理解我就从简单的解释一下Rabbitmq的原理吧​,首先你可以这样想RabbitMq就是一个队...
    螃蟹和骆驼先生Yvan阅读 7,404评论 6 4
  • 本文章翻译自http://www.rabbitmq.com/api-guide.html,并没有及时更新。 术语对...
    joyenlee阅读 7,658评论 0 3
  • 什么叫消息队列 消息(Message)是指在应用间传送的数据。消息可以非常简单,比如只包含文本字符串,也可以更复杂...
    lijun_m阅读 1,348评论 0 1