安装

推荐一篇博客https://blog.csdn.net/zhuzhezhuzhe1/article/details/80464291

项目结构

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>rabbitmq</name>
<description>Spring Boot 整合RabbitMQ</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <!-- rabbitmq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</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>

POM.XML

application.yml

需要将publisher-confrems设为true,启动确认回调, 将 publisher-returns设为true 确认返回回调

rabbitmq配置类--RabbitConfig

第一部分, 定义队列

第二部分,设置一些消息处理策略

 package com.example.rabbitmq;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; /**
* rabbitMq 配置类
* @author milicool
* Created on 2018/9/14
*/
@Configuration
public class RabbitConfig {
@Resource
private RabbitTemplate rabbitTemplate; /**
* 定义一个hello的队列
* Queue 可以有4个参数
* 1.队列名
* 2.durable 持久化消息队列 ,rabbitmq重启的时候不需要创建新的队列 默认true
* 3.auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
* 4.exclusive 表示该消息队列是否只在当前connection生效,默认是false
*/
@Bean
public Queue helloQueue() {
return new Queue("queue-test");
} /** ======================== 定制一些处理策略 =============================*/ /**
* 定制化amqp模版
*
* ConfirmCallback接口用于实现消息发送到RabbitMQ交换器后接收ack回调 即消息发送到exchange ack
* ReturnCallback接口用于实现消息发送到RabbitMQ 交换器,但无相应队列与交换器绑定时的回调 即消息发送不到任何一个队列中 ack
*/
@Bean
public RabbitTemplate rabbitTemplate() {
Logger log = LoggerFactory.getLogger(RabbitTemplate.class); // 消息发送失败返回到队列中, yml需要配置 publisher-returns: true
rabbitTemplate.setMandatory(true); // 消息返回, yml需要配置 publisher-returns: true
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
String correlationId = message.getMessageProperties().getCorrelationIdString();
log.debug("消息:{} 发送失败, 应答码:{} 原因:{} 交换机: {} 路由键: {}", correlationId, replyCode, replyText, exchange, routingKey);
}); // 消息确认, yml需要配置 publisher-confirms: true
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (ack) {
// log.debug("消息发送到exchange成功,id: {}", correlationData.getId());
} else {
log.debug("消息发送到exchange失败,原因: {}", cause);
}
}); return rabbitTemplate;
}
}

配置类

生产者

 /**
* 生产者
* @author milicool
* Created on 2018/9/14
*/
@Component
public class Producer { @Autowired
private RabbitTemplate rabbitTemplate; /**
* 给hello队列发送消息
*/
public void send() {
for (int i =0; i< 100; i++) {
String msg = "hello, 序号: " + i;
System.out.println("Producer, " + msg);
rabbitTemplate.convertAndSend("queue-test", msg);
}
} }

消费者

 /**
* 消费者
* @author milicool
* Created on 2018/9/14
*/
@Component
public class Comsumer {
private Logger log = LoggerFactory.getLogger(Comsumer.class); @RabbitListener(queues = "queue-test")
public void process(Message message, Channel channel) throws IOException {
// 采用手动应答模式, 手动确认应答更为安全稳定
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
log.info("receive: " + new String(message.getBody()));
}
}

测试类

 @RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqApplicationTests { @Autowired
private Producer producer; @Test
public void contextLoads() {
producer.send();
} }

测试结果

测试结果太长,没有截取全部,可以查看到消费者接收到了全部消息,如果有的消息在没有接收完,消息将被持久化,下次启动时消费

web端查看

感谢阅读  o(∩_∩)o

