RabbitMQ: return与confirm 机制整合 spirngboot实现

java 复制代码
package com.qf.mq2203.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class RabbitmqConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        // 设置 return callback
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {

            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {

                System.out.println(Thread.currentThread().getName() + "================");
                final byte[] body = message.getBody(); // 消息的内容
                System.out.println(message);
                System.out.println(replyCode);
                System.out.println(replyText);
                System.out.println(exchange);
                System.out.println(routingKey);

                System.out.println(Thread.currentThread().getName() + "================");

            }
        });

        // 设置 confirm回调方法
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println(Thread.currentThread().getName() + "----------------");

                System.out.println(correlationData);
                System.out.println(ack);
                System.out.println(cause);

                System.out.println(Thread.currentThread().getName() + "----------------");
            }
        });
    }


    @Bean
    public TopicExchange topicExchange() {
        final TopicExchange topicExchange = new TopicExchange("boot-topic-exchange", true, false);
        return topicExchange;
    }

    @Bean
    public Queue queue() {
        final Queue queue = new Queue("boot-queue");
        return queue;
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange topicExchange) {

        String routingkey = "*.red.*";
        final Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(routingkey);
        return binding;

    }

}

yml

XML 复制代码
spring:
  rabbitmq:
    host: 39.98.95.55
    port: 5672
    virtual-host: /test
    username: test
    password: test
    listener:
      simple:
        prefetch: 1
        acknowledge-mode: manual
    publisher-returns: true  # 开启mq的return机制
    publisher-confirm-type: correlated
相关推荐
Daniel 大东34 分钟前
BugJson因为json格式问题OOM怎么办
java·安全
Theodore_10225 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸6 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象6 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了7 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·7 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic7 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王7 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康7 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神8 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式