文章目录

  • ActivityMQ安装
  • 入门例子
    • pom.xml
    • MQ配置
    • 消息创建端
    • 消息消费端

ActivityMQ安装

前提:安装JDK

  1. 上官网下载apache-activemq-5.16.0windows版本,解压。
  2. 进入apache-activemq-5.16.0\bin\win64 双击ctivemq.bat
    窗口打印如下信息:
Size=104857600
jvm 1    |  INFO | Connector mqtt started
jvm 1    |  INFO | Starting Jetty server
jvm 1    |  INFO | Creating Jetty connector
jvm 1    |  WARN | ServletContext@o.e.j.s.ServletContextHandler@76a311d9{/,null,STARTING} has uncovered http methods for path: /
jvm 1    |  INFO | Listening for connections at ws://DESKTOP-HLLDRH0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector ws started
jvm 1    |  INFO | Apache ActiveMQ 5.16.0 (localhost, ID:DESKTOP-HLLDRH0-50964-1606466800157-0:1) started
jvm 1    |  INFO | For help or more information please see: http://activemq.apache.org
jvm 1    |  INFO | ActiveMQ WebConsole available at http://127.0.0.1:8161/
jvm 1    |  INFO | ActiveMQ Jolokia REST API available at http://127.0.0.1:8161/api/jolokia/

启动成功

浏览器上输入:http://127.0.0.1:8161/index.html
输入用户名密码admin/admin 默认

点击画圈的链接。

根据英文单词能看出菜单的含义。队列,主题、链接、网络等。做例子的时候可以点击看看具体里面是什么。

入门例子

需要编写一个消息生产端和一个消息消费方,达到消息的传递模拟。

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 https://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.4.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ycxy</groupId><artifactId>producer</artifactId><version>0.0.1-SNAPSHOT</version><name>producer</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-pool --><!-- 使用springboot2.0+及以下版本时候,maven配置依赖是: --><!-- <dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId><version>5.16.0</version></dependency>--><!--使用springboot2.1+时候,maven配置依赖是 --><dependency><groupId>org.messaginghub</groupId><artifactId>pooled-jms</artifactId><version>1.2.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

重点说明:看里面的注释,与 ConnectionFactory和JmsMessagingTemplate注入有关。使用了pooled-jms 之后这两个类就默认定义的Bean,可以直接使用,否则需要显示的自己创建。

MQ配置

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;@Configuration
public class MQconfig {@Value("${spring.activemq.broker-url}")private String brokerUrl;@Value("${spring.activemq.user}")private String username;@Value("${spring.activemq.topic-name}")private String password;@Value("${spring.activemq.queue-name}")private String queueName;@Value("${spring.activemq.topic-name}")private String topicName;@Bean(name = "queue")public Queue queue() {return new ActiveMQQueue(queueName);}@Bean(name = "topic")public Topic topic() {return new ActiveMQTopic(topicName);}//    @Bean
//    public ConnectionFactory connectionFactory(){//        return new ActiveMQConnectionFactory(username, password, brokerUrl);
//    }
//
//    @Bean
//    public JmsMessagingTemplate jmsMessageTemplate(){//        return new JmsMessagingTemplate(connectionFactory());
//    }// 在Queue模式中,对消息的监听需要对containerFactory进行配置@Bean("queueListener")public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();factory.setConnectionFactory(connectionFactory);factory.setPubSubDomain(false);return factory;}//在Topic模式中,对消息的监听需要对containerFactory进行配置@Bean("topicListener")public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();factory.setConnectionFactory(connectionFactory);factory.setPubSubDomain(true);return factory;}
}

这段在生产端和消费端都可以使用,配置以这样。Spring封装了对消息的操作,主要使用JmsMessagingTemplate进行操作,可以参考《Spring实战》一书。

消息创建端

我写了一个RestController用于测试消息产生。核心调用jmsMessagingTemplate.convertAndSend(destination, message);把消息产生之后发送给MQ哪个地方。

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;@RestController
public class ProducerController {//连接池jmspooled自动创建bean@Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;@Autowiredprivate Queue queue;@Autowiredprivate Topic topic;@PostMapping("/queue/test")public String sendQueue(@RequestBody String str) {this.sendMessage(this.queue, str);return "success";}@PostMapping("/topic/test")public String sendTopic(@RequestBody String str) {this.sendMessage(this.topic, str);return "success";}// 发送消息,destination是发送到的队列,message是待发送的消息private void sendMessage(Destination destination, final String message){jmsMessagingTemplate.convertAndSend(destination, message);}
}

消息消费端

创建一个监听用于监听指定的队列,接受到消息之后调用具体业务逻辑进行处理。

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class QueueConsumerListener
{//queue模式的消费者@JmsListener(destination="${spring.activemq.queue-name}", containerFactory="queueListener")public void readActiveQueue(String message) {System.out.println("queue接受到:" + message);}
}
@Component
public class TopicConsumerListener
{//topic模式的消费者@JmsListener(destination="${spring.activemq.topic-name}", containerFactory="topicListener")public void readActiveQueue(String message) {System.out.println("topic接受到:" + message);}
}

