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();
            }
        }

    }

}
相关推荐
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
需要8263 小时前
MySQL 索引 B+ 树与“查询为什么变慢了
java·spring boot·spring·spring cloud·tomcat
一 乐3 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序
vx-程序开发4 小时前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
一 乐4 小时前
养老院管理系统|基于springboot + vue养老院管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
今年下半年7 小时前
SpringBoot整合金蝶中间件
spring boot·中间件·maven
字节探索8 小时前
别再只会写 Controller 了!一文吃透 Spring Boot 高级特性与项目实战
spring boot·spring
MetaLite8 小时前
全网最好的SpringBoot接口请求对象设计
java·spring boot·后端
摇滚侠9 小时前
《Spring Boot 3:高级与架构设计》第 2 章 IOC容器的高级机制 BeanFactoryPostProcessor 个人理解 6
java·spring boot·笔记·后端
xcl092510 小时前
宠物商城社交托运购物系统开发实战:从架构设计到完整实现指南
java·spring boot