当前位置:网站首页>activemq--延迟投递和定时投递
activemq--延迟投递和定时投递
2022-07-25 09:12:00 【你怎么不笑了】
延迟和定时消息投递
在activemq.xml文件里激活schedulerSupport的属性:
| Property name | type | description |
|---|---|---|
| AMQ_SCHEDULED_DELAY | long | 延迟消息投递时间 |
| AMQ_SCHEDULED_PERIOD | long | 重复投递的时间间隔 |
| AMQ_SCHEDULED_REPEAT | int | 重复投递次数 |
| AMQ_SCHEDULED_CRON | String | 使用cron表达式 |
配置schedulerSupport=true属性:

代码
生产者
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; //延迟投递时间
int period = 3000; //重复投递间隔时间
int repeat = 3; //重复投递次数
for (int i = 1; i < 3; i++) {
TextMessage textMessage = session.createTextMessage("delay msg:" + UUID.randomUUID().toString());
// 通过前面了解的消息属性来设置 延迟投递和定时投递
// 注意 message设置的属性类型要对应上,Long, Int
textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
// 投递次数的用setIntProperty
textMessage.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
messageProducer.send(textMessage);
}
messageProducer.close();
session.close();
connection.close();
System.out.println(" **** 消息发送到MQ完成 **** ");
}
}

消费者
public class JmsConsumerDelayAndScheduler {
// 代码同正常的消费者代码
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("**** 消费者接收到消息 ****:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
System.in.read(); // 必须加行代码,不然程序会直接往下执行结束了
messageConsumer.close();
session.close();
connection.close();
System.out.println("**** 消费者消费消息完成 ****");
}
}

边栏推荐
- Django4.0 + web + MySQL 5.7 realize simple login operation
- Yolov5 environment configuration
- 51单片机内部外设:定时器和计数器
- canvas很多圆圈组成的文本js特效
- NFT guide for musicians
- 附加:在在下部分区/县(数据表)
- 全网最简约的sklearn环境配置教程(百分百成功)
- [STL]stack&queue模拟实现
- Redis sentry, master-slave deployment details
- [stl]stack & queue simulation implementation
猜你喜欢

js弹出式城市筛选组件匹配手机移动端

Labview--- signal generator
![[NPM] the](/img/ae/efccefae0323a1f6a425523e01d2ac.png)
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
![[hero planet July training leetcode problem solving daily] 19th binary tree](/img/16/d4beab998f00e09bb45c64673bb2c8.png)
[hero planet July training leetcode problem solving daily] 19th binary tree

Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)

Feiling ok1028a core board adapts to rtl8192cu WiFi module

Ctfhub skill tree Web

Solve the syntaxerror: unexpected end of JSON input

The simplest sklearn environment configuration tutorial in the whole network (100% success)

Dependency conflict resolution under idea
随机推荐
Canvas text JS special effect composed of many circles
How to connect tdengine with idea database tool?
Additional: in the lower division / county (data sheet)
Dark horse programmer JDBC
Unity ugui interaction (new ideas)
Network principle (2) -- network development
[stl]stack & queue simulation implementation
图解LeetCode——1184. 公交站间的距离(难度:简单)
Do you know these methods of MySQL database optimization?
51 MCU internal peripherals: serial port communication
[BUUCTF-n1book][第二章 web进阶]SSRF Training
附加:在在下部分区/县(数据表)
uniapp中scroll-view的坑
What are the commands used by pl/sql tools to export SQL files?
The simplest sklearn environment configuration tutorial in the whole network (100% success)
Illustration leetcode - 919. Complete binary tree inserter (difficulty: medium)
API parsing of JDBC
Solve NPM error: cannot find module 'shelljs‘
Robot jumping problem
Algorithm --- flip digit (kotlin)