ActiveMQ学习笔记-07——SpringBoot整合ActiveMQ
参考:【尚硅谷ActiveMQ教程(MQ消息中间件快速入门)
1. 队列
-
队列生产者
-
新建maven工程boot_mq_produce并设置包名com.naclo.boot.activemq
-
编写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.naclo</groupId> <artifactId>boot_mq_produce</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
编写application.yml
server: port: 7777 spring: activemq: broker-url: tcp://127.0.0.1:61616 #MQ服务器地址 user: admin password: admin jms: pub-sub-domain: false #false=Queue true=Topic # 自己定义队列名称 myqueue: boot-activemq-queue
-
配置bean
@Component @EnableJms public class ConfigBean { @Value("${myqueue}") private String myQueue; @Bean public Queue queue(){ return new ActiveMQQueue(myQueue); } }
-
编写消息生产者Queue_Produce
@Component public class Queue_Produce { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; public void produceMsg() { jmsMessagingTemplate.convertAndSend(queue, "----" + UUID.randomUUID().toString().substring(0, 6)); } }
-
编写主启动类MainApp_Produce
@SpringBootApplication public class MainApp_Produce { public static void main(String[] args) { SpringApplication.run(MainApp_Produce.class, args); } }
-
编写测试类
@SpringBootTest(classes = MainApp_Produce.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestActiveMQ { @Autowired private Queue_Produce queue_produce; @Test public void testSend() throws Exception { queue_produce.produceMsg(); } }
-
新增需求,定时推送,三秒发一条
-
修改Queue_Produce新增定时投递方法
//间隔时间三秒钟定投 @Scheduled(fixedDelay = 3000) public void produceMsgScheduled() { jmsMessagingTemplate.convertAndSend(queue, "----Scheduled" + UUID.randomUUID().toString().substring(0, 6)); System.out.println("----produceMsgScheduled----"); }
-
修改主启动类MainApp_Produce添加注解
@EnableScheduling
-
-
-
队列消费者
-
新建maven工程boot_mq_consumer并设置包名com.naclo.boot.activemq
-
编写pom.xml
同上
-
编写application.yml
server: port: 8888 spring: activemq: broker-url: tcp://127.0.0.1:61616 #MQ服务器地址 user: admin password: admin jms: pub-sub-domain: false #false=Queue true=Topic # 自己定义队列名称 myqueue: boot-activemq-queue
-
编写消息消费者Queue_Consumer
@Component public class Queue_consumer { @JmsListener(destination = "${myqueue}") public void receive(TextMessage textMessage) throws Exception{ System.out.println("---消费者收到消息:"+textMessage.getText()); } }
-
编写主启动类MainApp_Produce
@SpringBootApplication public class MainApp_Consumer { public static void main(String[] args) { SpringApplication.run(MainApp_Consumer.class, args); } }
-
2. 发布订阅
-
订阅生产者
-
新建maven工程boot_mq_topic_producer并设置包名com.naclo.boot.activemq.topic
-
编写pom.xml
同上
-
编写application.yml
server: port: 6666 spring: activemq: broker-url: tcp://127.0.0.1:61616 #MQ服务器地址 user: admin password: admin jms: pub-sub-domain: true #false=Queue true=Topic # 自己定义队列名称 mytopic: boot-activemq-topic
-
配置bean
@Component public class ConfigBean { @Value("${mytopic}") private String topicName; @Bean public Topic topic() { return new ActiveMQTopic(topicName); } }
-
编写消息生产者Topic_Produce
@Component public class Topic_Produce { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Topic topic; @Scheduled(fixedDelay = 3000) public void produceTopic(){ jmsMessagingTemplate.convertAndSend(topic,"主题消息"+ UUID.randomUUID().toString().substring(0,6)); } }
-
编写主启动类MainApp_TopicProduce
@SpringBootApplication @EnableScheduling public class MainApp_TopicProduce { public static void main(String[] args) { SpringApplication.run(MainApp_TopicProduce.class,args); } }
-
-
订阅消费者
-
新建maven工程boot_mq_topic_consumer并设置包名com.naclo.boot.activemq.topic
-
编写pom.xml
同上
-
编写application.yml
server: port: 5555 spring: activemq: broker-url: tcp://127.0.0.1:61616 #MQ服务器地址 user: admin password: admin jms: pub-sub-domain: true #false=Queue true=Topic # 自己定义队列名称 mytopic: boot-activemq-topic
-
编写消息消费者Topic_Consumer
@Component public class Topic_Consumer { @JmsListener(destination = "${mytopic}") public void receive(TextMessage textMessage) throws Exception { System.out.println("消费者收到订阅的主题" + textMessage.getText()); } }
-
编写主启动类MainApp_TopicConsumer
@SpringBootApplication public class MainApp_TopicConsumer { public static void main(String[] args) { SpringApplication.run(MainApp_TopicConsumer.class, args); } }
-