当前位置:网站首页>延时队列两种实现方式
延时队列两种实现方式
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!");
}
}
边栏推荐
- 解析少儿编程中的动手搭建教程
- Differential identities (help find mean, variance, and other moments)
- js面试收藏试题1
- Fabric.js 自由绘制矩形
- 黑馬筆記---Set系列集合
- Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
- [bus interface] Axi interface
- Leetcode basic programming: array
- MMAP zero copy knowledge point notes
- Pyflink writes MySQL examples with JDBC
猜你喜欢

Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I

Lm09 Fisher inverse transform inversion mesh strategy

Black Horse Notes - - set Series Collection

C case of communication between server and client based on mqttnet

Fabric.js IText 手动设置斜体

培养中小学生对教育机器人的热爱之心

Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing

Lay the foundation for children's programming to become a basic discipline

【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘

视差特效的原理和实现方法
随机推荐
Getting started with pytest ----- confitest Application of PY
Global and Chinese market of impact roll 2022-2028: Research Report on technology, participants, trends, market size and share
Briefly introduce chown command
Summary of MySQL key challenges (2)
Future trend of automated testing ----- self healing technology
Fabric.js 渐变
No logic is executed after the El form is validated successfully
Rhcsa --- work on the fourth day
Typescript function details
Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
将光盘中的cda保存到电脑中
国产全中文-自动化测试软件Apifox
leetcode存在重复元素go实现
Set the default style of scroll bar Google browser
Application d'un robot intelligent dans le domaine de l'agroécologie
Essence and physical meaning of convolution (deep and brief understanding)
6.30 year end summary, end of student age
The El cascader echo only selects the questions that are not displayed
黑马笔记---Map集合体系
Fabric.js IText 上标和下标