六、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"><!--      正真可以生产ConnectionConnectionFactory,由对应的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);}}

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

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

相关文章

使用dssm实现简易 搜索推荐 功能

毕设想要实现一个简易的电影搜索推荐功能&#xff0c;由于对AI算法没有过多深入研究&#xff0c;结合ChatGPT实现了一个简易的模型 模型相关实现 import pymysql import pandas as pd import numpy as np from sklearn.feature_extraction.text import TfidfVectorizer from …

安装luajit及使用python运行lua脚本

使用Python运行lua脚本前&#xff0c;需要先安装LuaJIT&#xff0c;LuaJIT的官网是下载 (luajit.org) 目前已不再使用.exe文件的下载方式&#xff0c;需要使用Git从公共仓库下载源码&#xff0c;git命令为&#xff1a; $ git clone https://luajit.org/git/luajit.git 下载后…

Mybatis速成(二)

文章目录 1. Mybatis基础操作1.1 需求1.2 准备1.3 删除1.3.1 功能实现1.3.2 日志输入1.3.3 预编译SQL1.3.3.1 介绍1.3.3.2 SQL注入1.3.3.3 参数占位符 1.4 新增1.4.1 基本新增1.4.2 主键返回 1.5 更新1.6 查询1.6.1 根据ID查询1.6.2 数据封装1.6.3 条件查询1.6.4 参数名说明 2.…

PLC-Recorder的延伸分析功能说明

目录 一、缘起 二、如何从PLC-Recorder获取数据 1、在线获取 2、全自主打开数据文件 3、延伸分析 三、设置方法 四、效果展示 一、缘起 在各个行业&#xff0c;在不同的场景中&#xff0c;朋友们拿到数据后&#xff0c;想做的事情五花八门&#xff0c;有做宏观分析的、…

浅谈Websocket

由于 http 存在⼀个明显的弊端(消息只能有客户端推送到服务器端,⽽服务器端不能主动推送到客户端),导致如果服务器如果有连续的变化,这时只能使⽤轮询,⽽轮询效率过低,并不适合。于是 WebSocket 被发明出来 WebSocket 是⼀种在 Web 应⽤程序中实现双向通信的协议。与传…

排序前言冒泡排序

目录 排序应用 常见的排序算法 BubbleSort冒泡排序 整体思路 图解分析 ​ 代码实现 每趟 写法1 写法2 代码NO1 代码NO2优化 时间复杂度 排序概念 排序&#xff1a;所谓排序&#xff0c;就是使一串记录&#xff0c;按照其中的某个或某些关键字的大小&#xff0c;递…

人类智能运转所需的知识通常是非线性弥散、聚合的

人类智能所需的知识通常不是按照线性的、逐步增长的方式获取的&#xff0c;而是通过多个不同领域的知识在脑中进行非线性的交互和整合。人类智能的形成是一个复杂的过程&#xff0c;涉及到多个不同的认知能力和知识领域。人们从各种不同的来源获取知识&#xff0c;包括通过学习…

rtt设备io框架面向对象学习-软件模拟rtc设备

目录 1.软件rtc设备实现类2.软件rtc设备类的子类3.初始化/构造流程3.1 设备驱动框架层3.3 设备io管理层 4.总结5.使用 硬件rtc和软件rtc设备是互斥的。因为它们的名字都叫"rtc"&#xff0c;在对象容器中不允许重名。 软件rtc设备比较特殊&#xff0c;不依赖于任何硬件…

Anaconda修改虚拟环境的路径

新版本的anaconda会默认将虚拟环境配置在C盘下&#xff0c;默认的路径是C:\Users\username。同时anaconda3下envs目录是空的。 这里是建立虚拟环境是将路径修改到anaconda的方法。 第一步——修改.condarc文件 首先&#xff0c;C:\Users\username找到.condarc文件 添加或修…

洛谷P5714 肥胖问题 题解

#题外话&#xff08;第25篇题解&#xff09; #先看题目 题目链接https://www.luogu.com.cn/problem/P5714 特别的&#xff0c;对于Microsoft Edge浏览器&#xff0c;其自带有截长图引擎&#xff0c;路径为三个点→截图→选定后下拉即可&#xff0c;浏览器会自动为你下拉并选中…

