栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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;


@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);
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号