网站开发及建设,有几家公司如何建设网站,玉林英文网站建设,上海缘魁网站建设Spring/Spring Boot整合ActiveMQ 一、Spring整合ActiveMQ1.pom.xml2.Queue - 队列2.1 applicationContext.xml2.2 生产者2.3 消费者 3.Topic - 主题3.1 applicationContext.xml3.2 生产者3.3 消费者 4.消费者 - 监听器4.1 编写监听器类4.2 配置监听器4.3 生产者消费者一体 二、… Spring/Spring Boot整合ActiveMQ 一、Spring整合ActiveMQ1.pom.xml2.Queue - 队列2.1 applicationContext.xml2.2 生产者2.3 消费者 3.Topic - 主题3.1 applicationContext.xml3.2 生产者3.3 消费者 4.消费者 - 监听器4.1 编写监听器类4.2 配置监听器4.3 生产者消费者一体 二、Spring Boot整合ActiveMQ1.Queue - 队列1.1 生产者1.1.1 创建Maven工程1.1.2 pom.xml1.1.3 application.yml1.1.4 创建配置文件Bean1.1.5 生产者1.1.6 主启动类1.1.7 测试单元类1.1.8 案例-定时投递 1.2 消费者1.2.1 创建Maven工程1.2.2 pom.xml1.2.3 application.yml1.2.4 消费者-监听类1.2.5 主启动类 2.Topic - 主题2.1 生产者2.1.1 创建Maven工程2.1.2 pom.xml2.1.3 application.yml2.1.4 创建配置文件Bean2.1.5 生产者2.1.6 主启动类 2.2 消费者2.2.1 创建Maven工程2.2.2 pom.xml2.2.3 application.yml2.2.4 消费者2.2.5 主启动类 一、Spring整合ActiveMQ
1.pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.qingsi.activemq/groupIdartifactIdactivemq_test/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependency!-- activemq所需要的jar包配置 --groupIdorg.apache.activemq/groupIdartifactIdactivemq-all/artifactIdversion5.15.11/version/dependency!-- 池化技术 --dependencygroupIdorg.apache.activemq/groupIdartifactIdactivemq-pool/artifactIdversion5.15.10/version/dependencydependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.10.1/version/dependency!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring --dependencygroupIdorg.apache.xbean/groupIdartifactIdxbean-spring/artifactIdversion4.15/version/dependency!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --dependencygroupIdorg.springframework/groupIdartifactIdspring-jms/artifactIdversion5.2.1.RELEASE/version/dependency!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion5.2.1.RELEASE/version/dependency!-- 下面是junit/logback等基础配置 --dependencygroupIdch.qos.logback/groupIdartifactIdlogback-classic/artifactIdversion1.2.3/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.16.18/versionscopeprovided/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependency/dependencies/project2.Queue - 队列
2.1 applicationContext.xml
路径src/main/resources/applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd!-- 开启包的自动扫描 --context:component-scan base-packagecom.qingsi.activemq/!-- 配置生产者 --bean idconnectionFactory classorg.apache.activemq.pool.PooledConnectionFactory destroy-methodstopproperty nameconnectionFactory!-- 正真可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --bean classorg.apache.activemq.spring.ActiveMQConnectionFactoryproperty namebrokerURL valuetcp://192.168.86.128:61616//bean/propertyproperty namemaxConnections value100//bean!-- 这个是队列目的地,点对点的Queue --bean iddestinationQueue classorg.apache.activemq.command.ActiveMQQueue!-- 通过构造注入Queue名 --constructor-arg index0 valuespring-active-queue//bean!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --bean idjmsTemplate classorg.springframework.jms.core.JmsTemplate!-- 传入连接工厂 --property nameconnectionFactory refconnectionFactory/!-- 传入目的地 --property namedefaultDestination refdestinationQueue/!-- 消息自动转换器 --property namemessageConverterbean classorg.springframework.jms.support.converter.SimpleMessageConverter//property/bean
/beans2.2 生产者
package com.qingsi.activemq.spring;import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;Service
public class SpringMQProduce {Autowiredprivate JmsTemplate jmsTemplate;public static void main(String[] args) {// 获取配置文件对象ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);// 获取SpringMQProduce对象用于调用jmsTemplate// 下面等同于 new SpringMQProduce()但是交给Spring管理他只会new一次后续都是用同一个实例对象SpringMQProduce produce ctx.getBean(SpringMQProduce.class);// 由于 目标队列 在配置文件指定了所以在发送的时候不需要再指定produce.jmsTemplate.send(new MessageCreator() {Overridepublic Message createMessage(Session session) throws JMSException {TextMessage textMessage session.createTextMessage(Spring和Active整合的消息);return textMessage;}});// 下面是lambda表达式写法和上面效果一样// produce.jmsTemplate.send((session - {// TextMessage textMessage session.createTextMessage(Spring和Active整合的消息);// return textMessage;// }));System.out.println(消息发送完毕);}}
2.3 消费者
package com.qingsi.activemq.spring;import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;Service
public class SpringMQConsumer {Autowiredprivate JmsTemplate jmsTemplate;public static void main(String[] args) {// 获取配置文件对象ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);// 获取SpringMQConsumer对象用于调用jmsTemplate// 下面等同于 new SpringMQConsumer()但是交给Spring管理他只会new一次后续都是用同一个实例对象SpringMQConsumer consumer ctx.getBean(SpringMQConsumer.class);String retValue (String) consumer.jmsTemplate.receiveAndConvert();System.out.println(spring消费者收到的消息 retValue);}}
3.Topic - 主题
3.1 applicationContext.xml
路径src/main/resources/applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd!-- 开启包的自动扫描 --context:component-scan base-packagecom.qingsi.activemq/!-- 配置生产者 --bean idconnectionFactory classorg.apache.activemq.pool.PooledConnectionFactory destroy-methodstopproperty nameconnectionFactory!-- 正真可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --bean classorg.apache.activemq.spring.ActiveMQConnectionFactoryproperty namebrokerURL valuetcp://192.168.86.128:61616//bean/propertyproperty namemaxConnections value100//bean!-- 这个是队列目的地, 发布订阅的主题Topic--bean iddestinationTopic classorg.apache.activemq.command.ActiveMQTopicconstructor-arg index0 valuespring-active-topic//bean!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --bean idjmsTemplate classorg.springframework.jms.core.JmsTemplate!-- 传入连接工厂 --property nameconnectionFactory refconnectionFactory/!-- 传入目的地 --property namedefaultDestination refdestinationTopic/!-- 消息自动转换器 --property namemessageConverterbean classorg.springframework.jms.support.converter.SimpleMessageConverter//property/bean
/beans3.2 生产者
和队列的代码一样只是在配置文件里面改了destination
3.3 消费者
和队列的代码一样只是在配置文件里面改了destination
4.消费者 - 监听器
实现目标在Spring里面实现消费者不启动直接通过配置监听完成类似于前面setMessageListenner实时间提供消息注意配置了监听器那么启动生产者就会自动消费不需要再启动消费者了
4.1 编写监听器类
package com.qingsi.activemq.spring;import org.springframework.stereotype.Component;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;Component
public class MyMessageListener implements MessageListener {Overridepublic void onMessage(Message message) {if (message instanceof TextMessage){TextMessage textMessage (TextMessage) message;try {System.out.println(收到消息 textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}}
}
4.2 配置监听器
applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd!-- 开启包的自动扫描 --context:component-scan base-packagecom.qingsi.activemq/!-- 配置生产者 --bean idconnectionFactory classorg.apache.activemq.pool.PooledConnectionFactory destroy-methodstopproperty nameconnectionFactory!-- 正真可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --bean classorg.apache.activemq.spring.ActiveMQConnectionFactoryproperty namebrokerURL valuetcp://192.168.86.128:61616//bean/propertyproperty namemaxConnections value100//bean!-- 这个是队列目的地, 发布订阅的主题Topic--bean iddestinationTopic classorg.apache.activemq.command.ActiveMQTopicconstructor-arg index0 valuespring-active-topic//bean!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --bean idjmsTemplate classorg.springframework.jms.core.JmsTemplate!-- 传入连接工厂 --property nameconnectionFactory refconnectionFactory/!-- 传入目的地 --property namedefaultDestination refdestinationTopic/!-- 消息自动转换器 --property namemessageConverterbean classorg.springframework.jms.support.converter.SimpleMessageConverter//property/bean!-- 配置Jms消息监听器 --bean iddefaultMessageListenerContainer classorg.springframework.jms.listener.DefaultMessageListenerContainer!-- Jms连接的工厂 --property nameconnectionFactory refconnectionFactory/!-- 设置默认的监听目的地 --property namedestination refdestinationTopic/!-- 指定自己实现了MessageListener的类 --property namemessageListener refmyMessageListener//bean
/beans4.3 生产者消费者一体
package com.qingsi.activemq.spring;import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;Service
public class SpringMQProduce {Autowiredprivate JmsTemplate jmsTemplate;public static void main(String[] args) {// 获取配置文件对象ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);// 获取SpringMQProduce对象用于调用jmsTemplate// 下面等同于 new SpringMQProduce()但是交给Spring管理他只会new一次后续都是用同一个实例对象SpringMQProduce produce ctx.getBean(SpringMQProduce.class);// 由于 目标队列 在配置文件指定了所以在发送的时候不需要再指定produce.jmsTemplate.send((session - {TextMessage textMessage session.createTextMessage(Spring和Active整合的消息);return textMessage;}));System.out.println(消息发送完毕);}}
二、Spring Boot整合ActiveMQ
1.Queue - 队列
1.1 生产者
1.1.1 创建Maven工程
这里有案例根据实际来创建https://qingsi.blog.csdn.net/article/details/136130132我创建了 工程名boot_activemq_test包名com.qingsi.boot.activemq
1.1.2 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.qingsi.boot.activemq/groupIdartifactIdboot_activemq_test/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion2.2.1.RELEASE/versionscopetest/scope/dependency!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-activemq/artifactIdversion2.2.1.RELEASE/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.2.1.RELEASE/version/plugin/plugins/build/project1.1.3 application.yml
路径src/main/resources/application.yml
#Springboot启动端口
server:port: 8080#ActiveMQ配置
spring:activemq:broker-url: tcp://192.168.86.128:61616 #ActiveMQ服务器IPuser: admin #ActiveMQ连接用户名password: admin #ActiveMQ连接密码jms:#指定连接队列还是主题pub-sub-domain: false # false Queue | true Topic#定义服务上的队列名
myqueue: springboot-activemq-queue1.1.4 创建配置文件Bean
package com.qingsi.boot.activemq.config;import javax.jms.Queue;import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;Component
EnableJms
public class ConfigBean {// 从配置文件读取 队列名称Value(${myqueue})private String myQueue;// 通过 队列名称 创建队列Beanpublic Queue queue() {return new ActiveMQQueue(myQueue);}
}1.1.5 生产者
package com.qingsi.boot.activemq.produce;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;import javax.jms.Queue;
import java.util.UUID;Component
public class QueueProduce {Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;Autowiredprivate Queue queue;public void produceMsg() {jmsMessagingTemplate.convertAndSend(queue, ----- UUID.randomUUID().toString().substring(0, 6));}
}
1.1.6 主启动类
package com.qingsi.boot.activemq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class MainAppProduce {public static void main(String[] args) {SpringApplication.run(MainAppProduce.class, args);}
}
1.1.7 测试单元类
package com.qingsi.boot.activemq;import com.qingsi.boot.activemq.produce.QueueProduce;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;import javax.annotation.Resource;SpringBootTest(classes MainAppProduce.class)
RunWith(SpringJUnit4ClassRunner.class)
WebAppConfiguration
public class TestActiveMQ {Resourceprivate QueueProduce queueProduce;Testpublic void testSend() {queueProduce.produceMsg();}
}运行测试类
1.1.8 案例-定时投递
要求每隔3秒钟,往MQ推送消息
生产者
package com.qingsi.boot.activemq.produce;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import javax.jms.Queue;
import java.util.UUID;Component
public class QueueProduce {Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;Autowiredprivate Queue queue;public void produceMsg() {jmsMessagingTemplate.convertAndSend(queue, ----- UUID.randomUUID().toString().substring(0, 6));}// 间隔实际3秒定投Scheduled(fixedDelay 3000)public void produceMsgScheduled(){jmsMessagingTemplate.convertAndSend(queue, ----- UUID.randomUUID().toString().substring(0, 6));}
}
主启动类
开启Schedule
package com.qingsi.boot.activemq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;SpringBootApplication
EnableScheduling
public class MainAppProduce {public static void main(String[] args) {SpringApplication.run(MainAppProduce.class, args);}
}
启动主启动类就会定时发消息了
1.2 消费者
1.2.1 创建Maven工程
这里有案例根据实际来创建https://qingsi.blog.csdn.net/article/details/136130132我创建了 工程名boot_activemq_consumer包名com.qingsi.boot.activemq
1.2.2 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.qingsi.boot.activemq/groupIdartifactIdboot_activemq_consumer/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion2.2.1.RELEASE/versionscopetest/scope/dependency!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-activemq/artifactIdversion2.2.1.RELEASE/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.2.1.RELEASE/version/plugin/plugins/build
/project1.2.3 application.yml
#Springboot启动端口
server:port: 8888#ActiveMQ配置
spring:activemq:broker-url: tcp://192.168.86.128:61616 #ActiveMQ服务器IPuser: admin #ActiveMQ连接用户名password: admin #ActiveMQ连接密码jms:#指定连接队列还是主题pub-sub-domain: false # false Queue | true Topic#定义服务上的队列名
myqueue: springboot-activemq-queue
1.2.4 消费者-监听类
package com.qingsi.activemq.consumer;import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;import javax.jms.JMSException;
import javax.jms.TextMessage;Component
public class QueueConsumer {// 开启监听JmsListener(destination ${myqueue})public void receive(TextMessage textMessage) throws JMSException {System.out.println(消费者收到消息 textMessage.getText());}
}1.2.5 主启动类
开启Jms监听
package com.qingsi.activemq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class MainAppConsumer {public static void main(String[] args) {SpringApplication.run(MainAppConsumer.class, args);}
}
2.Topic - 主题
2.1 生产者
2.1.1 创建Maven工程
这里有案例根据实际来创建https://qingsi.blog.csdn.net/article/details/136130132我创建了 工程名boot_mq_topic_produce包名com.qingsi.boot.activemq.topic.produce
2.1.2 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.qingsi.boot.activemq.topic.produce/groupIdartifactIdboot_mq_topic_produce/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion2.2.1.RELEASE/versionscopetest/scope/dependency!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-activemq/artifactIdversion2.2.1.RELEASE/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.2.1.RELEASE/version/plugin/plugins/build/project2.1.3 application.yml
#Springboot启动端口
server:port: 6666#ActiveMQ配置
spring:activemq:broker-url: tcp://192.168.86.128:61616 #ActiveMQ服务器IPuser: admin #ActiveMQ连接用户名password: admin #ActiveMQ连接密码jms:#指定连接队列还是主题pub-sub-domain: true # false Queue | true Topic#定义服务上的队列名
myTopic: springboot-activemq-topic2.1.4 创建配置文件Bean
package com.qingsi.boot.activemq.topic.config;import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;import javax.jms.Topic;Component
public class ConfigBean {Value(${myTopic})private String topicName;Beanpublic Topic topic(){return new ActiveMQTopic(topicName);}}
2.1.5 生产者
package com.qingsi.boot.activemq.topic.com.qingsi.boot.activemq.produce;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import javax.jms.Topic;
import java.util.UUID;Component
public class TopicProduce {Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;Autowiredprivate Topic topic;// 间隔实际3秒定投Scheduled(fixedDelay 3000)public void produceTopic() {jmsMessagingTemplate.convertAndSend(topic, 主题消息 UUID.randomUUID().toString().substring(0, 6));}}
2.1.6 主启动类
package com.qingsi.boot.activemq.topic;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;SpringBootApplication
EnableScheduling
public class MainAppTopicProduce {public static void main(String[] args) {SpringApplication.run(MainAppTopicProduce.class, args);}}2.2 消费者
2.2.1 创建Maven工程
这里有案例根据实际来创建https://qingsi.blog.csdn.net/article/details/136130132我创建了 工程名boot_mq_topic_consumer包名com.qingsi.boot.activemq.topic.consumer
2.2.2 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.qingsi.boot.activemq.topic.consumer/groupIdartifactIdboot_mq_topic_consumer/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion2.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion2.2.1.RELEASE/versionscopetest/scope/dependency!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-activemq/artifactIdversion2.2.1.RELEASE/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.2.1.RELEASE/version/plugin/plugins/build/project2.2.3 application.yml
#Springboot启动端口
server:port: 5555#ActiveMQ配置
spring:activemq:broker-url: tcp://192.168.86.128:61616 #ActiveMQ服务器IPuser: admin #ActiveMQ连接用户名password: admin #ActiveMQ连接密码jms:#指定连接队列还是主题pub-sub-domain: true # false Queue | true Topic#定义服务上的队列名
myTopic: springboot-activemq-topic2.2.4 消费者
package com.qingsi.activemq.topic.consumer;import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;import javax.jms.JMSException;
import javax.jms.TextMessage;Component
public class TopicConsumer {// 开启监听JmsListener(destination ${myTopic})public void receive(TextMessage textMessage) throws JMSException {System.out.println(消费者收到消息 textMessage.getText());}
}
2.2.5 主启动类
package com.qingsi.activemq.topic;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class MainAppTopicConsumer {public static void main(String[] args) {SpringApplication.run(MainAppTopicConsumer.class, args);}}