在rabbitmq中我们也有类似数据库的事务需求,及当程序运行过程中出现异常时,不能完整的执行一个流程时,为了保持功能完整性,我们需要将之前发送的消息也不让他发送出去,此时就需要使用到rabbitmq的事务功能。rabbitmq的事务功能与消息确认机制不能共存,只能同时使用其中一种。

在资源文件中我们需要引入外部的事务管理器transactionManager来控制rabbitmq的事务。以及在资源文件中配置其消息发送对象时声明 channel-transacted="true" 。在我们执行主要业务流程的方法上添加上spring的@Transaction注解来实现方法内的消息控制在同一事务内。

项目完整文件如下:

  1. pom.xml
  2. src/main/resources/application.properties
  3. src/main/resources/spring/transcation-rabbitmq.xml
  4. src/main/java/com/xiaohui/rabbitmq/ProducerApplication.java
  5. src/main/java/com/xiaohui/rabbitmq/controller/MsgController.java

一、pom.xml

pom.xml引入了spring web模块主要用来调用链接 测试模拟发送消息。

<?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.xiaohui.mqproducer</groupId><artifactId>MqProducer</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency></dependencies></project>

二、springboot配置问题application.properties

#端口
server.port=8888
#rabbitMq配置
spring.rabbitmq.host=172.18.255.118
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/myhost
spring.rabbitmq.username=xiaohui
spring.rabbitmq.password=root

三、spring资源文件 transcation-rabbitmq.xml

事务主要配置在这里,注意消息发送对象 不能遗漏配置 channel-transacted="true" 。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:rabbit="http://www.springframework.org/schema/rabbit"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsd"><!-- xml可以使用$占位符 --><context:property-placeholder location="classpath*:application.properties" /><!--  在使用事务时 链接工厂不能使用  publisher-confirms="true" publisher-returns="true" ,事务与工厂配置消息确认机制不能共存 --><rabbit:connection-factory id="connectionFactory"host="${spring.rabbitmq.host}"port="${spring.rabbitmq.port}"username="${spring.rabbitmq.username}"password="${spring.rabbitmq.password}"virtual-host="${spring.rabbitmq.virtual-host}"/><!-- 定义消息发送类,并指定回调处理对象 以及连接工厂信息 --><rabbit:template id="rabbitTemplate"connection-factory="connectionFactory"channel-transacted="true" /><!-- 事务管理器 --><bean id="transactionManager" class="org.springframework.amqp.rabbit.transaction.RabbitTransactionManager"><property name="connectionFactory" ref="connectionFactory"/></bean></beans>

四、springboot启动类ProducerApplication.java

在启动类中我们使用@ImportResource 引入了第三步的spring资源文件transcation-rabbitmq.xml。

package com.xiaohui.rabbitmq;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;/*** 生产者启动类*/
@SpringBootApplication
@ImportResource({"classpath:spring/transcation-rabbitmq.xml"})
public class ProducerApplication {public static void main(String[] args) {SpringApplication.run(ProducerApplication.class,args);}
}

五、web层Controller类MsgController.java

用来模拟发送消息以及 方法内报错时验证消息是否正常回滚。发送消息方法需要添加spring 注解@Transaction

