六、Spring/Spring Boot整合ActiveMQ
Spring/Spring Boot整合ActiveMQ
- 一、Spring整合ActiveMQ
- 1.pom.xml
- 2.Queue - 队列
- 2.1 applicationContext.xml
- 2.2 生产者
- 2.3 消费者
- 3.Topic - 主题
- 3.1 applicationContext.xml
- 3.2 生产者
- 3.3 消费者
- 4.消费者 - 监听器
- 4.1 编写监听器类
- 4.2 配置监听器
- 4.3 生产者+消费者一体
- 二、Spring Boot整合ActiveMQ
- 1.Queue - 队列
- 1.1 生产者
- 1.1.1 创建Maven工程
- 1.1.2 pom.xml
- 1.1.3 application.yml
- 1.1.4 创建配置文件Bean
- 1.1.5 生产者
- 1.1.6 主启动类
- 1.1.7 测试单元类
- 1.1.8 案例-定时投递
- 1.2 消费者
- 1.2.1 创建Maven工程
- 1.2.2 pom.xml
- 1.2.3 application.yml
- 1.2.4 消费者-监听类
- 1.2.5 主启动类
- 2.Topic - 主题
- 2.1 生产者
- 2.1.1 创建Maven工程
- 2.1.2 pom.xml
- 2.1.3 application.yml
- 2.1.4 创建配置文件Bean
- 2.1.5 生产者
- 2.1.6 主启动类
- 2.2 消费者
- 2.2.1 创建Maven工程
- 2.2.2 pom.xml
- 2.2.3 application.yml
- 2.2.4 消费者
- 2.2.5 主启动类
一、Spring整合ActiveMQ
1.pom.xml
<?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.qingsi.activemq</groupId><artifactId>activemq_test</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><!-- activemq所需要的jar包配置 --><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.15.11</version></dependency><!-- 池化技术 --><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId><version>5.15.10</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.10.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring --><dependency><groupId>org.apache.xbean</groupId><artifactId>xbean-spring</artifactId><version>4.15</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.2.1.RELEASE</version></dependency><!-- 下面是junit/logback等基础配置 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.18</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies></project>
2.Queue - 队列
2.1 applicationContext.xml
- 路径:src/main/resources/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://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-package="com.qingsi.activemq"/><!-- 配置生产者 --><bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"><property name="connectionFactory"><!-- 正真可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --><bean class="org.apache.activemq.spring.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.86.128:61616"/></bean></property><property name="maxConnections" value="100"/></bean><!-- 这个是队列目的地,点对点的Queue --><bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"><!-- 通过构造注入Queue名 --><constructor-arg index="0" value="spring-active-queue"/></bean><!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 传入连接工厂 --><property name="connectionFactory" ref="connectionFactory"/><!-- 传入目的地 --><property name="defaultDestination" ref="destinationQueue"/><!-- 消息自动转换器 --><property name="messageConverter"><bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/></property></bean>
</beans>
2.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 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://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-package="com.qingsi.activemq"/><!-- 配置生产者 --><bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"><property name="connectionFactory"><!-- 正真可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --><bean class="org.apache.activemq.spring.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.86.128:61616"/></bean></property><property name="maxConnections" value="100"/></bean><!-- 这个是队列目的地, 发布订阅的主题Topic--><bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg index="0" value="spring-active-topic"/></bean><!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 传入连接工厂 --><property name="connectionFactory" ref="connectionFactory"/><!-- 传入目的地 --><property name="defaultDestination" ref="destinationTopic"/><!-- 消息自动转换器 --><property name="messageConverter"><bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/></property></bean>
</beans>
3.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 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://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-package="com.qingsi.activemq"/><!-- 配置生产者 --><bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"><property name="connectionFactory"><!-- 正真可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --><bean class="org.apache.activemq.spring.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.86.128:61616"/></bean></property><property name="maxConnections" value="100"/></bean><!-- 这个是队列目的地, 发布订阅的主题Topic--><bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg index="0" value="spring-active-topic"/></bean><!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 传入连接工厂 --><property name="connectionFactory" ref="connectionFactory"/><!-- 传入目的地 --><property name="defaultDestination" ref="destinationTopic"/><!-- 消息自动转换器 --><property name="messageConverter"><bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/></property></bean><!-- 配置Jms消息监听器 --><bean id="defaultMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><!-- Jms连接的工厂 --><property name="connectionFactory" ref="connectionFactory"/><!-- 设置默认的监听目的地 --><property name="destination" ref="destinationTopic"/><!-- 指定自己实现了MessageListener的类 --><property name="messageListener" ref="myMessageListener"/></bean>
</beans>
4.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 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.qingsi.boot.activemq</groupId><artifactId>boot_activemq_test</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.2.1.RELEASE</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId><version>2.2.1.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.2.1.RELEASE</version></plugin></plugins></build></project>
1.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-queue
1.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 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.qingsi.boot.activemq</groupId><artifactId>boot_activemq_consumer</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.2.1.RELEASE</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId><version>2.2.1.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.2.1.RELEASE</version></plugin></plugins></build>
</project>
1.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 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.qingsi.boot.activemq.topic.produce</groupId><artifactId>boot_mq_topic_produce</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.2.1.RELEASE</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId><version>2.2.1.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.2.1.RELEASE</version></plugin></plugins></build></project>
2.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-topic
2.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 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.qingsi.boot.activemq.topic.consumer</groupId><artifactId>boot_mq_topic_consumer</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.2.1.RELEASE</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId><version>2.2.1.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.2.1.RELEASE</version></plugin></plugins></build></project>
2.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-topic
2.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);}}
相关文章:

