SpringBoot 整合ActiveMQ_企业实战

在这里插入图片描述

文章目录

  • 1. 新建Springboot工程
  • 2. 引入maven依赖
  • 3. ActiveMq配置类
  • 4. MQ生产者
  • 5. MQ 点对点消费者
  • 6. MQ 发布点阅消费者A
  • 7. MQ 发布点阅消费者B
  • 8. 统一测试类

1. 新建Springboot工程

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

2. 引入maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gblfy</groupId><artifactId>springboot-activemq</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-activemq</name><description>Spring Boot集成Activemq</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><!--全局编码设置--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!--JDK版本--><java.version>1.8</java.version><!--全局版本--><fastjson.version>1.2.58</fastjson.version><lombok.version>1.18.8</lombok.version></properties><dependencies><!--SpringMVC 启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Activemq Start--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId></dependency><!-- Activemq End--><!--数据处理--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency><!--lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><!--单元测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><!--maven编译插件--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3. ActiveMq配置类

package com.gblfy.activemq.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;import javax.jms.ConnectionFactory;/*** @author gblfy* @ClassNme ActiveMqConfig* @Description Mq配置类* @Date 2019/9/3 18:05* @version1.0*/
@Configuration
public class ActiveMqConfig {// queue模式的ListenerContainer@Beanpublic JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();bean.setConnectionFactory(activeMQConnectionFactory);return bean;}// topic模式的ListenerContainer@Beanpublic JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();bean.setPubSubDomain(true);bean.setConnectionFactory(activeMQConnectionFactory);return bean;}}

4. MQ生产者

package com.gblfy.activemq.producer;import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;import java.io.Serializable;
import java.util.List;
import java.util.Map;/*** @author gblfy* @ClassNme MqProducer* @Description Mq 生产者封装公共类* @Date 2019/9/3 18:05* @version1.0*/
@Service
public class MqProducer {@Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;/*** 发送字符串消息队列** @param queueName 队列名称* @param message   字符串*/public void sendStringQueue(String queueName, String message) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), message);}/*** 发送字符串集合消息队列** @param queueName 队列名称* @param list      字符串集合*/public void sendStringListQueue(String queueName, List<String> list) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), list);}/*** 发送Map消息队列** @param queueName* @param headers*/public void sendMapQueue(String queueName, Map<String, Object> headers) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), headers);}/*** 发送对象消息队列** @param queueName 队列名称* @param obj       对象*/public void sendObjQueue(String queueName, Serializable obj) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), obj);}/*** 发送对象集合消息队列** @param queueName 队列名称* @param objList   对象集合*/public void sendObjListQueue(String queueName, List<Serializable> objList) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), objList);}/*** 发送字符串消息主题** @param topicName 主题名称* @param message   字符串*/public void sendStringTopic(String topicName, String message) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topicName), message);}/*** 发送字符串集合消息主题** @param topicName 主题名称* @param list      字符串集合*/public void sendStringListTopic(String topicName, List<String> list) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topicName), list);}/*** 发送Map消息主题** @param queueName* @param headers*/public void sendMapTopic(String queueName, Map<String, Object> headers) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), headers);}/*** 发送对象消息主题** @param topicName 主题名称* @param obj       对象*/public void sendObjTopic(String topicName, Serializable obj) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topicName), obj);}/*** 发送对象集合消息主题** @param topicName 主题名称* @param objList   对象集合*/public void sendObjListTopic(String topicName, List<Serializable> objList) {this.jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topicName), objList);}}

5. MQ 点对点消费者