消息中间件-ActivityMQ系列文章-入门及例子相关推荐

  1. Python入门实战系列文章

    [时常听人说,一文解读某某技术,啥啥只看一文就够了,但一篇文章真的就能解读的了吗?不管你信不信,反正我是不信.]做为一个十多年开发经验的老程序员,在工作中,接触过各种各样的开发语言,前端后端都略有涉猎 ...

  2. Jenkins pipeline 入门到精通系列文章

    Jenkins2 入门到精通系列文章. Jenkins2 下载与启动 jenkins2 插件安装 jenkins2 hellopipeline jenkins2 pipeline介绍 jenkins2 ...

  3. saltstack之基础入门系列文章简介

    使用saltstack已有一段时间,最近由于各种原因,特来整理了saltstack基础入门系列文章,已备后续不断查阅(俗话说好记性不如烂笔头),也算是使用此工具的一个总结. saltstack的前六篇 ...

  4. 《高级着色语言HLSL入门》系列文章

    此系列文章由 博客园 天行健 归纳整理,此处仅作收藏与共享之用! 如欲转载该系列文章,需按如下方式于文章起始位置显式标明文章作者以及原文出处,以示尊重!! 文章作者:天行健 君子当自强而不息 原文出处 ...

  5. 《Managed DirectX +C# 开发(入门篇)》系列文章

    写在最前: 1.本系列文章版权归tongabcd所有,转载自dandanCool(并非原创作者),此处只作收藏与分享之用. 2.此系列文章如有再转,需按如下方式于文章醒目位置显示标明原创作者极其联系方 ...

  6. Spring batch系列文章(一)——介绍和入门

    只能靠写博客来鞭策自己学习了 Spring Batch 简介 what's 批处理 批处理的特点 spring batch 的好基友 spring batch 集成 spring boot 的入门程序 ...

  7. 用Python数据处理分析入门必备系列文章:环境安装

    最近有挺多小伙伴问我要入门 Python 的资料,还有小伙伴完全没有入门 Python 就直接购买了我的 pandas 专栏.因此我决定写几篇 Python 数据处理分析必备的入门知识系列文章,以帮助 ...

  8. openGauss数据库源码解析系列文章——openGauss开发快速入门(二)

    在上一篇openGauss数据库源码解析系列文章--openGauss开发快速入门(上)中,我们介绍了openGauss的安装部署方法,本篇将具体介绍openGauss基本使用. 二. openGau ...

  9. 领域驱动设计系列文章(1)——通过现实例子显示领域驱动设计的威力

    曾经参与过系统维护或是在现有系统中进行迭代开发的软件工程师们,你们是否有过这样的痛苦经历:当需要修改一个Bug的时候,面对一个类中成百上千行的代码,没有注释,千奇百怪的方法和变量名字,层层嵌套的方法调 ...

  10. 【微信小程序开发•系列文章一】入门

    本系统文章主要有以下几篇: <[微信小程序开发•系列文章一]入门> <[微信小程序开发•系列文章二]视图层> <[微信小程序开发•系列文章三]数据层> <[微 ...

最新文章

  1. USACO 1.2 挤牛奶
  2. electron 解压zip_如何将Node.js中的.zip/.rar文件解压缩到文件夹中
  3. 手把手引进门之 ASP.NET Core Entity Framework Core(官方教程翻译版 版本3.2.5)
  4. python爬虫网站简单_Python爬虫之简单爬虫框架实现
  5. 合成资产平台Public Mint与跨链资产协议Knit Finance达成合作
  6. python基础知识-Python基础知识
  7. ubuntu系统安装初始化脚本
  8. 模拟鼠标移动、点击,双击,键盘输入,鼠标滚轮滚动
  9. word中公式和文字不在一条水平线上
  10. 最简单最快速csv超大文件入库并统计Top5
  11. c语言编程:有理数比较,有理数类的设计
  12. oracle报1653解决办法,“IMP-00058: 遇到 ORACLE 错误 1653”,如何解决?
  13. 趋势杀毒曝远程执行漏洞 可盗取用户所有密码
  14. iOS 15 正式版发布,210 条改进大汇总
  15. 《linux内核分析》第三次课 实验作业
  16. 1032:大象喝水 题解 信息学奥赛 NOIP
  17. 硬盘参数 PIO/DMA/UDMA/SWDMA/MWDMA
  18. WinInet是什么?
  19. Hook是什么,React为什么要引入Hook
  20. OSChina 周五乱弹 —— 夏目不想上班,还想要甜甜的恋爱

热门文章

  1. 【分享】Adobe Flash Player各版本安装包官方直接下载地址
  2. C++超市管理系统(直接可以运行)
  3. AI眼中的世界 ——人工智能绘画入门
  4. Google Chrome谷歌浏览器离完整离线安装包下载地址整理总汇
  5. BlackBone工具集合:注入、hook、驱动程序
  6. 前端大佬们都在使用的JavaScript工具函数宝典-内含95个工具函数方法
  7. Unity3D lable控件
  8. 关于echarts3总chinamap的配置问题
  9. SecureCRT的下载、安装、使用( 过程非常详细~)
  10. java编写flash相册的制作软件_flash相册制作大师免费版