当前位置:网站首页>Activemq-- delayed delivery and scheduled delivery
Activemq-- delayed delivery and scheduled delivery
2022-07-25 09:18:00 【Why don't you laugh】
Delayed and timed message delivery
stay activemq.xml Active in file schedulerSupport Properties of :
| Property name | type | description |
|---|---|---|
| AMQ_SCHEDULED_DELAY | long | Delay message delivery time |
| AMQ_SCHEDULED_PERIOD | long | The time interval between repeated deliveries |
| AMQ_SCHEDULED_REPEAT | int | Number of repeated deliveries |
| AMQ_SCHEDULED_CRON | String | Use cron expression |
To configure schedulerSupport=true attribute :

Code
producer
public class JmsProducerDelayAndSheduler {
public static final String ACTIVEMQ_URL = "tcp://localhost:61616";
public static final String USERNAME = "admin";
public static final String PASSWORD = "hll123";
public static final String QUEUE_NAME = "queue01";
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, ACTIVEMQ_URL);
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE_NAME);
MessageProducer messageProducer = session.createProducer(queue);
int delay = 5000; // Delay delivery time
int period = 3000; // Repeat delivery interval
int repeat = 3; // Number of repeated deliveries
for (int i = 1; i < 3; i++) {
TextMessage textMessage = session.createTextMessage("delay msg:" + UUID.randomUUID().toString());
// Set through the message properties learned above Delayed delivery and scheduled delivery
// Be careful message The set attribute type should correspond to ,Long, Int
textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
// Use of delivery times setIntProperty
textMessage.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
messageProducer.send(textMessage);
}
messageProducer.close();
session.close();
connection.close();
System.out.println(" **** Message sent to MQ complete **** ");
}
}

consumer
public class JmsConsumerDelayAndScheduler {
// The code is the same as the normal consumer code
public static final String ACTIVEMQ_URL = "tcp://localhost:61616";
public static final String USERNAME = "admin";
public static final String PASSWORD = "hll123";
public static final String QUEUE_NAME = "queue01";
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, ACTIVEMQ_URL);
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE_NAME);
MessageConsumer messageConsumer = session.createConsumer(queue);
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
if (null != message && message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("**** Consumer receives message ****:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
System.in.read(); // You must add a line of code , Otherwise, the program will run directly down and end
messageConsumer.close();
session.close();
connection.close();
System.out.println("**** Consumer message complete ****");
}
}

边栏推荐
- Do you know these methods of MySQL database optimization?
- NFT guide for musicians
- ActiveMQ -- persistent mechanism
- Common tool classes under JUC package
- 有误差的字符串型时间比较方法String.compareTo
- activemq--死信队列
- js触屏小游戏源码冰雪之旅
- 酷炫canvas动画冲击波js特效
- Six storage types in C language: Auto register static extern const volatile
- Cool canvas animation shock wave JS special effect
猜你喜欢

activemq--可持久化机制之JDBC的journal

activemq--死信队列

Labview--- signal generator

Bi business interview with data center and business intelligence (I): preparation for Industry and business research

51 single chip microcomputer key control LED light status
![[C language] dynamic memory management, flexible array](/img/da/b9455885df0cb6646908e3655d62c5.png)
[C language] dynamic memory management, flexible array

Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?

分享一个避免递归的部门设计方法

Neural network learning (1) Introduction

360度拖拽全景图插件tpanorama.js
随机推荐
How to use pixi.js to make simple Parkour games
@Scheduled源码解析
艺术 NFT 的发展之路
28. Slot
JDBC的API解析
Arrange the array into the smallest number
Silicon Valley class lesson 11 - official account news and wechat authorization
Bi business interview with data center and business intelligence (I): preparation for Industry and business research
Additional: in the lower division / county (data sheet)
uniapp中scroll-view的坑
Canvas text JS special effect composed of many circles
Additional: SQL statement area / county in the middle half (data table)
【Nacos】NacosClient在服务注册时做了什么
JMeter test plan cannot be saved solution
Visual query (sp_helptext) -- quick query of stored procedures containing specified strings (with source code)
对称式加密与非对称式加密的对比
神经网络学习(1)前言介绍
c语言中的六个存储类型:auto register static extern const volatile
Sticky.js page scrolling div fixed position plug-in
[graduation project] cinema booking management system based on micro Service Framework