package com.gblfy.activemq.consumer;import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;import javax.jms.ObjectMessage;
import java.util.List;
import java.util.Map;/*** @author gblfy* @ClassNme QueueConsumer* @Description Mq点对点消费者* @Date 2019/9/3 18:05* @version1.0*/
@Component
public class QueueConsumer {@JmsListener(destination = "stringQueue", containerFactory = "jmsListenerContainerQueue")public void receiveStringQueue(String msg) {System.out.println("接收到消息...." + msg);}@JmsListener(destination = "stringListQueue", containerFactory = "jmsListenerContainerQueue")public void receiveStringListQueue(List<String> list) {System.out.println("接收到集合队列消息...." + list);}@JmsListener(destination = "mapQueue", containerFactory = "jmsListenerContainerQueue")public void receiveMapQueue(Map<String, Object> headers) {System.out.println("接收到集合队列消息...." + headers);}@JmsListener(destination = "objQueue", containerFactory = "jmsListenerContainerQueue")public void receiveObjQueue(ObjectMessage objectMessage) throws Exception {System.out.println("接收到对象队列消息...." + objectMessage.getObject());}@JmsListener(destination = "objListQueue", containerFactory = "jmsListenerContainerQueue")public void receiveObjListQueue(ObjectMessage objectMessage) throws Exception {System.out.println("接收到的对象队列消息..." + objectMessage.getObject());}
}

6. MQ 发布点阅消费者A

package com.gblfy.activemq.consumer;import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;import javax.jms.ObjectMessage;
import java.util.List;
/*** @author gblfy* @ClassNme ATopicConsumer* @Description Mq订阅者A* @Date 2019/9/3 18:05* @version1.0*/
@Component
public class ATopicConsumer {@JmsListener(destination = "stringTopic", containerFactory = "jmsListenerContainerTopic")public void receiveStringTopic(String msg) {System.out.println("ATopicConsumer接收到消息...." + msg);}@JmsListener(destination = "stringListTopic", containerFactory = "jmsListenerContainerTopic")public void receiveStringListTopic(List<String> list) {System.out.println("ATopicConsumer接收到集合主题消息...." + list);}@JmsListener(destination = "mapTopic", containerFactory = "jmsListenerContainerTopic")public void receiveMapTopic(List<String> list) {System.out.println("ATopicConsumer接收到Map主题消息...." + list);}@JmsListener(destination = "objTopic", containerFactory = "jmsListenerContainerTopic")public void receiveObjTopic(ObjectMessage objectMessage) throws Exception {System.out.println("ATopicConsumer接收到对象主题消息...." + objectMessage.getObject());}@JmsListener(destination = "objListTopic", containerFactory = "jmsListenerContainerTopic")public void receiveObjListTopic(ObjectMessage objectMessage) throws Exception {System.out.println("ATopicConsumer接收到的对象集合主题消息..." + objectMessage.getObject());}}

7. MQ 发布点阅消费者B

package com.gblfy.activemq.consumer;import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;import javax.jms.ObjectMessage;
import java.util.List;/*** @author gblfy* @ClassNme BTopicConsumer* @Description Mq订阅者B* @Date 2019/9/3 18:05* @version1.0*/
@Component
public class BTopicConsumer {@JmsListener(destination = "stringTopic", containerFactory = "jmsListenerContainerTopic")public void receiveStringTopic(String msg) {System.out.println("BTopicConsumer接收到消息...." + msg);}@JmsListener(destination = "stringListTopic", containerFactory = "jmsListenerContainerTopic")public void receiveStringListTopic(List<String> list) {System.out.println("BTopicConsumer接收到集合主题消息...." + list);}@JmsListener(destination = "objTopic", containerFactory = "jmsListenerContainerTopic")public void receiveObjTopic(ObjectMessage objectMessage) throws Exception {System.out.println("BTopicConsumer接收到对象主题消息...." + objectMessage.getObject());}@JmsListener(destination = "objListTopic", containerFactory = "jmsListenerContainerTopic")public void receiveObjListTopic(ObjectMessage objectMessage) throws Exception {System.out.println("BTopicConsumer接收到的对象集合主题消息..." + objectMessage.getObject());}}

8. 统一测试类

