SpringBoot整合ActivityMQ

一、安装及启动ActivityMQ

1、下载ActivityMQ
进去ActivityMQ官网下载自己对应版本的ActivityMQ包

http://activemq.apache.org/components/classic/download/


2、连接服务器或者虚拟机,将下载好的ActivityMQ包上传上去后解压

3、运行ActivityMQ
进入ActivityMQ bin目录,使用 ./activemq start 命令启动ActivityMQ ,使用 ./activemq status 查询ActivityMQ运行状态。

启动好后在浏览器输入http://...:8161 默认用户名和密码都是admin

二、创建项目

  • 添加maven的依赖,导入相应的jar包
  • 配置文件添加ActivityMQ相关配置
spring.activemq.broker-url=tcp://127.0.0.1:61616
# 在考虑结束之前等待的时间
spring.activemq.close-timeout=15s
# 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
spring.activemq.in-memory=true
# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
spring.activemq.non-blocking-redelivery=false
# 等待消息发送响应的时间。设置为0等待永远。
spring.activemq.send-timeout=0
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true
#账号
spring.activemq.user=admin
# 密码
spring.activemq.password=admin
# 是否信任所有包
#spring.activemq.packages.trust-all=
# 要信任的特定包的逗号分隔列表(当不信任所有包时)
#spring.activemq.packages.trusted=
# 当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。
#spring.activemq.pool.block-if-full=true
# 如果池仍然满,则在抛出异常前阻塞时间。
#spring.activemq.pool.block-if-full-timeout=-1ms
# 是否在启动时创建连接。可以在启动时用于加热池。
#spring.activemq.pool.create-connection-on-startup=true
# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。
#spring.activemq.pool.enabled=false
# 连接过期超时。
#spring.activemq.pool.expiry-timeout=0ms
# 连接空闲超时
#spring.activemq.pool.idle-timeout=30s
# 连接池最大连接数
#spring.activemq.pool.max-connections=1
# 每个连接的有效会话的最大数目。
#spring.activemq.pool.maximum-active-session-per-connection=500
# 当有"JMSException"时尝试重新连接
#spring.activemq.pool.reconnect-on-exception=true
# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。
#spring.activemq.pool.time-between-expiration-check=-1ms
# 是否只使用一个MessageProducer
#spring.activemq.pool.use-anonymous-producers=true
  • 创建配置文件ActivityMQConfig.calss
@Configuration
@EnableJms
public class Config {/*** 点对点模式*/@Beanpublic Queue queue(){return new ActiveMQQueue("active.queue");}/*** 发布/订阅模式*/@Beanpublic Topic topic(){return new ActiveMQTopic("active.topic");}
}
  • 创建生产者
public interface ProductService {void sendMessage(Destination destination, String message);
}
@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;@Overridepublic void sendMessage(Destination destination, String message) {jmsMessagingTemplate.convertAndSend(destination,message);}
}
  • 创建消费者
  • queue
@Service
public class ConsumerQueue {/*** 使用JmsListener配置消费者监听的队列,其中text是接收到的消息*/@JmsListener(destination = "active.queue")public void receiveQueue(String text) {System.out.println("Consumer收到:"+text+",类型:active.queue");}
}
  • topic
@Service
public class ConsumerTopic {/*** 使用JmsListener配置消费者监听的队列,其中text是接收到的消息*/@JmsListener(destination = "active.topic")public void receiveQueue(String text) {System.out.println("Consumer收到:"+text+",类型:active.topic");}
}
  • 创建Controller
@RestController
@Slf4j
@RequestMapping("/mq")
public class ActiveMqController {@Autowiredprivate Queue queue;@Autowiredprivate Topic topic;@Autowiredprivate ProductService productService;@RequestMapping(value = "/test",method = RequestMethod.GET)public String Test(String msg){return msg;}@RequestMapping(value = "/queue",method = RequestMethod.GET)public String sendQueue(String msg){productService.sendMessage(this.queue,msg);return msg;}@RequestMapping(value = "/topic",method = RequestMethod.GET)public String sendTopic(String msg){productService.sendMessage(this.topic,msg);return msg;}
}
  • 运行项目,访问项目路径

  • queue

#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=false

http://localhost:8081/mq/queue?msg=123

消费者监听到并获取到queue模式的MQ消息

  • topic
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true

http://localhost:8081/mq/topic?msg=123


消费者监听到并获取到topic模式的MQ消息

  • 双向模式
    1、生产者中添加接收返回消息
@JmsListener(destination = "receive-queue")public void receiveQueue(String text) {System.out.println("Product收到Consumer回复:"+text);}

2、创建返回消息的消费者

@Service
public class Message {@JmsListener(destination = "active.queue")@SendTo("receive-queue")public String Message(String message){System.out.println("收到:"+message);return "收到:"+message;}
}

3、 启动项目

访问 http://localhost:8081/mq/queue?msg=123