六、Spring/Spring Boot整合ActiveMQ
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 生产者消费者一体 二、…...
树莓派4B(Raspberry Pi 4B)使用docker搭建springBoot/springCloud服务
树莓派4B(Raspberry Pi 4B)使用docker搭建springBoot/springCloud服务 前提:本文基于Ubuntu,Java8,SpringBoot 2.6.13讲解 准备工作 准备SpringBoot/SpringCloud项目jar包 用 maven 打包springBoot/springCloud项目&…...

数据库设计、JDBC、数据库连接池
数据库设计 数据库设计概念 数据库设计就是根据业务 系统的具体需求,结合我们所选用的DBMS,为这个业务系统构造出最优的数据存储模型。建立数据库中的表结构以及表与表之间的关联关系的过程。有哪些表?表里有哪些字段?表和表之间有什么关系? 数据库设计的步骤…...

SpringBoot实现OneDrive文件上传
SpringBoot实现OneDrive文件上传 源码 OneDriveUpload: SpringBoot实现OneDrive文件上传 获取accessToken步骤 参考文档:针对 OneDrive API 的 Microsoft 帐户授权 - OneDrive dev center | Microsoft Learn 1.访问Azure创建应用Microsoft Azure,使…...

C++初阶:容器适配器介绍、stack和queue常用接口详解及模拟实现
介绍完了list类的相关内容后:C初阶:适合新手的手撕list(模拟实现list) 接下来进入新的篇章,stack和queue的介绍以及模拟: 文章目录 1.stack的初步介绍2.stack的使用3.queue的初步介绍4.queue的使用5.容器适…...
GRUB and the Boot Process on UEFI-based x86 Systems
background info : BIOS and UEFI-CSDN博客 The UEFI-based platform reads the partition table on the system storage and mounts the EFI System Partition (ESP), a VFAT partition labeled with a particular globally unique identifier (GUID). The ESP contains EFI a…...