package com.gblfy.activemq.entity;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;/*** @author gblfy* @ClassNme User* @Description Mq发送接送对象模拟* @Date 2019/9/3 18:05* @version1.0*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {private String id;private String name;private Integer age;
}
package com.gblfy.activemq;import com.gblfy.activemq.producer.MqProducer;
import com.gblfy.activemq.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @author gblfy* @ClassNme ActivemqdemoApplicationTests* @Description Mq 测试公共类* @Date 2019/9/3 18:05* @version1.0*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivemqdemoApplicationTests {@Autowiredprivate MqProducer mqProducer;/**************************MQ点对点测试场景***************************//*** 点对点场景 01* 消息类型 String*/@Testpublic void testStringQueue() {for (int i = 1; i <= 100; i++) {System.out.println("第" + i + "次发送字符串队列消息");mqProducer.sendStringQueue("stringQueue", "消息:" + i);}}/*** 点对点场景 02* 消息类型 StringList*/@Testpublic void testStringListQueue() {List<String> idList = new ArrayList<>();idList.add("id1");idList.add("id2");idList.add("id3");System.out.println("正在发送集合队列消息ing......");mqProducer.sendStringListQueue("stringListQueue", idList);}/*** 点对点场景 03* 消息类型 Map<String,Object></String,Object>*/@Testpublic void testMapQueue() {Map<String, Object> map = new HashMap<>();map.put("1", "sxh");map.put("2", "ljy");map.put("3", "sh");map.put("4", "qjj");map.put("5", "ygf");map.put("6", "lxj");map.put("7", "gblfy");System.out.println("正在发送Map队列消息ing......");mqProducer.sendMapQueue("mapQueue", map);}/*** 点对点场景 04* 消息类型 Obj*/@Testpublic void testObjQueue() {System.out.println("正在发送对象队列消息......");mqProducer.sendObjQueue("objQueue", new User("1", "小明", 20));}/*** 点对点场景 05* 消息类型 ObjList*/@Testpublic void testObjListQueue() {System.out.println("正在发送对象集合队列消息......");List<Serializable> userList = new ArrayList<>();userList.add(new User("1", "雨昕", 01));userList.add(new User("2", "刘英", 26));userList.add(new User("3", "振振", 12));mqProducer.sendObjListQueue("objListQueue", userList);}/**************************MQ发布订阅测试场景***************************//*** 发布订阅场景 01* 消息类型 String*/@Testpublic void testStringTopic() {for (int i = 1; i <= 100; i++) {System.out.println("第" + i + "次发送字符串主题消息");mqProducer.sendStringTopic("stringTopic", "消息:" + i);}}/*** 发布订阅场景 02* 消息类型 StringList*/@Testpublic void testStringListTopic() {List<String> idList = new ArrayList<>();idList.add("id1");idList.add("id2");idList.add("id3");System.out.println("正在发送集合主题消息ing......");mqProducer.sendStringListTopic("stringListTopic", idList);}/*** 发布订阅场景 03* 消息类型 Map*/@Testpublic void testMapTopic() {Map<String, Object> map = new HashMap<>();map.put("1", "sxh");map.put("2", "ljy");map.put("3", "sh");map.put("4", "qjj");map.put("5", "ygf");map.put("6", "lxj");map.put("7", "gblfy");System.out.println("正在发送Map队列消息ing......");mqProducer.sendMapTopic("mapQueue", map);}/*** 发布订阅场景 04* 消息类型 Obj*/@Testpublic void testObjTopic() {System.out.println("正在发送对象主题消息......");mqProducer.sendObjTopic("objTopic", new User("1", "小明", 20));}/*** 发布订阅场景 05* 消息类型 ObjList*/@Testpublic void testObjListTopic() {System.out.println("正在发送对象集合主题消息......");List<Serializable> userList = new ArrayList<>();userList.add(new User("1", "雨昕", 01));userList.add(new User("2", "刘英", 26));userList.add(new User("3", "振振", 12));mqProducer.sendObjListTopic("objListTopic", userList);}
}

GitLab地址:https://gitlab.com/gb-heima/springboot-activemq
Git下载方式:

git clone git@gitlab.com:gb-heima/springboot-activemq.git

zip包下载方式:
https://gitlab.com/gb-heima/springboot-activemq/-/archive/master/springboot-activemq-master.zip

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/523355.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

