当前位置: 首页 > news >正文

JAVA-springboot 整合Activemq

SpringBoot从入门到精通-第17章 消息中间件ActiveMQ

一、ActiveMQ简介

消息中间件基于队列与消息传递技术,在网络环境中为应用系统提供同步或异步的消息传输。在传递消息的过程中,发送消息的一方被称为生产者,接收消息的一方被称为消费者。这是一种概括性的称呼,凡是实现消息传递的双方都可以被称作生产者和消费者,它们可以是两个系统、两个程序、两个线程,甚至可以是同一个类中的两个方法。
消息中间件两个功能

  • 异步调用
  • 消减峰值

消息中间件两种传递模式

  • 点对点
    • 一个消息队列可以同时服务多个生产者和多个消费者,生产者将消息放到消息队列中,供消费者进行消费,一个消息只能被一个消费者消费的传递模式称作“点对点”模式,也叫一对一模式。
  • 发布/订阅
    • 生产者放入队列中的消息可以同时被多个消费者消费的传递模式称作“发布/订阅”模式。

二、示例

利用消息中间件异步保存订单数据
  • 模拟表单提交消息到ActiveMQ;
  • 提交成功返回“提交订单成功”,否则返回“传入的数据有误”。

三、整合ActiveMQ

2.1整体操作步骤

  • 安装部署ActiveMQ
  • 创建SpringBoot程序
  • 添加ActiveMQ依赖
  • 添加ActiveMQ配置
  • 编写实体类
  • 编写ActiveMQ配置类
  • 编写ActiveMQ消费者
  • 编写ActiveMQ生产者
  • 编写SpringBoot控制器
  • 编写index.html
  • 访问测试

2.2详细操作步骤

  • 安装部署ActiveMQ
    – 解压activemq.tar
    — tar xf activemq.tar
    – 740授权
    — chmod 740 activemq
    – 配置好java环境
    –启动ActiveMQ
    —cd bin && ./activemq start
    —ps -ef|grep activema
    端口8161 61616

管理路径为 http://localhost:8161 admin admin
业务路径为 tcp://localhost:61616
在这里插入图片描述

1、管理页面创建队列并发送消息
Queue–name–create
右侧operations—send to
2、命令行查看队列
./activemq browse event01
./activemq browse --amqurl tcp://127.0.0.1:61616 --user admin --password admin event01

  • 创建SpringBoot程序
    在这里插入图片描述

  • 添加ActiveMQ依赖

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency>

整体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.5.5</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.mr</groupId><artifactId>OrderDemo</artifactId><version>0.0.1-SNAPSHOT</version><name>OrderDemo</name><description>Demo project for Spring Boot</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><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><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>
  • 添加ActiveMQ配置
spring.activemq.broker-url=tcp://10.0.0.132:61616
activemq.queue.name=order_queue
  • 编写实体类