2.C语言——输入输出
1.字符输入输出函数 1.输入:getchar() 字面意思,接收单个字符,使用方法 char a; a getchar();实际上效果等同于char a; scanf("%c",&a);2.输出:putchar() 2.格式化输入输出函数 1.输入:scanf() 格式: scanf(“格式控制…...

MySQL篇之SQL优化
一、表的设计优化 表的设计优化(参考阿里开发手册《嵩山版》): 1. 比如设置合适的数值(tinyint int bigint),要根据实际情况选择。 2. 比如设置合适的字符串类型(char和varchar)…...

QGis —— 1、Windows10下载安装QGis及插件
QGis官网 QGIS(自由开源的地理信息系统)是一个专业的GIS应用程序,它建立在免费和开源软件(FOSS)之上,并为此而自豪。QGIS 是一个方便使用的开源地理信息系统 (GIS),根据 GNU 通用公共许可授权。…...

【打工日常】使用docker部署Dashdot工具箱
一、Dashdot介绍 dashdot是一个简洁清晰的服务器数据仪表板,基于React实现 ,主要是显示操作系统、进程、存储、内存、网络这五个的数据。 二、本次实践介绍 1. 本次实践简介 本次实践部署环境为个人测试环境 2. 本地环境规划 本次实践环境规划…...

使用client-only 解决组件不兼容SSR问题
目录 前言 一、解决方案 1.基于Nuxt 框架的SSR应用 2.基于vue2框架的应用 3.基于vue3框架的应用 二、总结 往期回顾 前言 最近在我的单页面SSR应用上开发JSON编辑器功能,在引入组件后直接客户端跳转OK,但是在直接加载服务端渲染的时候一直报这…...

基于Java SSM框架实现网上报名系统项目【项目源码+论文说明】
基于java的SSM框架实现网上报名系统演示 摘要 随着互联网时代的到来,同时计算机网络技术高速发展,网络管理运用也变得越来越广泛。因此,建立一个B/S结构的网上报名系统,会使网上报名系统工作系统化、规范化,也会提高网…...

7.1 Qt 中输入行与按钮
目录 前言: 技能: 内容: 参考: 前言: line edit 与pushbotton的一点联动 当输入行有内容时,按钮才能使用,并能读出输入行的内容 技能: pushButton->setEnabled(false) 按钮不…...

云计算基础-网络虚拟化
虚拟交换机 什么是虚拟交换机 虚拟交换机是一种运行在虚拟化环境中的网络设备,其运行在宿主机的内存中,通过软件方式在宿主机内部实现了部分物理交换机的功能,如 VLAN 划分、流量控制、QoS 支持和安全功能等网络管理特性 虚拟交换机在云平…...

166基于matlab的通过峭度指标与互相关系数筛选IMF进行SVD分解去噪
基于matlab的通过峭度指标与互相关系数筛选IMF进行SVD分解去噪,分辨虚假imf,提取最大峭度imf图。输出去噪前后时域及其包络谱结果。程序已调通,可直接运行。 166 matlab SVD去噪 IMF筛选 包络谱 (xiaohongshu.com)...

第六十三天 服务攻防-框架安全CVE复现DjangoFlaskNode.JSJQuery
第六十三天 服务攻防-框架安全&CVE复现&Django&Flask&Node.JS&JQuery 知识点: 中间件及框架列表: IIS,Apache,Nginx,Tomcat,Docker,K8s,Weblogic.JBoos,WebSphere, Jenkins,GlassFish,Jetty,Jira,Struts2,Laravel,Solr,Shiro,Thin…...

最大子序和+旅行问题——单调队列
一、最大子序和 输入一个长度为 n 的整数序列,从中找出一段长度不超过 m 的连续子序列,使得子序列中所有数的和最大。 注意: 子序列的长度至少是 1。 输入 第一行输入两个整数 n,m (1 ≤ n,m ≤ 300000)。 第二行输入 n 个数,代…...

Unity设备分级策略
Unity设备分级策略 前言 之前自己做的设备分级策略,在此做一个简单的记录和思路分享。希望能给大家带来帮助。 分级策略 根据拟定的评分标准,预生成部分已知机型的分级信息,且保存在包内;如果设备没有被评级过,则优…...
自己在开发AI应用的过程总结的 Prompt - 持续更新
自己在开发AI应用的过程总结的 Prompt - 持续更新 0. 引言1. 让模型以"中文"进行回复2. 控制模型仅输出"hi"3. 让模型"提供简单、清晰而具体的回答"4. 让模型"在最后说谢谢" 0. 引言 我想,我们多半有着相似的经历…...

STM32——OLED菜单
文章目录 一.补充二. 二级菜单代码 简介:首先在我的51 I2C里面有OLED详细讲解,本期代码从51OLED基础上移植过来的,可以先看完那篇文章,在看这个,然后按键我是用的定时器扫描不会堵塞程序,可以翻开我的文章有单独的定时…...

UE5 学习系列(二)用户操作界面及介绍
这篇博客是 UE5 学习系列博客的第二篇,在第一篇的基础上展开这篇内容。博客参考的 B 站视频资料和第一篇的链接如下: 【Note】:如果你已经完成安装等操作,可以只执行第一篇博客中 2. 新建一个空白游戏项目 章节操作,重…...
uniapp 对接腾讯云IM群组成员管理(增删改查)
UniApp 实战:腾讯云IM群组成员管理(增删改查) 一、前言 在社交类App开发中,群组成员管理是核心功能之一。本文将基于UniApp框架,结合腾讯云IM SDK,详细讲解如何实现群组成员的增删改查全流程。 权限校验…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)
HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...
pam_env.so模块配置解析
在PAM(Pluggable Authentication Modules)配置中, /etc/pam.d/su 文件相关配置含义如下: 配置解析 auth required pam_env.so1. 字段分解 字段值说明模块类型auth认证类模块,负责验证用户身份&am…...

