ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
前面有介绍与基础部分,有兴趣的可以移步:
初步认识了ActiveMQ:https://blog.csdn.net/qq_26975307/article/details/98875098,
结合JavaSE进行初尝试:https://blog.csdn.net/qq_26975307/article/details/98968854,
详细讲讲JMS:https://blog.csdn.net/qq_26975307/article/details/99408962,
JMS的可靠性:https://phubing.blog.csdn.net/article/details/99412285,
结合 Spring,基于配置文件的使用 ActiveMQ:https://phubing.blog.csdn.net/article/details/99413883
此篇开始结合 SpringBoot,基于 application.xml 使用ActiveMQ
1、步骤
1、新建maven工程并设置包名类名
2.pom.xml
3.application.yml
4.配置Bean
5.Queue_Produce
6.主启动类
7.测试单元
1.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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.phubing</groupId> <artifactId>bootdemotopicpro</artifactId> <version>0.0.1-SNAPSHOT</version> <name>bootdemotopicpro</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </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> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>2.1.5.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
1.2、application.yml
server: port: 8080 spring: activemq: #ActiveMQ服务器地址 broker-url: tcp://192.168.177.130:61616 user: admin password: admin jms: #false == Queue ; true == Topic ; 默认为false pub-sub-domain: false #自定义的队列名称 myQueue: boot-activemq-queue
1.3、消息生产者
package com.phubing.bootmqdemo.queue; 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; /** * @ClassName Queue_Produce * @Description TODO * @Author phubing * @Date 2019-08-12 22:39 * @Version 1.0 * * 相当于Service层 **/ @Component public class Queue_Produce { //可以当做Dao层 @Autowired private JmsMessagingTemplate jmsMessagingTemplate; //在ConfigBean中进行Spring管理后,可以直接注入 @Autowired private Queue queue; //业务逻辑方法 public void produceMsg(){ jmsMessagingTemplate.convertAndSend(queue, "boot整合ActiveMQ案例:" + UUID.randomUUID().toString().substring(0, 32)); System.out.println("Queue生产者消息发送完毕"); } }
1.4、ConfigBean
package com.phubing.bootmqdemo.config; 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; import javax.jms.Queue; /** * @ClassName ConfigBean * @Description TODO * @Author phubing * @Date 2019-08-12 22:35 * @Version 1.0 **/ @Component //开启JMS适配的注解 @EnableJms public class ConfigBean { //1、从配置文件中读取队列名称 @Value("${myQueue}") private String myQueue; //相当于 <bean id="" class=""/> @Bean public Queue queue(){ //也即按照yml的配置去访问ActiveMQ return new ActiveMQQueue(myQueue); } }
1.5、生产者测试类
package com.phubing.bootmqdemo.queue; import com.phubing.bootmqdemo.BootmqdemoApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; /** * @ClassName TestActiceMQQueue * @Description TODO * @Author phubing * @Date 2019-08-12 22:47 * @Version 1.0 **/ @SpringBootTest(classes = BootmqdemoApplication.class) @RunWith(SpringJUnit4ClassRunner.class) //以web的形式启动测试 @WebAppConfiguration public class TestActiceMQQueue { /** * @Autowired 与 @Resource 的异同? * */ @Resource private Queue_Produce queue_produce; @Test public void test() throws Exception{ queue_produce.produceMsg(); } }
1.6、新建一个boot工程
说明:消费者与生产者分开主要是为了测试方便,pom 与 application 文件直接粘贴即可
1.7、消息消费者
package com.phubing.queue; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import javax.jms.TextMessage; /** * @ClassName Queue_Consumer * @Description TODO * @Author phubing * @Date 2019-08-12 23:06 * @Version 1.0 * * * @Component 和 @Service 的区别 * **/ @Component public class Queue_Consumer { @JmsListener(destination = "${myQueue}") public void recive(TextMessage textMessage) throws Exception{ System.out.println("消费者收到队列消息:"+textMessage.getText()); } }
2、新需求:要求每隔3秒钟往 MQ 推送消息以下定时发送
2.1、在消息生产者类中增加(Queue_Produce)
//定时投递(主启动类需开启) @Scheduled(fixedDelay = 3000L) public void produceMsgScheduled(){ jmsMessagingTemplate.convertAndSend(queue, "produceMsgScheduled:" + UUID.randomUUID().toString().substring(0, 32)); System.out.println("Queue生产者定投消息发送完毕"); }
2.2、在消息生产者工程的启动类中增加
@EnableScheduling
2.3、直接启动两个工程的启动类,看看控制台效果
3、主题模式
主题模式与队列模式相类似,这里就不再多赘述,主要修改的是 application.yml 文件中的
pub-sub-domain: true
未完待续......(下篇开始讲讲 ActiveMQ 传输协议以及持久化机制)