一、安装activeMQ

​ 安装步骤参照网上教程,本文不做介绍

二、修改activeMQ配置文件

​ broker新增配置信息 schedulerSupport="true"

  1. <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true" >
  2. <destinationPolicy>
  3. <policyMap>
  4. <policyEntries>
  5. <policyEntry topic=">" >
  6. <!-- The constantPendingMessageLimitStrategy is used to prevent
  7. slow topic consumers to block producers and affect other consumers
  8. by limiting the number of messages that are retained
  9. For more information, see:
  10. http://activemq.apache.org/slow-consumer-handling.html
  11. -->
  12. <pendingMessageLimitStrategy>
  13. <constantPendingMessageLimitStrategy limit="1000"/>
  14. </pendingMessageLimitStrategy>
  15. </policyEntry>
  16. </policyEntries>
  17. </policyMap>
  18. </destinationPolicy>

三、创建SpringBoot工程

  1. 配置ActiveMQ工厂信息,信任包必须配置否则会报错
  1. package com.example.demoactivemq.config;
  2. import org.apache.activemq.ActiveMQConnectionFactory;
  3. import org.apache.activemq.RedeliveryPolicy;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * @author shanks on 2019-11-12
  11. */
  12. @Configuration
  13. public class ActiveMqConfig {
  14. @Bean
  15. public ActiveMQConnectionFactory factory(@Value("${spring.activemq.broker-url}") String url){
  16. ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
  17. // 设置信任序列化包集合
  18. List<String> models = new ArrayList<>();
  19. models.add("com.example.demoactivemq.domain");
  20. factory.setTrustedPackages(models);
  21. return factory;
  22. }
  23. }
  1. 消息实体类
  1. package com.example.demoactivemq.domain;
  2. import lombok.Builder;
  3. import lombok.Data;
  4. import java.io.Serializable;
  5. /**
  6. * @author shanks on 2019-11-12
  7. */
  8. @Builder
  9. @Data
  10. public class MessageModel implements Serializable {
  11. private String titile;
  12. private String message;
  13. }
  1. 生产者
  1. package com.example.demoactivemq.producer;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.activemq.ScheduledMessage;
  4. import org.apache.activemq.command.ActiveMQQueue;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.autoconfigure.jms.JmsProperties;
  7. import org.springframework.jms.core.JmsMessagingTemplate;
  8. import org.springframework.stereotype.Service;
  9. import javax.jms.*;
  10. import java.io.Serializable;
  11. /**
  12. * 消息生产者
  13. *
  14. * @author shanks
  15. */
  16. @Service
  17. @Slf4j
  18. public class Producer {
  19. public static final Destination DEFAULT_QUEUE = new ActiveMQQueue("delay.queue");
  20. @Autowired
  21. private JmsMessagingTemplate template;
  22. /**
  23. * 发送消息
  24. *
  25. * @param destination destination是发送到的队列
  26. * @param message message是待发送的消息
  27. */
  28. public <T extends Serializable> void send(Destination destination, T message) {
  29. template.convertAndSend(destination, message);
  30. }
  31. /**
  32. * 延时发送
  33. *
  34. * @param destination 发送的队列
  35. * @param data 发送的消息
  36. * @param time 延迟时间
  37. */
  38. public <T extends Serializable> void delaySend(Destination destination, T data, Long time) {
  39. Connection connection = null;
  40. Session session = null;
  41. MessageProducer producer = null;
  42. // 获取连接工厂
  43. ConnectionFactory connectionFactory = template.getConnectionFactory();
  44. try {
  45. // 获取连接
  46. connection = connectionFactory.createConnection();
  47. connection.start();
  48. // 获取session,true开启事务,false关闭事务
  49. session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
  50. // 创建一个消息队列
  51. producer = session.createProducer(destination);
  52. producer.setDeliveryMode(JmsProperties.DeliveryMode.PERSISTENT.getValue());
  53. ObjectMessage message = session.createObjectMessage(data);
  54. //设置延迟时间
  55. message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
  56. // 发送消息
  57. producer.send(message);
  58. log.info("发送消息:{}", data);
  59. session.commit();
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. } finally {
  63. try {
  64. if (producer != null) {
  65. producer.close();
  66. }
  67. if (session != null) {
  68. session.close();
  69. }
  70. if (connection != null) {
  71. connection.close();
  72. }
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. }
  1. 消费者
  1. package com.example.demoactivemq.producer;
  2. import com.example.demoactivemq.domain.MessageModel;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.jms.annotation.JmsListener;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * 消费者
  8. */
  9. @Component
  10. @Slf4j
  11. public class Consumer {
  12. @JmsListener(destination = "delay.queue")
  13. public void receiveQueue(MessageModel message) {
  14. log.info("收到消息:{}", message);
  15. }
  16. }
  1. application.yml
  1. spring:
  2. activemq:
  3. broker-url: tcp://localhost:61616
  1. 测试类
  1. package com.example.demoactivemq;
  2. import com.example.demoactivemq.domain.MessageModel;
  3. import com.example.demoactivemq.producer.Producer;
  4. import org.junit.jupiter.api.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. @SpringBootTest(classes = DemoActivemqApplication.class)
  10. @RunWith(SpringRunner.class)
  11. class DemoActivemqApplicationTests {
  12. /**
  13. * 消息生产者
  14. */
  15. @Autowired
  16. private Producer producer;
  17. /**
  18. * 及时消息队列测试
  19. */
  20. @Test
  21. public void test() {
  22. MessageModel messageModel = MessageModel.builder()
  23. .message("测试消息")
  24. .titile("消息000")
  25. .build();
  26. // 发送消息
  27. producer.send(Producer.DEFAULT_QUEUE, messageModel);
  28. }
  29. /**
  30. * 延时消息队列测试
  31. */
  32. @Test
  33. public void test2() {
  34. for (int i = 0; i < 5; i++) {
  35. MessageModel messageModel = MessageModel.builder()
  36. .titile("延迟10秒执行")
  37. .message("测试消息" + i)
  38. .build();
  39. // 发送延迟消息
  40. producer.delaySend(Producer.DEFAULT_QUEUE, messageModel, 10000L);
  41. }
  42. try {
  43. // 休眠100秒,等等消息执行
  44. Thread.currentThread().sleep(100000L);
  45. } catch (InterruptedException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }

执行结果

  1. 2019-11-12 22:18:52.939 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 发送消息:MessageModel(titile=延迟10秒执行, message=测试消息0)
  2. 2019-11-12 22:18:52.953 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 发送消息:MessageModel(titile=延迟10秒执行, message=测试消息1)
  3. 2019-11-12 22:18:52.958 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 发送消息:MessageModel(titile=延迟10秒执行, message=测试消息2)
  4. 2019-11-12 22:18:52.964 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 发送消息:MessageModel(titile=延迟10秒执行, message=测试消息3)
  5. 2019-11-12 22:18:52.970 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 发送消息:MessageModel(titile=延迟10秒执行, message=测试消息4)
  6. 2019-11-12 22:19:03.012 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延迟10秒执行, message=测试消息0)
  7. 2019-11-12 22:19:03.017 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延迟10秒执行, message=测试消息1)
  8. 2019-11-12 22:19:03.019 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延迟10秒执行, message=测试消息2)
  9. 2019-11-12 22:19:03.020 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延迟10秒执行, message=测试消息3)
  10. 2019-11-12 22:19:03.021 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延迟10秒执行, message=测试消息4)

