RabbitMQ消费自动重试,消费ACK,逻辑异常补漏

本文介绍了如何在SpringBoot项目中添加RabbitMQ依赖,配置连接参数,以及创建一个消费者实例处理消息。内容包括YML文件中的配置项和处理消息时的错误处理策略。
摘要由CSDN通过智能技术生成

1、添加依赖

 <!--rabbitMq 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2、yml配置

spring:
  rabbitMq:
    host: 10.106.11.37
    port: 5672
    username: admin
    password: 123456
    listener:
      simple:
        #消息采取手动应答ACK
        acknowledge-mode: manual
        #消息被拒后(即未消费),重新(true)放入队列
        default-requeue-rejected: true
        #并发数 
        concurrency: 2
        #最大并发数
        max-concurrency: 10
        #消费重试
        retry:
          #开启消费者出现异常情况下,进行重试消费,默认false
          enabled: true
          #最大重试次数,默认为3
          max-attempts: 5
          #重试间隔时间,默认1000(单位毫秒)
          initial-interval: 3000
      direct:
        #消息开启手动确认
        acknowledge-mode: manual

3、示例代码

 @RabbitListener(queues = Constants.SALE_GIFT_ORDER_METADATA_QUEUE)
    public void execute(Message message, Channel channel) {
        String orderType = OrderCommonConstants.ORDER_TYPE_LH;

        //Step 1.获取消费数据
        if (null == message || null == message.getBody()) {
            log.info("接收数据为空......");
            return;
        }
        String msgBody = new String(message.getBody(), StandardCharsets.UTF_8);

        try {
            log.info("收到数据:{}", msgBody);
            Map<String, Object> msgBodyMap = JSON.parseObject(msgBody);
            //版本号
            if (Objects.isNull(msgBodyMap.get("version"))) {
                throw new RuntimeException("版本号参数不能为空");
            }
            Long currentVersion = Long.valueOf(msgBodyMap.get("version").toString());
            //主表
            JSONObject jsonObject = (JSONObject) msgBodyMap.get("businessData");
            //
            Object obj= JSONObject.parseObject(JSON.toJSONString(jsonObject), Object.class);
            if (Objects.isNull(obj)) {
                throw new RuntimeException("实体为Null");
            }
           

            //Step 2.保存数据
            int res =  save(obj);
            if (res ==  OrderCommonConstants.ADD_LOCK_FAIL) {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            } else {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }
        } catch (JSONException e){
            log.error("消息格式异常,收到消息:{}, 错误信息:", msgBody, e);
            //记录消费异常日志
           	//。。。。。保存到数据库
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                log.error("消息消费失败,消息格式异常");
            } catch (IOException ioException) {
                log.error("消息消费失败,消息格式异常,异常信息", ioException);
            }
        } catch (Exception e){
            log.error("消息异常,收到消息:{}, 错误信息:", msgBody, e);
            //记录消费异常日志
            //。。。。。保存到数据库
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
                log.error("消息消费失败,重新放回队列");
            } catch (IOException ioException) {
                log.error("消息消费失败,重新放回队列,消息体{},异常信息", msgBody, ioException);
            }
        }
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值