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
相关推荐
Aqua Cheng.1 分钟前
代码随想录第七天|哈希表part02--454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和
java·数据结构·算法·散列表
Nebula_g2 分钟前
Java哈希表入门详解(Hash)
java·开发语言·学习·算法·哈希算法·初学者
努力努力再努力wz4 分钟前
【C++进阶系列】:万字详解unordered_set和unordered_map,带你手搓一个哈希表!(附模拟实现unordered_set和unordered_map的源码)
java·linux·开发语言·数据结构·数据库·c++·散列表
懂得节能嘛.22 分钟前
【设计模式】Java规则树重构复杂业务逻辑
java·开发语言·设计模式
自由的疯39 分钟前
Java Docker部署RuoYi框架的jar包
java·后端·架构
薛家明43 分钟前
C#转java的最好利器easy-query就是efcore4j sqlsugar4j freesql4j
java·orm·easy-query·sqlsugar-java
自由的疯1 小时前
Java Docker本地部署Java服务
java·后端·架构
paishishaba1 小时前
JAVA面试复习笔记(待完善)
java·笔记·后端·面试
四谎真好看2 小时前
Java 黑马程序员学习笔记(进阶篇19)
java·笔记·学习·学习笔记