网站关键字优化,知名建筑类的网站,销售类网站模板,易烊千玺网页设计模板代码RabbitMQ队列详细属性 1、队列的属性介绍1.1、Type#xff1a;队列类型1.2、Name#xff1a;队列名称1.3、Durability#xff1a;声明队列是否持久化1.4、Auto delete#xff1a; 是否自动删除1.5、Exclusive#xff1a;1.6、Arguments#xff1a;队列的其他属性#xf… RabbitMQ队列详细属性 1、队列的属性介绍1.1、Type队列类型1.2、Name队列名称1.3、Durability声明队列是否持久化1.4、Auto delete 是否自动删除1.5、Exclusive1.6、Arguments队列的其他属性例如指定DLX死信交换机等 2、队列的属性代码验证2.1、Auto delete2.1.1、RabbitConfig配置类关键代码2.1.2、发送消息2.1.3、接收消息2.1.4、启动类2.1.5、配置文件application.yml2.1.6、配置文件pom.xml2.1.7、测试 2.2、Arguments参数属性之x-overflow和x-max-length2.2.1、RabbitConfig配置类2.2.2、其他代码同上2.2.3、测试 2.3、Arguments参数属性之x-single-active-consumer2.3.1、消费者2.3.2、RabbitConfig配置类2.3.3、测试 2.4、Arguments参数属性之x-max-length-bytes2.5、Arguments参数属性之x-max-priority2.5.1、设置优先级2.5.2、测试 1、队列的属性介绍 1.1、Type队列类型 1.2、Name队列名称
就是一个字符串随便一个字符串就可以
1.3、Durability声明队列是否持久化
声明队列是否持久化代表队列在服务器重启后是否还存在默认值为true;
1.4、Auto delete 是否自动删除
如果为true当没有消费者连接到这个队列的时候队列会自动删除默认为false
1.5、Exclusive
exclusive属性的队列只对首次声明它的连接可见并且在连接断开时自动删除 基本上不设置它设置成false
1.6、Arguments队列的其他属性例如指定DLX死信交换机等
1、x-expiresNumber 当Queue队列在指定的时间未被访问则队列将被自动删除 在指定时间内队列未被使用没有被消费者访问就会自动删除该队列。
2、x-message-ttlNumber 发布的消息在队列中存在多长时间后被取消单位毫秒 消息发送到队列中后在指定时间后会过期。
3、x-overflowString 设置队列溢出行为当达到队列的最大长度时消息会发生什么有效值为Drop Head或Reject Publish 默认值是Drop Head即如果队列满了会从头部开始删除消息继续接收新的消息 Reject Publish拒绝发布如果队列满了就不再接收新的消息了。 4、x-max-lengthNumber 队列所能容下消息的最大长度当超出长度后新消息将会覆盖最前面的消息类似于Redis的LRU算法 5、 x-single-active-consumer默认为false 激活单一的消费者也就是该队列只能有一个消息者消费消息 如果设置该值为true,那么只能设置一个消费者接收消息。 6、x-max-length-bytesNumber 限定队列的最大占用空间当超出后也使用类似于Redis的LRU算法 7、x-dead-letter-exchangeString 指定队列关联的死信交换机有时候我们希望当队列的消息达到上限后溢出的消息不会被删除掉而是走到另一个队列中保存起来
8.x-dead-letter-routing-keyString 指定死信交换机的路由键一般和6一起定义
9.x-max-priorityNumber 如果将一个队列加上优先级参数那么该队列为优先级队列 1给队列加上优先级参数使其成为优先级队列 x-max-priority10【0-255取值范围】 2给消息加上优先级属性 通过优先级特性将一个队列实现插队消费 MessageProperties messagePropertiesnew MessageProperties(); messageProperties.setPriority(8);
10、x-queue-modeString理解下即可 队列类型x-queue-modelazy懒队列在磁盘上尽可能多地保留消息以减少RAM使用如果未设置则队列将保留内存缓存以尽可能快地传递消息 11、x-queue-master-locatorString用的较少 在集群模式下设置队列分配到的主节点位置信息 每个queue都有一个master节点所有对于queue的操作都是事先在master上完成之后再slave上进行相同的操作 每个不同的queue可以坐落在不同的集群节点上这些queue如果配置了镜像队列那么会有1个master和多个slave。 基本上所有的操作都落在master上那么如果这些queues的master都落在个别的服务节点上而其他的节点又很空闲这样就无法做到负载均衡那么势必会影响性能 关于master queue host 的分配有几种策略可以在queue声明的时候使用x-queue-master-locator参数或者在policy上设置queue-master-locator或者直接在rabbitmq的配置文件中定义queue_master_locator有三种可供选择的策略 1min-masters选择master queue数最少的那个服务节点host 2client-local选择与client相连接的那个服务节点host 3random随机分配
2、队列的属性代码验证
2.1、Auto delete 2.1.1、RabbitConfig配置类关键代码 package com.power.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class RabbitConfig {Value(${my.exchangeName})private String exchangeName;Value(${my.queueName})private String queueName;//创建交换机Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange(exchangeName).build();}//创建队列Beanpublic Queue queue(){return QueueBuilder.durable(queueName).autoDelete().build();}Beanpublic Binding binding(DirectExchange exchangeName,Queue queueName){return BindingBuilder.bind(queueName).to(exchangeName).with(info);}
}2.1.2、发送消息
package com.power.service;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Date;Service
Slf4j
public class MessageService {Resourceprivate RabbitTemplate rabbitTemplate;Value(${my.exchangeName})private String exchangeName;Beanpublic void sendMsg(){Message message MessageBuilder.withBody(hello world.getBytes()).build();rabbitTemplate.convertAndSend(exchangeName,info,message);log.info(消息发送完毕发送时间是new Date());}
}2.1.3、接收消息
package com.power.message;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;Component
Slf4j
public class ReceiveMessage {private static final String queueNamequeue.queueProperties.01;RabbitListener(queues{queueName})public void receiveMsg(Message message){log.info(接收到队列的消息{},new String(message.getBody()));}
}2.1.4、启动类
package com.power;import com.power.service.MessageService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import javax.annotation.Resource;SpringBootApplication
public class Application implements ApplicationRunner {Resourceprivate MessageService messageService;public static void main(String[] args) {SpringApplication.run(Application.class);}Overridepublic void run(ApplicationArguments args) throws Exception {messageService.sendMsg();}
}2.1.5、配置文件application.yml
server:port: 8080
spring:application:name: rabbit_11_quque_properties01_autoDeleterabbitmq:host: 你的服务器IPport: 5672username: 你的账号password: 你的密码virtual-host: powermy:exchangeName: exchange.queueProperties.01queueName: queue.queueProperties.012.1.6、配置文件pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.power/groupIdartifactIdrabbit_11_quque_properties01_autoDelete/artifactIdversion1.0-SNAPSHOT/versionnamerabbit_11_quque_properties01_autoDelete/namepropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.13/versionrelativePath//parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.24/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2.1.7、测试
启动服务消息发送成功接收消息成功 登录rabbitmq后台查看到有一个队列 此时我们停止服务即断掉消费者和队列的联系队列会自动删除。 2.2、Arguments参数属性之x-overflow和x-max-length
2.2.1、RabbitConfig配置类 package com.power.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;
import java.util.Map;Configuration
public class RabbitConfig {Value(${my.exchangeName})private String exchangeName;Value(${my.queueName})private String queueName;//创建交换机Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange(exchangeName).build();}//创建队列Beanpublic Queue queue(){MapString,Object arguments new HashMap();arguments.put(x-overflow,reject-publish);//队列的溢出行为默认是删除头部此处设置未拒绝发布当队列满了之后不再接收消息arguments.put(x-max-length,5);//队列的最大长度return new Queue(queueName,true,false,false,arguments);}Beanpublic Binding binding(DirectExchange directExchange,Queue queue){return BindingBuilder.bind(queue).to(directExchange).with(info);}
}2.2.2、其他代码同上
2.2.3、测试
启动服务登录rabbitmq后台。 我们发现当设置为arguments.put(“x-overflow”,“reject-publish”);和arguments.put(“x-max-length”,5);时我们发现队列里只存放了5条消息。后边的678三条消息并没有进入队列。
2.3、Arguments参数属性之x-single-active-consumer
2.3.1、消费者
我们设置多个消费者接收生产者发送的消息
package com.power.message;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Date;Component
Slf4j
public class ReceiveMessage {RabbitListener(queues{queue.properties.03})public void receiveMsg1(Message message){byte[] body message.getBody();String msg new String(body);log.info(1监听到的消息是msg,接收的时间是new Date());}RabbitListener(queues{queue.properties.03})public void receiveMsg2(Message message){byte[] body message.getBody();String msg new String(body);log.info(2监听到的消息是msg,接收的时间是new Date());}RabbitListener(queues{queue.properties.03})public void receiveMsg3(Message message){byte[] body message.getBody();String msg new String(body);log.info(3监听到的消息是msg,接收的时间是new Date());}}2.3.2、RabbitConfig配置类
package com.power.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;
import java.util.Map;Configuration
public class RabbitConfig {Value(${my.exchangeName})private String exchangeName;Value(${my.queueName})private String queueName;//创建交换机Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange(exchangeName).build();}//创建队列Beanpublic Queue queue(){MapString,Object arguments new HashMap();return new Queue(queueName,true,false,false,arguments);}Beanpublic Binding binding(DirectExchange directExchange,Queue queue){return BindingBuilder.bind(queue).to(directExchange).with(info);}
}2.3.3、测试
启动服务我们发现当不设置该参数时3个消费者都可以接收到消息
当设置了该参数时只有一个消费者可以接收到消息 2.4、Arguments参数属性之x-max-length-bytes
如下图所示修改RabbitConfig类设置队列的x-max-length-bytes属性值为100 尝试发送100条消息 启动服务登录rabbitmq后台 可以看到队列中只有7条消息
2.5、Arguments参数属性之x-max-priority
2.5.1、设置优先级
给队列设置优先级 给队列中的消息设置优先级
2.5.2、测试
启动服务登录rabbitmq后台