当前位置:网站首页>延时队列两种实现方式
延时队列两种实现方式
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!");
}
}
边栏推荐
- 国产全中文-自动化测试软件Apifox
- Mathematical knowledge -- understanding and examples of fast power
- List of common bugs in software testing
- leetcode存在重复元素go实现
- Oracle和MySQL的基本区别(入门级)
- Pyechart1.19 national air quality exhibition
- Lay the foundation for children's programming to become a basic discipline
- 06 装饰(Decorator)模式
- 从数组中找出和为目标的下标
- Pyechats 1.19 generate a web version of Baidu map
猜你喜欢

Go Chan's underlying principles

Analyzing the hands-on building tutorial in children's programming

Ansible installation and use

Fabric.js 右键菜单

10 minute quick start UI automation ----- puppeter

Here comes the chicken soup! Keep this quick guide for data analysts

解决:代理抛出异常错误

el-cascader回显只选中不显示的问题

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

【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
随机推荐
Fasttext text text classification
Fabric.js 激活输入框
How to configure PostgreSQL 12.9 to allow remote connections
Oracle和MySQL的基本区别(入门级)
LeetCode 1175. 质数排列(质数判断+组合数学)
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
[bus interface] Axi interface
解决:代理抛出异常错误
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
C # picture display occupancy problem
Leetcode basic programming: array
Ansible installation and use
Essence and physical meaning of convolution (deep and brief understanding)
Learn BeanShell before you dare to say you know JMeter
Video cover image setting, put cover images into multiple videos in the simplest way
Black Horse Notes - - set Series Collection
Mathematical knowledge (Euler function)
删除排序数组中的重复项go语言实现
Global and Chinese market of cell culture freezers 2022-2028: Research Report on technology, participants, trends, market size and share