springboot整合rabbitmq,支持消息确认机制的更多相关文章

  1. SpringBoot 整合 RabbitMQ 实现消息可靠传输

    消息的可靠传输是面试必问的问题之一,保证消息的可靠传输主要在生产端开启 comfirm 模式,RabbitMQ 开启持久化,消费端关闭自动 ack 模式. 环境配置 SpringBoot 整合 Rab ...

  2. RabbitMQ的消息确认机制

    一:确认种类 RabbitMQ的消息确认有两种. 一种是消息发送确认.这种是用来确认生产者将消息发送给交换器,交换器传递给队列的过程中,消息是否成功投递.发送确认分为两步,一是确认是否到达交换器,二是 ...

  3. RabbitMQ 之消息确认机制(事务+Confirm)

    概述 在 Rabbitmq 中我们可以通过持久化来解决因为服务器异常而导致丢失的问题,除此之外我们还会遇到一个问题:生产者将消息发送出去之后,消息到底有没有正确到达 Rabbit 服务器呢?如果不错得 ...

  4. RabbitMQ (十一) 消息确认机制 - 消费者确认

    由于生产者和消费者不直接通信,生产者只负责把消息发送到队列,消费者只负责从队列获取消息(不管是push还是pull). 消息被"消费"后,是需要从队列中删除的.那怎么确认消息被&q ...

  5. RabbitMQ(四): rabbitmq 的消息确认机制(事务+confirm)

    在 rabbitmq 中我们可以通过持久化数据解决 rabbitmq 服务器异常的数据丢失问题. 问题:生产者将消息发送出去之后,消息到底有没有到达 rabbitmq 服务器.默认情况下是不知道的. ...

  6. springboot整合rabbitMq实现消息延时发送

    实现思路:利用mq的ttl设置消息失效时间 当达到设置时间后通过交换机到达死信队列中,消费者端绑定读取死信队列中信息来达到延时发送消息的功能. demo 如下: (1)在pom.xml 中引入rabb ...

  7. SpringBoot整合Rabbitmq设置消息请求头

    String str = "{\"origin\":\"BBC\",\"origin_coupon_id\":51,\" ...

  8. 【Java】SpringBoot整合RabbitMQ

    介绍 RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,AMQP,即A ...

  9. RabbitMQ消息确认机制

    文章目录 1. 事务机制2. Confirm模式2.1 生产者2.1.1 普通Confirm模式2.1.2 批量Confirm模式2.1.3 异步Confirm模式2.2 消费者3. 其他 消费者如何 ...

随机推荐

  1. 二、搭建SpringBoot项目

    与其说是搭建,还不如说去下载...(注意,在此之前要确保你的3000块钱的笔记本上安装了JDK8+已经最新的相对较新的maven:apache-maven-3.6.0,至于JDK以及maven的相关安 ...

  2. 比较php字符串连接的效率

    php字符串连接有三种方式 1)使用 . 链接 2)使用 .= 连接 3)implode 函数连接数组元素 /*以下测试在ci框架进行*/ private function get_mcrotime( ...

  3. eclipse操作

    1.手动添加组件源码 2.源码阅读技巧 选择类Ctrl+T(Quick Type Hierarchy),查看该类的继承关系: 选择方法Ctrl+Alt+H(Open Call Hierarchy),查 ...

  4. 计算机基础知识和tcp详解

    计算机基础知识 作为应用软件开发程序员是写应用软件的,而应用软件必须应用在操作系统之上,调用操作系统接口,由操作系统控制硬件 比如客户端软件想要基于网络发送一条消息给服务端软件,流程是: 1.客户端软 ...

  5. Sqoop 遇到的问题

    1.   想用 sqoop 增量的方式导入到 hive.运行下面的命令: sqoop import --connect jdbc:mysql://192.168.7.159:3306/test --u ...

  6. MyBatis与Hibernate比较

    MyBatis: 1.是一个sql语句映射的框架(工具). 2.注重pojo与sql之间的映射关系.不会为程序员在运行期自动生成sql 3.自动化程度低,手工映射sql灵活程度高 4.需要开发人员熟练 ...

  7. 14、OpenCV Python 直线检测

    __author__ = "WSX" import cv2 as cv import numpy as np #-----------------霍夫变换------------- ...

  8. SDUT OJ 迷之好奇 (字典树 )

    迷之好奇 Time Limit: 2000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description FF得到了一个有n个数字的集 ...

  9. Win7 如何阻止程序联网

    https://jingyan.baidu.com/article/9113f81b03d4e12b3214c7c3.html

  10. 修复win10无法双击打开txt文档.reg

    Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\.txt]@="txtfile""Content Type& ...