Code Composer Studio (CCS) - Comment (注释)

Code Composer Studio [CCS] - Comment [注释] References Add Block Comment: 选中几行代码 -> 鼠标右键 -> Source -> Add Block Comment shortcut key: Ctrl Shift / Remove Block Comment: 选中几行代码->鼠标右键->Source->Remove Block Comment s…

Unity类银河恶魔城学习记录7-6 P72 Bouncy sword源代码

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释&#xff0c;可供学习Alex教程的人参考 此代码仅为较上一P有所改变的代码【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili Sword_Skill_Controller.cs using System.Collections; using System.Colle…

相机图像质量研究(32)常见问题总结:图像处理对成像的影响--振铃效应

系列文章目录 相机图像质量研究(1)Camera成像流程介绍 相机图像质量研究(2)ISP专用平台调优介绍 相机图像质量研究(3)图像质量测试介绍 相机图像质量研究(4)常见问题总结&#xff1a;光学结构对成像的影响--焦距 相机图像质量研究(5)常见问题总结&#xff1a;光学结构对成…

CSS概述 | CSS的引入方式 | 选择器

文章目录 1.CSS概述2.CSS的引入方式2.1.内部样式表2.2.行内样式表2.3.外部样式表 3.选择器 1.CSS概述 CSS&#xff0c;全称Cascading Style Sheets&#xff08;层叠样式表&#xff09;&#xff0c;是一种用来设置HTML&#xff08;或XML等&#xff09;文档样式的语言。CSS的主要…

c# 二叉树

在 C# 中&#xff0c;二叉树是一种常见的数据结构&#xff0c;它由节点组成&#xff0c;每个节点最多有两个子节点&#xff1a;左子节点和右子节点。在 C# 中&#xff0c;可以使用类来实现二叉树的节点&#xff0c;并且通过引用连接节点来构建整棵树。 以下是一个简单的示例&a…

C++中的拷贝构造函数和深拷贝、浅拷贝,如何在C++中实现类和对象的序列化与反序列化?

C中的拷贝构造函数和深拷贝、浅拷贝 拷贝构造函数&#xff1a; 拷贝构造函数是一种特殊的构造函数&#xff0c;它接受一个对其同类的常量引用作为参数&#xff0c;并用于初始化新创建的对象。其主要作用是实现一个类的对象到另一个相同类型对象的拷贝。 拷贝构造函数的原型通常…

SG3225EAN规格书

SG3225EAN 晶体振荡器利用先进的锁相环技术和AT切割晶体单元&#xff0c;提供了宽频率范围和高性能LV-PECL输出&#xff0c;73.5 MHz至700 MHz的宽频率范围&#xff0c;能够保证高稳定性和宽频率调整的能力&#xff0c;适应于多样化的应用需求。2.5V和3.3V两种供电电压&#xf…

win10 环境下Python 3.8按装fastapi paddlepaddle 进行图片文字识别1

###按装 用conda 创建python 3.8的环境&#xff0c;可参看本人python下的其它文章。 在pycharm开发环境下按装相关的模块&#xff1a; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple fastapi pip install -i https://pypi.tuna.tsinghua.edu.cn/simple "uvi…

StarRocks表设计——分区分桶与副本数

目录 一、数据分布 1.1 概述 1.2 数据分布方式 1.2.1 Round-Robin 1.2.2 Range 1.2.3 List 1.2.4 Hash 1.3 StarRocks的数据分布方式 1.3.1 不分区 Hash分桶 1.3.2 Range分区Hash分桶 三、分区 3.1 分区概述 3.2 创建分区 3.2.1 手动创建分区 3.2.2 批量创建分区…

【教学类-19-10】20240214《ABAB式-规律黏贴18格-手工纸15*15CM-一页3种图案,AB一组样板,纵向、有边框》(中班)

背景需求 利用15*15CM手工纸制作AB色块手环&#xff08;手工纸自带色彩&#xff09;&#xff0c;一页3个图案&#xff0c;2条为一组&#xff0c;画图案&#xff0c;黏贴成一个手环。 素材准备 代码展示 # # 作者&#xff1a;阿夏 # 时间&#xff1a;2024年2月14日 # 名称&…