当前位置:网站首页>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!");
}
}
边栏推荐
- Fabric.js 精简JSON
- MMAP zero copy knowledge point notes
- Gee: remote sensing image composite and mosaic
- Feign realizes file uploading and downloading
- Video multiple effects production, fade in effect and border background are added at the same time
- Fabric.js IText 手动设置斜体
- Pyechart1.19 national air quality exhibition
- 线程池批量处理数据
- paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
- Implementation of leetcode two number addition go
猜你喜欢

Johnson–Lindenstrauss Lemma(2)

Gee dataset: chirps pentad high resolution global grid rainfall dataset

Super detailed pycharm tutorial

ansible安装与使用

2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路

CubeMx DMA笔记

Disable access to external entities in XML parsing

Fabric.js 激活输入框

06 decorator mode

No logic is executed after the El form is validated successfully
随机推荐
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
[opencv] image binarization
Go Chan's underlying principles
Fabric.js IText设置指定文字的颜色和背景色
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
js中的Map(含leetcode例题)
Application of intelligent robot in agricultural ecology
How to make an RPM file
Solution: the agent throws an exception error
线程池批量处理数据
Fabric.js 背景不受视口变换影响
Gee: remote sensing image composite and mosaic
国产全中文-自动化测试软件Apifox
[bus interface] Axi interface
Fabric.js IText 手动设置斜体
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Fabric.js 3个api设置画布宽高
Mathematical knowledge (Euler function)
The underlying principle of go map (storage and capacity expansion)