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
相关推荐
不惑_16 分钟前
缓存技巧 · Spring Cache && Caffeine 高性能缓存库
java·spring·缓存
六月的雨__1 小时前
自驾游拼团系统小程序的设计
java·sql·学习·小程序
Tinalee-电商API接口呀1 小时前
【经验分享】电商api接口——各类商品数据一键获取
java·大数据·开发语言·python·json·产品运营·html5
2401_857638031 小时前
Spring Boot框架在高校心理辅导中的实践
java·spring boot·后端
BUG指挥官1 小时前
7种限流算法打开新方式
java·redis·算法·spring cloud
我不会起名字呀2 小时前
c/c++八股文
java·c语言·c++
户伟伟2 小时前
设计模式六大原则:面向对象设计的核心
java·后端·设计模式
神的孩子都在歌唱2 小时前
个人导航网站介绍和部署
java·开发语言·开源软件
拉丁解牛说技术2 小时前
JAVA并发编程系列(11)线程池底层原理架构剖析
java·面试
ChinaRainbowSea2 小时前
十五,Spring Boot 整合连接数据库(详细配置)
java·数据库·spring boot·后端·spring·web