比你优秀的人比你还努力,你有什么资格不去奋斗!!!

SpringBoot之ActiveMQ实现延迟消息的更多相关文章

  1. SpringBoot - 集成RocketMQ实现延迟消息队列

    目录 前言 环境 具体实现 前言 RocketMQ是阿里巴巴在2012年开源的分布式消息中间件,记录下SpringBoot整合RocketMQ的方式,RocketMQ的安装可以查看:Windows下安 ...

  2. ActiveMQ延迟消息配置

    ActiveMQ使用延迟消息,需要在activemq.xml配置文件中添加这项: schedulerSupport="true" <broker xmlns="ht ...

  3. SpringBoot集成ActiveMq消息队列实现即时和延迟处理

    原文链接:https://blog.csdn.net/My_harbor/article/details/81328727 一.安装ActiveMq 具体安装步骤:自己谷歌去 二.新建springbo ...

  4. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  5. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  6. SpringBoot JMS(ActiveMQ) 使用实践

    ActiveMQ 1. 下载windows办的activeMQ后,在以下目录可以启动: 2. 启动后会有以下提示 3. 所以我们可以通过http://localhost:8161访问管理页面,通过tc ...

  7. Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ

    集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...

  8. springboot与ActiveMQ整合

    前言 很多项目, 都不是一个系统就做完了. 而是好多个系统, 相互协作来完成功能. 那, 系统与系统之间, 不可能完全独立吧? 如: 在学校所用的管理系统中, 有学生系统, 资产系统, 宿舍系统等等. ...

  9. RabbitMQ延迟消息学习

    准备做一个禁言自动解除的功能,立马想到了订单的超时自动解除,刚好最近在看RabbitMQ的实现,于是想用它实现,查询了相关文档发现确实可以实现,动手编写了这篇短文. 准备工作 1.Erlang安装请参 ...