怎样看Linux字体所在目录,Linux下列出所有字体的目录

fc-list|grep 黑/usr/share/fonts/adobe-source-han-sans/SourceHanSans-Bold.ttc: Source Han Sans HW TC,思源黑體 HW,思源黑體 HW Bold,Source Han Sans HW TC Bold:styleBold,Regular/usr/share/fonts/adobe-source-han-sans/SourceHanSans-Bold.ttc: 思源黑体,Source Han …

Docker精华问答 |为什么巨头会支持Docker?

如今Docker的使用已经非常普遍&#xff0c;特别在一线互联网公司。使用Docker技术可以帮助企业快速水平扩展服务&#xff0c;从而到达弹性部署业务的能力。在云服务概念兴起之后&#xff0c;Docker的使用场景和范围进一步发展。今天&#xff0c;就让我们来看看关于Docker的深度…

Unable to process Jar entry [module-info.class] from Jar

Unable to process Jar entry [module-info.class] from Jar [jar:file:/J:/rep/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar!/] for annotations org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19 解决方案&…

linux的批处理进程,巧用linux-top的批处理模式

IT技术学习&#xff1a;一种是采用循序渐进的系统式学习&#xff1b;一种是采用“投机取巧”的碎片式学习。我这个人比较赖&#xff0c;也没有那些大牛执着的精神和水平&#xff0c;所以只能和大家分享后者命令简述top命令-是UNIN&&LINUX系统自带的系统管理工具。可以实…

最新突破!科学家研发出世界首款精神控制手臂机器人;近日Kubernetes 1.15 正式发布;雷军公布小米手机产品线布局……...

关注并标星星CSDN云计算极客头条&#xff1a;速递、最新、绝对有料。这里有企业新动、这里有业界要闻&#xff0c;打起十二分精神&#xff0c;紧跟fashion你可以的&#xff01;每周三次&#xff0c;打卡即read更快、更全了解泛云圈精彩newsgo go go 腾讯微信电子健康卡整体解决…

java.lang.NoClassDefFoundError: org/springframework/core/ErrorCoded

这个错误主要是因为spring的相关jar的版本号不一致导致的&#xff0c;所以用maven管理会好很多

linux账户初始化文件,Linux启动初始化配置文件浅析

1)/etc/profile 登录时&#xff0c;会执行。全局(公有)配置&#xff0c;不管是哪个用户&#xff0c;登录时都会读取该文件。(2)/ect/bashrc Ubuntu没有此文件&#xff0c;与之对应的是/ect/bash.bashrcbash.bashrc 是交互式shell的初始化文件。(3)~/.profile 某个用户读取…

一文读懂云计算和PAYG“现付现用”模型

戳蓝字“CSDN云计算”关注我们哦&#xff01;译者 | 风车云马或许您对云计算听的比较多&#xff0c;但是对于“现付现用”(Pay-As-You-Go,PAYG)模型是什么还不十分了解。简单地说&#xff0c;PAYG是一种“实用”计算模型&#xff0c;它允许用户根据使用的机器小时数或消耗的资源…

JDBC出现这个错误的解决办法Connections could not be acquired from the underlying database!

以上问题可能有一下原因造成&#xff1a; 1、驱动配置有误 2、据库连接地址有误 3、H密码或帐号有误 4、数据库未启动或无权访问 5、项目未引入对应的驱动jar包 6、mysql root没有远程访问的权限&#xff0c;需要增加权限 7、grant all privileges on . to ‘root’’%’ iden…

linux用户权限简介,Linux用户及权限管理

【文件管理、管道、用户及组管理、用户及权限管理】\用户及组管理用户与组管理Linux系统是一个多用户多任务的分时操作系统&#xff0c;任何一个要使用系统资源的用户&#xff0c;都必须首先向系统管理员申请一个账号&#xff0c;然后以这个账号的身份进入系统。用户的账号一方…

Hadoop精华问答 | hadoop能干什么?