整体项目的整合完毕
整个项目使用到activityMQ,和springboot整合,在pringboot的配置文件中配置activityMQ的相关信息,包括链接地址,账号,密码,连接时间等等
在生产者调用JmsMessagingTemplate进行发送消息到mq
消费者监听到消息,就会进行消费

Java常用中间件---SpringBoot整合ActivityMQ相关推荐

  1. java中间件有哪些java常用中间件介绍

    之前给大家介绍了很多的java基础知识,今天同样的也是要给大家介绍这方面的内容,那么对于java中间件你都了解多少呢究竟什么是java中间件比较常用的java中间件都有哪些一起来了解一下吧. 一.什么 ...

  2. 【Java进阶】SpringBoot整合Redis

    SpringBoot整合Redis SpringBoot 操作数据:spring-data jpa jdbc mongodb redis SpringData 也是和 SpringBoot 齐名的项目 ...

  3. Java学习之SpringBoot整合SSM Demo

    背景:在Java Web中Spring家族有着很重要的地位,之前JAVA开发需要做很多的配置,一堆的配置文件和部署调试一直是JavaWeb开发中的一大诟病,但现在Spring推出了SpringBoot ...

  4. Java常用工具类整合

    JSON转换工具 package com.taotao.utils;import java.util.List;import com.fasterxml.jackson.core.JsonProces ...

  5. Java常用工具类整合(史上最全)

    JSON转换工具 package com.taotao.utils;import java.util.List;import com.fasterxml.jackson.core.JsonProces ...

  6. ueditor编辑器java使用_java/springboot整合UEditor编辑器

    官网下载jsp版ueditor放进项目,其实放在哪个目录看项目习惯(有些按照网上的来就是路径问题导致),主要是获取配置的时候能找到相应的路径. 页面上引入相应js,加上也可script方式 uedit ...

  7. java集群解析文件_java相关:springboot整合redis集群过程解析

    java相关:springboot整合redis集群过程解析 发布于 2020-4-13| 复制链接 摘记: 简介 在springboot使用搭建好的redis集群添加redis和连接池依赖 ```x ...

  8. springboot整合JPA+MYSQL+queryDSL数据增删改查

    Spring Boot Jpa 是 Spring 基于 ORM 框架.Jpa 规范的基础上封装的一套 Jpa 应用框架,可使开发者用极简的代码即可实现对数据的访问和操作.它提供了包括增删改查等在内的常 ...

  9. Springboot整合Netty,实现Socket通信

    文章目录 *Springboot整合Netty,实现Socket通信* 1.模拟单客户端 2.模拟单服务端 总结 Springboot整合Netty,实现Socket通信 1.模拟单客户端 引入Net ...

  10. Springboot整合JPA

    文章目录 JPA技术 常用注解 Springboot整合JPA 1.引入JPA依赖 2.配置 3.启动类 4.实体类 5.定义接口和数据库交互(dao) 6.JPA中使用原生态的SQL语句 7.Tes ...

最新文章

  1. 实战 | 深度学习轻松学:如何用可视化界面来部署深度学习模型
  2. MySQL配置文件mysql.ini参数详解
  3. 阿里云API网关(8)开发指南-SDK下载
  4. 几个小例子告诉你, 一行Python代码能干哪些事
  5. verilog中的代码使用
  6. mos管结电容等效模型_为什么我的mos管炸了???
  7. iOS 项目经验以及APP上架流程 _Dylan
  8. html仿苹果浏览器,完美仿iPhone风格主题 领航浏览器体验
  9. [BZOJ1834][ZJOI2010]network 网络扩容 最大流+费用流
  10. 信息学奥赛C++语言:社会实践任务
  11. 架构中的设计原则之单一职责原则 - 《java开发技术-在架构中体验设计模式和算法之美》...
  12. 2015-12-02 定时自动执行存储过程
  13. swift菜鸟入门视频教程-02-基本运算符
  14. 电流检测时运放的偏置电流对精度的影响
  15. 【MySQL基础】03:约束与运算符
  16. supermap java,SuperMap iObjects Java 10i 产品介绍
  17. XE中FMX操作ListBox,添加上千条记录(含图片)
  18. 计算机安全模式win7,Win7如何进入计算机安全模式?
  19. svn服务器现存的库文件导入,svn导入版本库及相关知识
  20. 神策数据如何帮助企业实现营销自动化?

热门文章

  1. flash电脑安装包_Flash动画制作,Animate CC 2019下载安装
  2. java抓包WIFI包_教你手机应用如何通过wifi上网抓包
  3. SAP物料批次管理配置及操作手册
  4. 蓝桥杯题目 abcde/fghij=n
  5. CSDN免费获得积分和直接获取下载码的方法,亲测有效
  6. java中的递归算法_java递归算法
  7. 建模学习笔记(一)层次分析法模型学习及相关论文书写 清风数学建模
  8. 线性反馈移位寄存器(LFSR,Linear Feedback Shift Register)
  9. [样本分析] Ramnit感染型病毒
  10. visio一分二的箭头_Microsoft Office Visio绘画双箭头直线的具体步骤介绍