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
相关推荐
qq_5432485210 分钟前
Tomcat服务部署
java·tomcat
Auc2427 分钟前
OJ判题系统第4期之判题机模块架构——设计思路、实现步骤、代码实现(工厂模式、代理模式的实践)
java·spring cloud·log4j·mybatis·代理模式·工厂模式
熊大如如30 分钟前
Java NIO 文件处理接口
java·linux·nio
有梦想的攻城狮1 小时前
spring中的@Lazy注解详解
java·后端·spring
愿你天黑有灯下雨有伞1 小时前
Spring Boot集成RabbitMQ高级篇:可靠性与性能提升
spring boot·rabbitmq·java-rabbitmq
什码情况2 小时前
星际篮球争霸赛/MVP争夺战 - 华为OD机试真题(A卷、Java题解)
java·数据结构·算法·华为od·面试·机试
AA-代码批发V哥2 小时前
正则表达式: 从基础到进阶的语法指南
java·开发语言·javascript·python·正则表达式
字节高级特工2 小时前
【C++】”如虎添翼“:模板初阶
java·c语言·前端·javascript·c++·学习·算法
晴天下小雨o2 小时前
排序算法总结
java·算法·排序算法
曼岛_3 小时前
[Java实战]Spring Boot 整合 Redis(十八)
java·spring boot·redis