文章目录

  • 一、依赖配置引入
    • 1. 引入SpringBoot整合RabbitMQ依赖
    • 2. 生产者配置文件
    • 3. 主配置
  • 二、代码Conding
    • 2.1. 生产者代码
    • 2.2. 实体对象
    • 2.3. 测试类

一、依赖配置引入

1. 引入SpringBoot整合RabbitMQ依赖

        <!--springboot整合RabbitMQ依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

2. 生产者配置文件

#RabbitMQ 连接信息
spring:rabbitmq:addresses: 127.0.0.1port: 5672username: adminpassword: admin#虚拟主机virtual-host: /admin#连接超时时间connection-timeout: 15000##开启 confirm 确认机制#发送确认 对应RabbitTemplate.ConfirmCallback接口#消息发送成功 有2个重要参数# ack 状态为true correlationId 全局唯一ID用于标识每一支队列publisher-confirms: true#开启 return 确认机制publisher-returns: true#设置为 true 后 消费者在消息没有被路由到合适队列情况下会被return监听,而不会自动删除#发送失败回退,对应RabbitTemplate.ReturnCallback接口template:mandatory: true

3. 主配置

package com.gblfy.springboot.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan({"com.gblfy.springboot.*"})
public class MainConfig {}

二、代码Conding

2.1. 生产者代码

package com.gblfy.springboot.producer;import com.gblfy.springboot.entity.Order;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;import java.util.Map;@Component
public class RabbitMQSender {//自动注入RabbitTemplate模板类@Autowiredprivate RabbitTemplate rabbitTemplate;/*** MQ发送 字符串类型消息+额外的属性** @param message* @param properties* @throws Exception*///发送消息方法调用: 构建Message消息public void send(Object message, Map<String, Object> properties) throws Exception {//构造一个添加额外属性的容器 储存额外消息MessageHeaders mhs = new MessageHeaders(properties);Message msg = MessageBuilder.createMessage(message, mhs);//自动签收rabbitTemplate.setConfirmCallback(confirmCallback);//消息确认rabbitTemplate.setReturnCallback(returnCallback);//id + 时间戳 全局唯一CorrelationData correlationData = new CorrelationData("1234567890");rabbitTemplate.convertAndSend("exchange-1", "springboot.abc", msg, correlationData);}/*** 发送MQ 对象类型消息** @param order* @throws Exception*///发送消息方法调用: 构建自定义对象消息public void sendOrder(Order order) throws Exception {rabbitTemplate.setConfirmCallback(confirmCallback);rabbitTemplate.setReturnCallback(returnCallback);//id + 时间戳 全局唯一CorrelationData correlationData = new CorrelationData("0987654321");rabbitTemplate.convertAndSend("exchange-2", "springboot.ff", order, correlationData);}//回调函数: confirm确认final ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) {System.err.println("correlationData: " + correlationData);System.err.println("ack: " + ack);if (!ack) {System.err.println("异常处理....");}}};//回调函数: return返回final ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {@Overridepublic void returnedMessage(org.springframework.amqp.core.Message message, int replyCode, String replyText,String exchange, String routingKey) {System.err.println("return exchange: " + exchange + ", routingKey: "+ routingKey + ", replyCode: " + replyCode + ", replyText: " + replyText);}};
}

2.2. 实体对象

package com.gblfy.springboot.entity;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Order implements Serializable {private String id;private String name;
}

2.3. 测试类

package com.gblfy.springboot;import com.gblfy.springboot.entity.Order;
import com.gblfy.springboot.producer.RabbitMQSender;
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.SpringRunner;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;@RunWith(SpringRunner.class)
@SpringBootTest
public class ProducerApplicationTests {@Testpublic void contextLoads() {}@Autowiredprivate RabbitMQSender rabbitMQSender;private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");@Testpublic void testSender1() throws Exception {Map<String, Object> properties = new HashMap<>();properties.put("number", "12345");properties.put("send_time", simpleDateFormat.format(new Date()));rabbitMQSender.send("Hello RabbitMQ For Spring Boot!", properties);}@Testpublic void testSender2() throws Exception {Order order = new Order("001", "第一个订单");rabbitMQSender.sendOrder(order);}
}

