RabbitMQ - Channel 类主要方法参数详解

本文详细介绍了RabbitMQ中Channel类的exchangeDeclare和queueDeclare方法,用于声明交换器和队列,包括参数的含义和用法。此外,还提到了queueBind和exchangeBind方法,用于交换器和队列之间的绑定,以及自定义绑定参数。这些方法在消息中间件的配置中至关重要,确保了消息路由的正确性和持久性。
摘要由CSDN通过智能技术生成

Channel 类的方法,如 exchangeDeclare(...)queueDeclare(...)等方法,均采用了重载的方式。

channel.exchangeDeclare(…)

    /**
     * Declare an exchange, via an interface that allows the complete set of
     * arguments.
     * @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 internal true if the exchange is internal, i.e. can't be directly
     * published to by a client.
     * @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,
                                              boolean durable,
                                              boolean autoDelete,
                                              boolean internal,
                                              Map<String, Object> arguments) throws IOException;
次序名称类型示例解释
1exchangeString“normalExchange”交换器的名称
2typeString“direct”交换器的类型,可选值有:direct , fanout , topic, headers
3durablebooleantrue设置是否持久化。true 持久化。持久化表示可以将交换器存盘,rabbitMQ重启,相关信息不会丢失
4autoDeletebooleanfalse设置是否自动删除。true 会自动删除。自动删除的前提是至少有一个队列或交换器与这个交换器绑定,之后所有与这个交换器绑定的队列或交换器都解除。
5internalbooleanfalse设置是否是内置的。true 是内置的交换器。客户端无法直接发送消息至这个交换器,只能通过交换器到交换器的这种方式。
6argumentsMapnull其它结构化的一些参数。如制定args.put("alternate-exchange", "myAE");

channel.queueDeclare(…)

    /**
     * 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, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments) throws IOException;
次序名称类型示例解释
1queueString“normalQueue”队列的名称
2durablebooleantrue设置是否持久化。true 持久化。RabbitMQ 重启后,队列还会存在,不会丢失。
3exclusivebooleanfalse设置是否具有排他性。如果一个队列设置为排他队列,该队列仅对首次声明他的连接可见,并在连接断开时自动删除。这里需要注意三点:排他队列是基于连接( Connection)可见的,同一个连接的不同信道 (Channel)是可以同时访问同一连接创建的排他队列;"首次"是指如果一个连接己经声明了排他队列,其他连接是不允许建立同名的排他队列的,这个与普通队列不同:即使该队列是持久化的,一旦连接关闭或者客户端退出,该排他队列都会被自动删除,这种队列适用于一个客户端同时发送和读取消息的应用场景。
4autoDeletebooleanfalse设置是否自动删除。true 自动删除。自动删除的意思是至少有一个消费者连接这个队列,之后与这个队列连接的消费者都断开后,就自动删除该队列。
5argumentsMapnull设置队列的其他一些参数,如 x-message-ttl、x-expires、x-max-length、x-max-length-bytes、 x-dead-letter-exchange、x-dead-letter-routing-key、x-max-priority等。

Channel.queueBind(…)

    /**
     * Bind a queue to an exchange.
     * @see com.rabbitmq.client.AMQP.Queue.Bind
     * @see com.rabbitmq.client.AMQP.Queue.BindOk
     * @param queue the name of the queue
     * @param exchange the name of the exchange
     * @param routingKey the routing key to use for the binding
     * @param arguments other properties (binding parameters)
     * @return a binding-confirm method if the binding was successfully created
     * @throws java.io.IOException if an error is encountered
     */
    Queue.BindOk queueBind(String queue, String exchange, String routingKey, Map<String, Object> arguments) throws IOException;
次序名称类型示例解释
1queueString“normalQueue”队列名称。
2exchangeString“normalExchange”交换器名称。
3routingKeyString“normal”路由键。
4argumentsMapnull自定义绑定的参数。

Channel.exchangeBind(…)

    /**
     * Bind an exchange to an exchange.
     * @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 routing key to use for the binding
     * @param arguments other properties (binding parameters)
     * @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, Map<String, Object> arguments) throws IOException;
次序名称类型示例解释
1destinationStringdestination需要绑定的目标交换器名称。
2sourceStringsource需要绑定的交换器名称。
3routingKeyStringexKey路由键。
4argumentsMapnull定义化参数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值