rabbitmq 中的队列 交换机 路由的创建与绑定介绍以及队列参数介绍

x-message-ttl:number
How long a message published to a queue can live before it is discarded (milliseconds).
(Sets the "x-message-ttl" argument.)
x-expires:number
How long a queue can be unused for before it is automatically deleted (milliseconds).
(Sets the "x-expires" argument.)
x-max-length:number
How many (ready) messages a queue can contain before it starts to drop them from its head.
x-max-length-bytes:number
Total body size for ready messages a queue can contain before it starts to drop them from its head.
x-overflow:string:number
Sets the queue overflow behaviour. This determines what happens to messages when the maximum length of a queue is reached.
 Valid values are drop-head, reject-publish or reject-publish-dlx. The quorum queue type only supports drop-head.
x-dead-letter-exchange:string
Optional name of an exchange to which messages will be republished if they are rejected or expire.
x-dead-letter-routing-key:string
Optional replacement routing key to use when a message is dead-lettered. If this is not set, the message's original routing key will be used.
x-single-active-consumer:boolean
If set, makes sure only one consumer at a time consumes from the queue and fails 
over to another registered consumer in case the active one is cancelled or dies.
x-max-priority:number
Maximum number of priority levels for the queue to support; if not set, the queue will not support message priorities.
x-queue-mode:lazy
Set the queue into lazy mode, keeping as many messages as possible on disk to reduce RAM usage; if not set, 
the queue will keep an in-memory cache to deliver messages as fast as possible.
x-queue-master-locator:string
Set the queue into master location mode, 
determining the rule by which the queue master is located when declared on a cluster of nodes.

mq 中的交换机 路由 队列的绑定示例:code eg1:

Logger logger = LoggerFactory.getLogger(MqTest.class);
public static final String DEAD_EXCHANGE = "dead_exchange";
public static final String DEAD_QUEUE = "dead_queue";
public static final String DEAD_ROUT_KEY = "dead_route";

public static final String NORMAL_EXCHANGE = "normal_exchange";
public static final String NORMAL_QUEUE = "normal_queue";
public static final String NORMAL_ROUT_KEY = "normal_route";

@Test
void initQueueAndExchange() throws Exception {
    //队列 交换机 路由的绑定
    Channel channel = RabbitmqUtil.getChannel(false);
    // 死信交换机
    channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
    // 普通交换机
    channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);

    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("x-dead-letter-exchange", DEAD_EXCHANGE);
    hashMap.put("x-dead-letter-routing-key", DEAD_ROUT_KEY);
    hashMap.put("x-max-length", 6);

    channel.queueDeclare(NORMAL_QUEUE, false, false, false, hashMap);
    channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, NORMAL_ROUT_KEY);
    channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
    channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, DEAD_ROUT_KEY);
    logger.info("init ok ");
}

eg2 :

import com.lt.springcloudcommon.mqfile.MqConts;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author admin
 * @description DemoMqConfig
 * @date 2021/12/29 16:15
 */
@Configuration
public class DemoMqConfig {
    @Bean
    public Queue directQueue1() {
        return new Queue(MqConts.testQueue);
    }

    @Bean
    public DirectExchange directExchangeDemo() {
        // 三个构造参数:name durable autoDelete
        return new DirectExchange(MqConts.testExchange, false, false);
    }

    @Bean
    public Binding directBinding1() {
        return BindingBuilder.bind(directQueue1()).to(directExchangeDemo()).with(MqConts.testRoutKey);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40408317/article/details/122242564