package com.xiaohui.rabbitmq.controller;import com.xiaohui.rabbitmq.config.RabbitMqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MsgController {@AutowiredRabbitTemplate rabbitTemplate;@GetMapping("/sendTransMsg")@Transactionalpublic String sendTransMsg(@RequestParam String msg,@RequestParam String exchange,@RequestParam String key){rabbitTemplate.convertAndSend(exchange,key,msg);//模拟报错Integer.parseInt(msg);rabbitTemplate.convertAndSend(exchange,key,"@@:"+msg);return "消息发送成功!";}
}

六、启动工程,通过消息参数分别模拟正常发送情况和报错情况。

场景1:我们在消息参数上传入数字值时请求后在mq控制台可以看到消息正常的进入到了队列中。

场景2:当我们在消息参数中传入其他非数字时,发送消息逻辑中发生异常,通过控制台观察没有消息进入到队列中。

项目工程结构如下,划线文件为未使用到噢文件,本示例中消息队列以及交换机应之前做好了创建绑定关系。

RabbitMq(十四)消息的事务支持及代码演示相关推荐

  1. RabbitMQ中7种消息队列和保姆级代码演示!

    blog.csdn.net/qq_32828253/article/details/110450249 七种模式介绍与应用场景 简单模式(Hello World) 做最简单的事情,一个生产者对应一个消 ...

  2. springcloudstream+rabbitmq+eureka进行消息发送和接收实例代码

    文章目录 eureka作注册中心的配置: 消息提供方: 消费者代码 注册中心.消息接受者.消息提供者分别启动: eureka作注册中心的配置: 依赖包: <dependencies>< ...

  3. 嵌入式Linux系统编程学习之二十四消息队列

    文章目录 前言 一.msgget 函数 二.msgsnd 函数 三.msgctl 函数 补充 前言   消息队列与 FIFO 很相似,都是一个队列结构,都可以有多个进程往队列里面写信息,多个进程从队列 ...

  4. 牛客网力扣算法编程之十四 | 字符串 - 字符个数统计 - Java代码实现

    [算法编程]字符个数统计 一. 题目描述 编写一个函数,计算字符串中含有的不同字符的个数.字符在 ASCII 码范围内( 0~127 ,包括 0 和 127 ),换行表示结束符,不算在字符里.不在范围 ...

  5. JavaScript实现中国地图圆点标注(二十四)

    JavaScript实现中国地图圆点标注(二十四) 效果如下: 部分代码如下: <!DOCTYPE html> <html lang="en"><he ...

  6. 视觉SLAM十四讲(第二版)环境安装心得体会

    前言 将高博的视觉SLAM14讲(第二版)看完后,也将里面所有的代码都跑了一遍,在安装环境的时候遇到了无数的坑.为了后来的人能够更加快速上手SLAM,现将书中环境安装心得记录如下. 环境安装 1. 安 ...

  7. 【读书笔记】《视觉SLAM十四讲(高翔著)》 第13讲

    文章目录 工程文件一:dense_monocular(单目稠密地图) 工程文件二:dense_RGBD(点云地图 & 八叉树地图) 本博客的内容是本章程序编译运行方法,记录调通本章程序的过程. ...

  8. STM32驱动SDIO WIFI 介绍(十六) ---- 上位机UDP操作/代码

    代码工程的GITHUB连接:点进进入GITHUB仓库 https://github.com/sj15712795029/stm32f1_marvell88w8801_marvell8801_wifi ...

  9. RabbitMQ(十):RabbitMQ 如何保证消息的可靠性

    一条消费成功被消费经历了生产者->MQ->消费者,因此在这三个步骤中都有可能造成消息丢失. 一 消息生产者没有把消息成功发送到MQ 1.1 事务机制 AMQP协议提供了事务机制,在投递消息 ...

最新文章

  1. 一键获取解锁码_Windows 骚操作:轻轻一按手机指纹解锁电脑!
  2. 【编译原理】递归下降的预测分析(真の能看懂~!)
  3. Socket recv()之前进行select代码
  4. android组件通讯 Intent-Action属性
  5. 【栈】【232. 用栈实现队列】【简单】
  6. Mac解决Apache2目录权限问题
  7. 2010-03-23 杂七杂八
  8. ubuntu16.04安装百度网盘(使用deepin-wine)2019年3月亲测可用以及安装MATLAB的技巧
  9. Ubuntu下网络调试助手 NetAssist(实际这个我启动不了)
  10. 20155313 杨瀚 《网络对抗技术》实验二 后门原理与实践
  11. Linux 之十三 嵌入式系统搭建工具 Yocto、OpenEmbedded、BitBake 详解
  12. 一文带你快速入门【哈希表】
  13. 会声会影试用版到期了怎么办_会声会影2018试用版如何正确安装、卸载?
  14. 老掉牙的ASP文件的加密与解密
  15. EBYTE ROLA通信模块初步学习
  16. uniapp 监听网络情况
  17. 银行暑期实习生面试经验
  18. 数学建模美赛写作指导20篇(一)-美赛数学专业词汇
  19. 10.java正则表达式URL匹配
  20. c语言矩阵对角线之和

热门文章

  1. .NET 指南:包装异常
  2. 调整idea中控制台及右侧提示框字体大小
  3. Spring定时任务并行(异步)处理
  4. mvvm怎么让光标制定属性的文本框_Word怎么快速制作斜线表头?10秒搞定,表格颜值直线上升...
  5. C# 利用反射机制开启控件双缓存
  6. 西北大学集训队选拔赛 F-三生三世(STL set和map的简单应用)
  7. 科技英语翻译计算机化考试,2017年英语四级翻译范文之考公热
  8. element ui分页怎么做_vue+element-ui的分页完整版
  9. Prometheus-使用Prometheus监控Kubernetes集群
  10. 权限修饰符(public、protected、default、private)权限验证