当前位置:网站首页>Two implementation methods of delay queue
Two implementation methods of delay queue
2022-07-02 05:16:00 【@Calm down】
rabbitmq_delayed_message_exchange plug-in unit
// The configuration file /properties:
spring.rabbitmq.host=localhost
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
spring.rabbitmq.virtual-host
// Bind queues and switches , Configuration class
@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();
}
}
// consumer
@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);
}
}
// producer
@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);
}
}
Use dead letter queue mode
//properties The configuration file
spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=8888
// Dead letter queue and ordinary queue binding configuration class
@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";
/** * Dead letter queue * @return */
@Bean
Queue dlxQueue() {
return new Queue(DLX_QUEUE_NAME, true, false, false);
}
/** * Dead letter switch * @return */
@Bean
DirectExchange dlxExchange() {
return new DirectExchange(DLX_EXCHANGE_NAME, true, false);
}
/** * Bind dead letter queue and dead letter switch * @return */
@Bean
Binding dlxBinding() {
return BindingBuilder.bind(dlxQueue()).to(dlxExchange())
.with(DLX_ROUTING_KEY);
}
/** * Normal message queue * @return */
@Bean
Queue javaboyQueue() {
Map<String, Object> args = new HashMap<>();
// Set message expiration time
args.put("x-message-ttl", 1000*10);
// Set up dead letter switch
args.put("x-dead-letter-exchange", DLX_EXCHANGE_NAME);
// Set dead letter routing_key
args.put("x-dead-letter-routing-key", DLX_ROUTING_KEY);
return new Queue(JAVABOY_QUEUE_NAME, true, false, false, args);
}
/** * General switch * @return */
@Bean
DirectExchange javaboyExchange() {
return new DirectExchange(JAVABOY_EXCHANGE_NAME, true, false);
}
/** * Bind the normal queue and the corresponding switch * @return */
@Bean
Binding javaboyBinding() {
return BindingBuilder.bind(javaboyQueue())
.to(javaboyExchange())
.with(JAVABOY_ROUTING_KEY);
}
}
// Dead letter queue consumers
@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);
}
}
// Do not configure ordinary queue consumers , After expiration, the message will automatically enter the dead letter queue for consumption
// producer
@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!");
}
}
边栏推荐
- 运维工作的“本手、妙手、俗手”
- LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
- Exercise notes 13 (effective letter ectopic words)
- Dark horse notes -- Set Series Collection
- Gee: create a new feature and set corresponding attributes
- Application d'un robot intelligent dans le domaine de l'agroécologie
- Leetcode18题 【四数之和】递归解法
- 黑马笔记---Map集合体系
- Lay the foundation for children's programming to become a basic discipline
- Black Horse Notes - - set Series Collection
猜你喜欢
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
将光盘中的cda保存到电脑中
Using QA band and bit mask in Google Earth engine
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
Ansible installation and use
農業生態領域智能機器人的應用
LeetCode 1175. 质数排列(质数判断+组合数学)
Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
Cubemx DMA notes
Analyzing the hands-on building tutorial in children's programming
随机推荐
C case of communication between server and client based on mqttnet
Record my pytorch installation process and errors
Gee dataset: chirps pentad high resolution global grid rainfall dataset
Cultivate primary and secondary school students' love for educational robots
摆正元素(带过渡动画)
Fabric.js 圆形笔刷
Feign realizes file uploading and downloading
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
How to configure PostgreSQL 12.9 to allow remote connections
Save the CDA from the disc to the computer
There are duplicate elements in leetcode. Go implementation
Preparation for writing SAP ui5 applications using typescript
Find the subscript with and as the target from the array
7.1 Résumé du concours de simulation
Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
Gee data set: export the distribution and installed capacity of hydropower stations in the country to CSV table
Pyechart1.19 national air quality exhibition
黑马笔记---Set系列集合
[opencv] image binarization