高等数学(下)题型笔记(八)空间解析几何与向量代数
目录 0 前言 1 向量的点乘 1.1 基本公式 1.2 例题 2 向量的叉乘 2.1 基础知识 2.2 例题 3 空间平面方程 3.1 基础知识 3.2 例题 4 空间直线方程 4.1 基础知识 4.2 例题 5 旋转曲面及其方程 5.1 基础知识 5.2 例题 6 空间曲面的法线与切平面 6.1 基础知识 6.2…...
sqlserver 根据指定字符 解析拼接字符串
DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...
解决本地部署 SmolVLM2 大语言模型运行 flash-attn 报错
出现的问题 安装 flash-attn 会一直卡在 build 那一步或者运行报错 解决办法 是因为你安装的 flash-attn 版本没有对应上,所以报错,到 https://github.com/Dao-AILab/flash-attention/releases 下载对应版本,cu、torch、cp 的版本一定要对…...

selenium学习实战【Python爬虫】
selenium学习实战【Python爬虫】 文章目录 selenium学习实战【Python爬虫】一、声明二、学习目标三、安装依赖3.1 安装selenium库3.2 安装浏览器驱动3.2.1 查看Edge版本3.2.2 驱动安装 四、代码讲解4.1 配置浏览器4.2 加载更多4.3 寻找内容4.4 完整代码 五、报告文件爬取5.1 提…...

Ubuntu Cursor升级成v1.0
0. 当前版本低 使用当前 Cursor v0.50时 GitHub Copilot Chat 打不开,快捷键也不好用,当看到 Cursor 升级后,还是蛮高兴的 1. 下载 Cursor 下载地址:https://www.cursor.com/cn/downloads 点击下载 Linux (x64) ,…...

Ubuntu系统多网卡多相机IP设置方法
目录 1、硬件情况 2、如何设置网卡和相机IP 2.1 万兆网卡连接交换机,交换机再连相机 2.1.1 网卡设置 2.1.2 相机设置 2.3 万兆网卡直连相机 1、硬件情况 2个网卡n个相机 电脑系统信息,系统版本:Ubuntu22.04.5 LTS;内核版本…...