Hadoop能够进行大批量数据的离线处理,但是在实时计算上的表现实在是不尽如人意;而Storm就可以担当这部分的角色&#xff0c;今天&#xff0c;就让我们看看关于Storm的精华问答吧。1Q&#xff1a;hadoop是什么A&#xff1a;Hadoop被公认是一套行业大数据标准开源软件&#xff0c…

实战01_SSM整合ActiveMQ支持多种类型消息

ActiveMQ能干什么&#xff1f; 主要功能涉及JMS Provider用来帮助实现高可用、高性能、高伸缩、易用的企业级面向消息服务的系统。 ActiveMQ支持消息类型如下&#xff1a; 1、StreamMessage java原始值数据流 2、MapMessage 键值对 3、TextMessage 字符串 4、ObjectMessage 一…

linux xguest用户,在/etc/passwd中得到普通用户列表

/etc/passwd文件用来保存系统中当前所有的用户信息&#xff0c;该文件对所有用户都可见。在该文件中&#xff0c;每行信息代表一个用户。每个用户的信息由7部分组成&#xff1a;用户名&#xff1a;加密后的用户密码&#xff1a;用户ID(UID)&#xff1a;用户所在组ID(GID)&#…

实战02_SSM整合ActiveMQ支持多种类型消息

接上一篇&#xff1a;企业实战01_SSM整合ActiveMQ支持多种类型消息https://blog.csdn.net/weixin_40816738/article/details/100557400 ActiveMQ支持消息类型如下&#xff1a; 1、StreamMessage java原始值数据流 2、MapMessage 键值对 3、TextMessage 字符串 4、ObjectMessag…

华为首超苹果,iPhone 风光已不再?百度资讯搜索来源调整;自动驾驶激光雷达厂商Velodyne筹备上市;...

关注并标星星CSDN云计算极客头条&#xff1a;速递、最新、绝对有料。这里有企业新动、这里有业界要闻&#xff0c;打起十二分精神&#xff0c;紧跟fashion你可以的&#xff01;每周三次&#xff0c;打卡即read更快、更全了解泛云圈精彩newsgo go go 三星Galaxy A90更多细节曝光…

实战03_SSM整合ActiveMQ支持多种类型消息

接上一篇&#xff1a;企业实战02_SSM整合ActiveMQ支持多种类型消息https://blog.csdn.net/weixin_40816738/article/details/100572053 1、StreamMessage java原始值数据流 2、MapMessage 键值对 3、TextMessage 字符串 4、ObjectMessage 一个序列化的java对象 5、BytesMessage…

千字谏言!Python入门:这两点绝对不能偷懒!否则工作后必后悔

作为程序员&#xff0c;你有没有遇到过这样的领导&#xff1a;“别人还没走&#xff0c;你先走了不合适吧。”“不能确定功劳&#xff0c;总得有苦劳吧&#xff01;你看别人9点谁走了&#xff1f;”说到996&#xff0c;这很有可能是诱因之一。所以很多程序员会在学习了Java、C的…

linux上点时间延时,Linux上时间和定时器

Linux下时间和定时器http://blog.chinaunix.net/u1/35065/showart_1870601.html重点读了第三种方法。文章写得很好&#xff0c;加了一点点注释可参考http://linux.die.net/man/3/timer_settimehttp://linux.die.net/man/2/setitimerhttp://opengroup.org/onlinepubs/007908799/…

04_SSM整合ActiveMQ支持多种类型消息

接上一篇&#xff1a;企业实战03_SSM整合ActiveMQ支持多种类型消息https://blog.csdn.net/weixin_40816738/article/details/100572104 1、StreamMessage java原始值数据流 2、MapMessage 键值对 3、TextMessage 字符串 4、ObjectMessage 一个序列化的java对象 5、BytesMessage…

​听说,私有云也出新一代了?

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者 | 孙浩峰自从私有云的概念出现以来&#xff0c;由于其能够较好的解决传统IT 架构、IT 流程的诸如缺乏弹性、IT 架构难以支撑多变的业务环境&#xff0c;交付流程效率低难以及时响应业务需求等诸多问题&#xff0c;获得了蓬勃发…