package com.mr.dto;public class Order {private String orderId;// 订单流水号private String goodsId;// 商品编号private String goodsName;// 商品名称private String goodsType;// 商品类型private String goodsCount;// 商品数量private String goodsUnit;// 商品单位private String createDate;// 下单日期private String creator;// 创建人public Order() {}public Order(String orderId, String goodsId, String goodsName, String goodsType, String goodsCount,String goodsUnit, String createDate, String creator) {this.orderId = orderId;this.goodsId = goodsId;this.goodsName = goodsName;this.goodsType = goodsType;this.goodsCount = goodsCount;this.goodsUnit = goodsUnit;this.createDate = createDate;this.creator = creator;}public String getOrderId() {return orderId;}public void setOrderId(String orderId) {this.orderId = orderId;}public String getGoodsId() {return goodsId;}public void setGoodsId(String goodsId) {this.goodsId = goodsId;}public String getGoodsName() {return goodsName;}public void setGoodsName(String goodsName) {this.goodsName = goodsName;}public String getGoodsType() {return goodsType;}public void setGoodsType(String goodsType) {this.goodsType = goodsType;}public String getGoodsCount() {return goodsCount;}public void setGoodsCount(String goodsCount) {this.goodsCount = goodsCount;}public String getGoodsUnit() {return goodsUnit;}public void setGoodsUnit(String goodsUnit) {this.goodsUnit = goodsUnit;}public String getCreateDate() {return createDate;}public void setCreateDate(String createDate) {this.createDate = createDate;}public String getCreator() {return creator;}public void setCreator(String creator) {this.creator = creator;}@Overridepublic String toString() {return "Order [orderId=" + orderId + ", goodsId=" + goodsId + ", goodsName=" + goodsName + ", goodsType="+ goodsType + ", goodsCount=" + goodsCount + ", goodsUnit=" + goodsUnit + ", createDate=" + createDate+ ", creator=" + creator + "]";}}
  • 编写ActiveMQ配置类

package com.mr.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.context.annotation.Configuration;@Configuration
public class ActiveMqConfig {@Value("${activemq.queue.name}")String queueName;@Beanpublic Queue queue() {return new ActiveMQQueue(queueName);}
}
  • 编写ActiveMQ消费者

package com.mr.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mr.dto.Order;@Service
public class QueueConsumer {@AutowiredObjectMapper jackson;@JmsListener(destination = "${activemq.queue.name}")public void receiveQueueMsg(String msg) throws JsonMappingException, JsonProcessingException {System.out.println("接收的订单数据:" + msg);Order order = jackson.readValue(msg, Order.class);System.out.println("(模拟)将数据保存到数据库,提交的单号为:" + order.getGoodsId());}
}
  • 编写ActiveMQ生产者

package com.mr.service;
import javax.jms.Queue;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsOperations;
import org.springframework.stereotype.Service;@Service
public class QueueProducer {@AutowiredJmsOperations jmsTemplate;@AutowiredQueue queue;public void sendMessage(String message) {jmsTemplate.convertAndSend(queue, message);}}
  • 编写SpringBoot控制器

package com.mr.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mr.dto.Order;
import com.mr.service.QueueProducer;@Controller
public class OrderController {@AutowiredQueueProducer producer;@AutowiredObjectMapper jackson;@RequestMapping("/submit")@ResponseBodypublic String getOrder(String orderId, String goodsId, String goodsName, String goodsType,String goodsCount, String goodsUnit, String createDate, String creator) {Order order = new Order(orderId, goodsId, goodsName, goodsType, goodsCount,goodsUnit, createDate, creator);try {producer.sendMessage(jackson.writeValueAsString(order));} catch (JsonProcessingException e) {e.printStackTrace();return "传入的数据有误";}return "提交订单成功";}@RequestMapping("/index")public String index() {return "index";}
}
  • 编写index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body><form action="/submit" method="post"><a>订单流水号</a><input type="text" name="orderId" /><br><a>商品编号</a><input type="text" name="goodsId" /><br><a>商品名称</a><input type="text" name="goodsName" /><br><a>商品类型</a><input type="text" name="goodsType" /><br><a>商品数量</a><input type="text" name="goodsCount" /><br><a>商品单位</a><input type="text" name="goodsUnit" /><br><a>下单日期</a><input type="text" name="createDate" /><br><a>创建人</a><input type="text" name="creator" /><br><input type="submit" value="提交" /></form>
</body>
</html>
  • 访问测试
    http://127.0.0.1:8080/index在这里插入图片描述

填写表单信息,点击提交
在这里插入图片描述
在这里插入图片描述
查看下ActiveMQ变化
在这里插入图片描述

http://www.dtcms.com/a/277898.html

相关文章:

  • ECU(电子控制单元)是什么?
  • C++中顶层const与底层const
  • JSX 语法
  • 【前端知识】移动端APP原生应用与H5交互底层逻辑
  • Dubbo跨越分布式事务的最终一致性陷阱
  • 有效感受野(ERF)可视化工具
  • hash表的模拟--开放定址法
  • 如何将本地代码同步到远程Github仓库
  • 【Docker基础】Dockerfile指令速览:环境与元数据指令详解
  • OSPF与BGP的联动特性
  • Utils系列之内存池(MultiSizePool)
  • 【MLLM】多模态理解GLM-4.1V-Thinking模型
  • OpenVela 日志系统:从配置到落地的实操手册
  • Python装饰器(自定义装饰器和3个内置装饰器)
  • Java反射机制深度解析
  • 树莓派5-ollama-linux-arm64.tgz 下载
  • AEC线性处理
  • 在 OCI 生成式 AI 上搭一个「指定地区拉面店 MCP Server」——从 0 到 1 实战记录
  • 《数据库》MySQL事务
  • gcc 源码阅读--C语言预处理
  • (一)SAP Group Reporting (GR) 集团财务合并解决方案套件概述
  • 构造函数延伸应用
  • [Python 基础课程]字典
  • 代码随想录算法训练营第十七天
  • spring--@Autowired
  • LlamaIndex Querying 自定义查询
  • JavaScript数据结构算法
  • js入门01
  • YOLOv5目标检测标准化流程
  • 013_流式输出与实时响应