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
相关推荐
咖啡教室2 小时前
java日常开发笔记和开发问题记录
java
咖啡教室2 小时前
java练习项目记录笔记
java
鱼樱前端3 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea3 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea3 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
李少兄5 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝5 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖5 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信
s9123601015 小时前
rust 同时处理多个异步任务
java·数据库·rust
9号达人5 小时前
java9新特性详解与实践
java·后端·面试