当前位置:网站首页>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!");
}
}
边栏推荐
- C # picture display occupancy problem
- The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
- Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Dark horse notes -- Set Series Collection
- Cultivate primary and secondary school students' love for educational robots
- 黑马笔记---Map集合体系
- Briefly introduce chown command
- Dark horse notes -- map set system
- [high speed bus] Introduction to jesd204b
- Draw a wave chart_ Digital IC
猜你喜欢
Video cover image setting, put cover images into multiple videos in the simplest way
Fabric.js IText 上标和下标
摆正元素(带过渡动画)
4. Flask cooperates with a tag to link internal routes
Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
Ansible installation and use
Fabric.js 将本地图像上传到画布背景
Gee: remote sensing image composite and mosaic
el form 表单validate成功后没有执行逻辑
C case of communication between server and client based on mqttnet
随机推荐
Gee: create a new feature and set corresponding attributes
Collectors. Groupingby sort
C # picture display occupancy problem
Gee series: Unit 2 explore datasets
Pyflink writes MySQL examples with JDBC
Video multiple effects production, fade in effect and border background are added at the same time
Application of intelligent robot in agricultural ecology
Cultivate primary and secondary school students' love for educational robots
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
Analyzing the hands-on building tutorial in children's programming
js面试收藏试题1
黑马笔记---Set系列集合
Fabric.js 基础笔刷
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
画波形图_数字IC
Ansible installation and use
Record my pytorch installation process and errors
The underlying principle of go map (storage and capacity expansion)
从数组中找出和为目标的下标
4. Flask cooperates with a tag to link internal routes