当前位置:网站首页>延时队列两种实现方式
延时队列两种实现方式
2022-07-02 05:11:00 【@淡 定】
rabbitmq_delayed_message_exchange插件
//配置文件 /properties:
spring.rabbitmq.host=localhost
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
spring.rabbitmq.virtual-host
//绑定队列和交换机,配置类
@Configuration
public class RabbitConfig {
public static final String QUEUE_NAME = "javaboy_delay_queue";
public static final String EXCHANGE_NAME = "javaboy_delay_exchange";
public static final String EXCHANGE_TYPE = "x-delayed-message";
@Bean
Queue queue() {
return new Queue(QUEUE_NAME, true, false, false);
}
@Bean
CustomExchange customExchange() {
Map<String, Object> args = new HashMap<>();
args.put("x-delayed-type", "direct");
return new CustomExchange(EXCHANGE_NAME, EXCHANGE_TYPE, true, false,args);
}
@Bean
Binding binding() {
return BindingBuilder.bind(queue())
.to(customExchange()).with(QUEUE_NAME).noargs();
}
}
//消费者
@Component
public class MsgReceiver {
private static final Logger logger = LoggerFactory.getLogger(MsgReceiver.class);
@RabbitListener(queues = RabbitConfig.QUEUE_NAME)
public void handleMsg(String msg) {
logger.info("handleMsg,{}",msg);
}
}
//生产者
@SpringBootTest
class MqDelayedMsgDemoApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void contextLoads() throws UnsupportedEncodingException {
Message msg = MessageBuilder.withBody(("hello world"+new Date()).getBytes("UTF-8")).setHeader("x-delay", 3000).build();
rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, RabbitConfig.QUEUE_NAME, msg);
}
}
使用死信队列方式
//properties配置文件
spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=8888
//死信队列和普通队列绑定配置类
@Configuration
public class QueueConfig {
public static final String JAVABOY_QUEUE_NAME = "javaboy_queue_name";
public static final String JAVABOY_EXCHANGE_NAME = "javaboy_exchange_name";
public static final String JAVABOY_ROUTING_KEY = "javaboy_routing_key";
public static final String DLX_QUEUE_NAME = "dlx_queue_name";
public static final String DLX_EXCHANGE_NAME = "dlx_exchange_name";
public static final String DLX_ROUTING_KEY = "dlx_routing_key";
/** * 死信队列 * @return */
@Bean
Queue dlxQueue() {
return new Queue(DLX_QUEUE_NAME, true, false, false);
}
/** * 死信交换机 * @return */
@Bean
DirectExchange dlxExchange() {
return new DirectExchange(DLX_EXCHANGE_NAME, true, false);
}
/** * 绑定死信队列和死信交换机 * @return */
@Bean
Binding dlxBinding() {
return BindingBuilder.bind(dlxQueue()).to(dlxExchange())
.with(DLX_ROUTING_KEY);
}
/** * 普通消息队列 * @return */
@Bean
Queue javaboyQueue() {
Map<String, Object> args = new HashMap<>();
//设置消息过期时间
args.put("x-message-ttl", 1000*10);
//设置死信交换机
args.put("x-dead-letter-exchange", DLX_EXCHANGE_NAME);
//设置死信 routing_key
args.put("x-dead-letter-routing-key", DLX_ROUTING_KEY);
return new Queue(JAVABOY_QUEUE_NAME, true, false, false, args);
}
/** * 普通交换机 * @return */
@Bean
DirectExchange javaboyExchange() {
return new DirectExchange(JAVABOY_EXCHANGE_NAME, true, false);
}
/** * 绑定普通队列和与之对应的交换机 * @return */
@Bean
Binding javaboyBinding() {
return BindingBuilder.bind(javaboyQueue())
.to(javaboyExchange())
.with(JAVABOY_ROUTING_KEY);
}
}
//死信队列消费者
@Component
public class DlxConsumer {
private static final Logger logger = LoggerFactory.getLogger(DlxConsumer.class);
@RabbitListener(queues = QueueConfig.DLX_QUEUE_NAME)
public void handle(String msg) {
logger.info(msg);
}
}
//不配置普通队列消费者,过期之后消息会自动进入死信队列进行消费
//生产者
@SpringBootTest
class DelayQueueApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void contextLoads() {
System.out.println(new Date());
rabbitTemplate.convertAndSend(QueueConfig.JAVABOY_EXCHANGE_NAME, QueueConfig.JAVABOY_ROUTING_KEY, "hello world!");
}
}
边栏推荐
- Oracle和MySQL的基本区别(入门级)
- Analyzing the hands-on building tutorial in children's programming
- Save the CDA from the disc to the computer
- How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
- LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
- Summary of MySQL key challenges (2)
- Lay the foundation for children's programming to become a basic discipline
- LeetCode 1175. 质数排列(质数判断+组合数学)
- How to make an RPM file
- Getting started with pytest ----- confitest Application of PY
猜你喜欢
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
农业生态领域智能机器人的应用
Application d'un robot intelligent dans le domaine de l'agroécologie
Orthogonal test method and function diagram method for test case design
Steam教育的实际问题解决能力
Mathematical knowledge (Euler function)
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
Fabric.js 居中元素
Here comes the chicken soup! Keep this quick guide for data analysts
奠定少儿编程成为基础学科的原理
随机推荐
leetcode存在重复元素go实现
[opencv] image binarization
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
6.30 year end summary, end of student age
Practical problem solving ability of steam Education
Set the default style of scroll bar Google browser
Collectors.groupingBy 排序
Summary of database problems
About PROFIBUS: communication backbone network of production plant
画波形图_数字IC
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
Summary of MySQL key challenges (2)
Leetcode 18 problem [sum of four numbers] recursive solution
國產全中文-自動化測試軟件Apifox
数学知识(欧拉函数)
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Find the subscript with and as the target from the array
Cubemx DMA notes