springBoot-rabbitMq手动确认消息

代码基础怎么写我就不说了,看我的另一篇博客

springBoot整合RabbitMQ(Demo)_我要用代码向我喜欢的女孩表白的博客-CSDN博客

假设你要手动ack,怎么做呢?

通常自动是,mq发给服务端,服务端收到了就当处理过了,但是我们要保证数据不丢失。所以得处理完了,才告诉mq说我做了。所以做完了在ack确认,遇到了异常就重发。比如a,b,c,三台机器发给了b,b收到没有处理完,就被关机了。那这条消息就没有发完。

依赖

复制代码
<!--MQ-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.properties的配置

复制代码
spring.rabbitmq.host=xxxx
spring.rabbitmq.port=5672
spring.rabbitmq.username=xxxxx
spring.rabbitmq.password=xxxxx
spring.rabbitmq.listener.simple.cknowledge-mode=manual
spring.rabbitmq.listener.simple.prefetch=1
spring.rabbitmq.publisher-returns=true

下面黄色这一段就是ack手动确认。

复制代码
MqReceiveDevListener消费者
复制代码
package com.dengta.tanzhiwcustomermarket.config;


import com.dengta.tanzhiwcustomermarket.tools.Constants;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
public class MqReceiveDevListener {



//@Value("${dev-gen-audio-url}")
//String genAudioUrl;
//
//@Value("${dev-audio-token}")
//String audioToken;


    private static final Logger logger = LoggerFactory.getLogger(MqReceiveDevListener.class);


    //---------------------------下面为dev环境下对应的消息队列-----------------------------------------



//todo 1.多个消费者并行消费
    @RabbitListener(queues = Constants.DT_MARKET_MQ_TASK,concurrency = "3")
    /**
     * 这个content是一个json,由barrageController发来的
     */
    public void receiveDouYinBarrageDev(String content, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {

        try {
            Thread.sleep(1000*10);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                channel.basicAck(tag, false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
相关推荐
weixin_425023006 分钟前
Spring Boot + MyBatis Plus JOIN 分页多表查询项目文档
spring boot·后端·mybatis
肉丸滚球1 小时前
飞算 JavaAI 转 SpringBoot 项目沉浸式体验:高效开发在线图书借阅平台
java·spring boot·后端
叫我阿柒啊1 小时前
从Java全栈到前端框架:一场真实的技术面试对话
java·vue.js·spring boot·微服务·typescript·前端开发·后端开发
泉城老铁3 小时前
目前开源架构需要注意的安全问题
spring boot·后端
一 乐3 小时前
健身房预约|基于springboot + vue健身房预约小程序系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·小程序
JIngJaneIL5 小时前
基于java+ vue家庭理财管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
老华带你飞5 小时前
电商系统|基于java + vue电商系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
程序媛徐师姐6 小时前
Java基于SpringBoot的智能城市管理平台,附源码+文档说明
java·spring boot·java springboot·智能城市管理平台·java智能城市管理平台·java智能城市管理·智能城市管理
李慕婉学姐6 小时前
【开题答辩过程】以《婴幼儿辅食健康监测与反馈系统》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot
默 语6 小时前
Spring Boot 3.x升级踩坑记:到底值不值得升级?
hive·spring boot·后端