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

    }

}
相关推荐
需要82618 分钟前
分布式事务:Seata AT/TCC/SAGA 的取舍与踩坑
java·spring boot·分布式·spring·spring cloud
m0_587383001 小时前
深圳24小时自助健身房解决方案实战:从系统架构到部署指南
java·人工智能·spring boot·spring·系统架构·需求分析
小马哥程序开发3 小时前
[点赞收藏免费领取 · 项目源码]57105基于Spring Boot的充电桩管理系统的设计与实现
java·spring boot·源码·课程设计·毕设·大作业·课设
卓怡学长5 小时前
w202springboot基于B_S架构的图书信息管理系统
java·spring boot·spring·intellij-idea
走到天涯海角5 小时前
用springBoot写几个简单的接口
java·spring boot·intellij-idea
xcl09256 小时前
西安同城货运系统源码开发实战 核心功能实现与部署全指南
java·spring boot
灯澜忆梦6 小时前
【RabbitMQ #4】 | Work 任务模型
分布式·rabbitmq
ym hyd 1116 小时前
慢性病精细化管理平台源码 Java+SpringBoot+Vue3 前后分离
java·vue.js·spring boot·毕设
RuoyiOffice6 小时前
SpringBoot3+Vue3 低代码业务表单:从拖拽设计、独立建表到审批与数据查询
spring boot·vue3·数据建模·flowable·版本管理·低代码表单·ruoyi office
需要8267 小时前
MySQL MVCC 与事务隔离级别:从一条 update 看版本链
java·数据库·spring boot·mysql·spring cloud