1:手动构建RabbitmqQueueExchangeAutoConfiguration
该类用于初始化 queue exchange 并进行Binding绑定
package com.fc.rabbitmq_demo.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static com.fc.rabbitmq_demo.config.CommonConstant.*;
/**
*初始化 queue exchange 并进行Binding绑定
* @author fanyuan
*/
@Configuration
public class RabbitmqQueueExchangeAutoConfiguration {
/**
* 默认有四种bean 对于rabbitmq四种交换器
* DirectExchange: 直连型 和queue一一对应
* FanoutExchange: 广播型 广播exchange下多有queue
* TopicExchange: 通过routeKey和 Binding Key 进行模糊匹配 进行路由发送queue
* HeadersExchange:而根据消息中的 Headers 和创建绑定关系时指定的 Arguments 来匹配决定路由到哪些 Queue基本不用
*/
/*
* 设置交换器 exchange
*/
@Bean
public Exchange directExchange(){
return new DirectExchange(exchange_direct);
}
@Bean
public Exchange fanoutExchange(){
return new FanoutExchange(exchange_fanout);
}
@Bean
public Exchange topicExchange(){
return new TopicExchange(exchange_topic);
}
/**
* Queue创建有4个属性
* @param name 队列名称
* @param durable 设置是否为持久性队列 如果为持久队列,则为true(该队列将在服务器重新启动后继续有效,否则重启后失效) 默认为true
* @param exclusive 设置是否为独占队列(该队列将仅由声明者的连接使用) 如果声明独占队列,则为true 默认为false
* @param autoDelete 设置是否自动删除 当该队列不再使用了是否自动删除
*/
/*创建queue*/
/**
* 声明创建一个queue
* @return
*/
@Bean
public Queue directQueue1(){
return new Queue(queue_direct1,true,false,false);
}
@Bean
public Queue directQueue2(){
return new Queue(queue_direct2,true,false,false);
}
@Bean
public Queue fanoutQueue1(){
return new Queue(queue_fanoutQueue1,true,false,false);
}
@Bean
public Queue fanoutQueue2(){
return new Queue(queue_fanoutQueue2,true,false,false);
}
@Bean
public Queue topicQueue1(){
return new Queue(queue_topic1,true,false,false);
}
@Bean
public Queue topicQueue2(){
return new Queue(queue_topic2,true,false,false);
}
/* 将exchange与topic进行绑定*/
@Bean
public Binding directBinding1(){
return BindingBuilder.bind(directQueue1()).to(directExchange()).with(routingKey_direct1).noargs();
}
@Bean
public Binding directBinding2(){
return BindingBuilder.bind(directQueue2()).to(directExchange()).with(routingKey_direct2).noargs();
}
@Bean
public Binding fanoutBinding1(){
return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange()).with("").noargs();
}
@Bean
public Binding fanoutBinding2(){
return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange()).with("").noargs();
}
@Bean
public Binding topicBinding1(){
return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with(routingKey_topic1).noargs();
}
@Bean
public Binding topicBinding2(){
return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(routingKey_topic2).noargs();
}
}
在项目中将需要创建的exchange和queue进行 binding完成之后 ,此时我们去构建生产者相关配置,
2:RabbitmqProducerAutoConfiguration
该类用于对生产者进行配置,包含对公共的异步ack消息处理
/**
* 对生产者进行设置
* @author fangyuan
*/
@Configuration
@Slf4j
public class RabbitmqProducerAutoConfiguration {
/**
*
* 若需要 使用到returnCallback 与confirmCallback 生产者确认机制
* 其中confirmCallback:消息从生产者到达exchange时返回ack,消息未到达exchange返回nack(ack为false);
* returnCallBack:消息进入exchange但未进入queue时会被调用 这个时候说明路由失败了 需要进行 设置
* 需要在配置中使用以下配置:
* publisher-returns: true
* publisher-confirm-type: correlated
*
*/
/**
* 设置生产者的生产消息的ack信息回调(公共处理)
* @return
*/
@Bean
public RabbitTemplate.ConfirmCallback confirmCallback(){
//返回
return (correlationData, ack, cause)->{
//我们可以通过correlationData原始数据 来对消息进行后续处理,当时这是有个要求在于发送必须使用CorrelationData类
//成功
if(ack){
log.info("消息发送成功!!!!!,消息data:{},时间:{}",correlationData,System.currentTimeMillis());
}else {
log.error("消息发送失败!!!!,原因是:{}",cause);
}
};
}
/**
* 发送者失败通知
*/
@Bean
public RabbitTemplate.ReturnCallback returnCallback(){
//构建一个
return (Message message, int re