(需求实战_01) SpringBoot2.x 整合RabbitMQ_生产端相关推荐

  1. SpringBoot2.x 整合RabbitMQ_消费端

    这一篇讲解消费者 文章目录 一.依赖配置 1. 引入依赖 2. 配置文件 3. 主配置 二.代码Conding 2.1. 消费者代码 一.依赖配置 1. 引入依赖 <!--springboot整 ...

  2. (需求实战_01)_shell脚本 ftp协议下载文件

    文章目录 一.需求文档说明 二.脚本解释/说明 三.脚本内容 3.1. 案例脚本 3.2. 案例脚本升级 一.需求文档说明 序号 要求 说明 ① 协议 使用ftp协议远程下载 ② 文件类型 .zip ...

  3. (需求实战_终章) SpringBoot2.x 整合RabbitMQ

    文章目录 1. maven依赖 2. MainConfig 3. application.properties 4. 发送字符串 生产者 5. 发送对象 生产者 6. 接收字符串客户端 7. 接收对象 ...

  4. 视频教程-19年录制SpringBoot2.x整合微信支付在线教育网站项目实战-Java

    19年录制SpringBoot2.x整合微信支付在线教育网站项目实战 7年的开发架构经验,曾就职于国内一线互联网公司,开发工程师,现在是某创业公司技术负责人, 擅长语言有node/java/pytho ...

  5. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download           2.新 ...

  6. SpringBoot2.x整合redis实战讲解

    SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-boot/do ...

  7. SpringBoot2.0整合Redis实战

    SpringBoot2.x整合Redis实战 1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/down ...

  8. (需求实战_进阶_02)SSM集成RabbitMQ 关键代码讲解、开发、测试

    接上一篇:(企业内部需求实战_进阶_01)SSM集成RabbitMQ 关键代码讲解.开发.测试 https://gblfy.blog.csdn.net/article/details/10419730 ...

  9. SpringBoot2.x 整合websocket 消息推送,单独发送信息,群发信息

    根据公司需求在SpringBoot项目中集成站内信,于是,我做了一个SpringBoot2.x 整合websocket 消息推送,给指定用户发送信息和群发信息即点点对方式和广播方式2种模式. 文章目录 ...

最新文章

  1. 优秀的博客与文章总结链接地址
  2. 转载:python3 安装pycrypto
  3. 超图球面与平面场景的区别和地理坐标系
  4. 状态管理 界面数据信息
  5. boost::multiprecision模块float128相关的测试程序
  6. c语言结构体易错点,C语言结构体注意点
  7. spring的InitializingBean介绍
  8. [JZOJ5836] Sequence
  9. DPDK内存篇(一): 基本概念
  10. 【数据结构 严蔚敏版】 排序基本操作
  11. XP计算机屏蔽vac系统,网吧屏蔽VAC一键修复——V1.1by RiCkY
  12. 『伪原创工具 』英文在线伪原创工具
  13. 在线3D大脑建模网站分享
  14. tom邮箱怎么样,邮箱一天能发多少邮件
  15. NotePad++ HexEditor.dll下载地址,32位,64位
  16. python图像处理学习笔记
  17. 微信小程序连接物联网(二):NodeMCU Lua学习笔记
  18. 数字图像中手写阿拉伯数字的识别技术概览
  19. [从零开始学习FPGA编程-41]:视野篇 - 摩尔时代与摩尔定律以及后摩尔时代的到来
  20. App安全架构之前端安全防护

热门文章

  1. 数学生物学:生命是数字游戏
  2. 数风·数林 | 炉石传说中的概率(声控篇)
  3. MNTD论文修改20211114(Y Zhou)
  4. k-shingles和MinHash优秀文章保存
  5. Unicode和Utf-8
  6. 千万商家的智能决策引擎--AnalyticDB如何助力生意参谋双十一
  7. 如何快速实现精准的个性化搜索服务
  8. 数据湖元数据服务的实现和挑战
  9. Dubbo在互金行业的应用
  10. uDevice Center - IoT弹性在线多设备开发平台