随机推荐

  1. redis等缓存

    文章出处 https://www.cnblogs.com/wupeiqi/articles/5246483.html Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: ...

  2. sech和asech--双曲正割和反双曲正割函数

    sech和asech--双曲正割和反双曲正割函数 [功能简介]求变量的双曲正割和反双曲正割. [语法格式] 1.Y=sech(X) 计算X的双曲正割,sech(x)=1/cosh(x).X可以为向量. ...

  3. e课表项目第二次冲刺周期第六天

    昨天干了什么? 昨天是这次冲刺周期的第五天,我们的冲刺周期已经快过了一半,我们已经实现了对第一层界面的设计,所以我们的进度和我们的时间正好吻合,所以我们有信心完成我们的软件.我在网上搜了一些关于监听的 ...

  4. Java的Object类

    (1)Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类. (2)Object类的构造方法有一个,并且是无参构造 这其实就是理解当时我们说过,子类构造方法默认访问父类的构造是 ...

  5. postman动态数据获取

    1.以获取token(JWT)和uid为例 2.在登录接口的tests中写入代码(因为登录接口报文信息中有返回JWT和uid) 3.在其他接口中需要用到JWT和uid的地方设置变量{{JWT}}和{{ ...

  6. eclipse中的项目运行时不出现run as→java application选项

    eclipse中的运行java project时不出现run as→java application选项? 解决方案☞必须有正确的主方法,即public static void main(String ...

  7. CSP2019 考前复习

    动态规划 [NOIP2016]愤怒的小鸟(状压+思维) 多组数据题 共有i只猪,给出每只猪的坐标,鸟的飞行轨迹为经过原点的抛物线,求最少要多少只鸟能消灭所有的猪 \[ 猪数量n<=18 \] 看 ...

  8. Mybatis使用自定义类型转换Postgresql

    Mybatis使用自定义类型转换Postgresql 主要目的 为了解决从数据库取出来之后再手动转换为javaBean的问题. 主要用mybatis提供的Handler来把处理前置 添加转换类 imp ...

  9. Java_条件控制与循环控制

    条件控制语句: 1.     if-else语句 if(条件1){ 代码块1; }else if(条件2){ 代码块2; }else{ 代码块3; } 2.     switch语句 switch(变 ...

  10. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...