# ActiveMQ_Course **Repository Path**: e4glet/active-mq_-course ## Basic Information - **Project Name**: ActiveMQ_Course - **Description**: 学习很容易,要有信心,不会的问老师。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2023-05-18 - **Last Updated**: 2024-05-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ActiveMQ 快速入门与提高课程 #### 简介 Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件;由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。 #### 特色 1. 支持Java消息服务(JMS) 1.1 版本 2. Spring Framework 3. 集群 (Clustering) 4. 支持的编程语言包括:C、C++、C#、Delphi、Erlang、Adobe Flash、Haskell、Java、JavaScript、Perl、PHP、Pike、Python和Ruby 5. 协议支持包括:OpenWire、REST、STOMP、WS-Notification、MQTT、XMPP以及AMQP #### 安装教程 1. 下载apache-activemq-5.15.8-bin.zip 2. 解压到指定的目录下,双击运行目录下的bin\win64\activemq.bat 3. ActiveMQ默认启动8161端口,启动方法在浏览器输入:http://localhost:8161/admin 默认用户名为admin、密码为admin ![输入图片说明](imgs/ActiveMQ01.png) #### ActiveMQ的使用【重点】 1. 如何创建Topics ![输入图片说明](imgs/ActiveMQ07.png) 2. 如何配置 查看官方文档:https://activemq.apache.org/ #### SpringBoot整合ActiveMQ【重点】 1. 创建SpringBoot项目并添加依赖 ![输入图片说明](imgs/ActiveMQ02.png) ![输入图片说明](imgs/ActiveMQ03.png) 2. pom.xml依赖 ``` org.springframework.boot spring-boot-starter-activemq ``` 3. application.yml配置信息 ``` spring: application: name: activemq-test # springboot整合activemq配置 activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: admin # 指定发送模式 (点对点 false , 发布订阅 true) jms: pub-sub-domain: false ``` 4. 消费者类 新建consume包,并添加消费者类: ``` package com.mq.activemq.consume; import org.apache.activemq.Message; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import javax.jms.JMSException; import javax.jms.TextMessage; /** * 消息的消费者 * 处理消息生产者发送过来的消息 */ @Component public class messageConsume { /** * 接收消息 */ @JmsListener(destination = "testQueue") public void receive(Message message){ if(message instanceof TextMessage){ TextMessage textMessage = (TextMessage) message; try { System.out.println("消费者接收的信息是:"+textMessage.getText()); }catch (JMSException e){ e.printStackTrace(); } } } } ``` 5. 生产者类 在测试类中模拟消息生产者: ``` package com.mq.activemq; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jms.core.JmsMessagingTemplate; /** * 测试类中模拟消息生产者 */ @SpringBootTest class ActiveMqApplicationTests { //注入消息发送模板JmsMessagingTemplate @Autowired JmsMessagingTemplate jmsMessagingTemplate; /** * 参数1:队列名称 * 参数2:消息内容 */ @Test void contextLoads() { jmsMessagingTemplate.convertAndSend("testQueue","消息生产者发送的消息"); } } ``` 分别运行消费者端和消息生产者测试类。 在终端可以看到消费者接收的消息: ![输入图片说明](imgs/ActiveMQ05.png) 并且我们在AativeMQ中可以看到消息队列信息: ![输入图片说明](imgs/ActiveMQ04.png) #### 其他消息类型的应用 修改消息生产者测试类: ``` package com.mq.activemq; import org.apache.activemq.command.ActiveMQMapMessage; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jms.core.JmsMessagingTemplate; import javax.jms.JMSException; import javax.jms.MapMessage; /** * 测试类中模拟消息生产者 */ @SpringBootTest class ActiveMqApplicationTests { //注入消息发送模板JmsMessagingTemplate @Autowired JmsMessagingTemplate jmsMessagingTemplate; /** * 参数1:队列名称 * 参数2:消息内容 */ @Test void contextLoads() throws JMSException { //创建消息Map MapMessage mapMessage = new ActiveMQMapMessage(); mapMessage.setString("key1","测试值"); mapMessage.setInt("key2",68); jmsMessagingTemplate.convertAndSend("testQueue",mapMessage); } } ``` 修改消费者类: ``` package com.mq.activemq.consume; import org.apache.activemq.Message; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import javax.jms.JMSException; import javax.jms.MapMessage; /** * 消息的消费者 * 处理消息生产者发送过来的消息 */ @Component public class messageConsume { /** * 接收消息 */ @JmsListener(destination = "testQueue") public void receive(Message message) throws JMSException { MapMessage mapMessage = (MapMessage) message; String key1 = mapMessage.getString("key1"); Integer key2 = mapMessage.getInt("key2"); System.out.println("消费者接收到的MapMessage信息是:"+key1+","+key2); } } ``` 先运行消费者等待接收,再运行消息生产测试类,查看终端结果如下图: ![输入图片说明](imgs/ActiveMQ06.png) #### 演示项目 1. ActiveMQ目录为IDEA创建的SpringBoot演示项目 #### 教程作者 1. e4glet