#### 发送信息总流程
RabbitTemplate
- convertAndSend()
- execute()
- doExecute() //获取Channel
- doExecute()方法内:invokeAction(action, connectionFactory, channel);
addListener(channel);
publisherConfirmChannels.putIfAbsent(key,this)//a
action.doInRabbit(channel);
- doSend() //发送真正的消息
//1. 添加 beforePublishPostProcessors
setupConfirm(channel, messageToUse, correlationData); //设置确认
CorrelationDataPostProcessor //查看是否存在CorrelationData处理类
channel.getNextPublishSeqNo();// 设置下一个publishSeqNo,并设置MessageProperties中
sendToRabbit()
// 添加消息属性转换器 MessagePropertiesConverter
-
RabbitTemplate如何发送数据
// 执行 @Nullable private <T> T doExecute(ChannelCallback<T> action, ConnectionFactory connectionFactory) { // NOSONAR complexity // 当前的ChannelCallback是否存在,如果不存在。则报错 Assert.notNull(action, "Callback object must not be null"); Channel channel = null; boolean invokeScope = false; // No need to check the thread local if we know that no invokes are in process if (this.activeTemplateCallbacks.get() > 0) { channel = this.dedicatedChannels.get(); } RabbitResourceHolder resourceHolder = null; Connection connection = null; // NOSONAR (close) // 判断当前的channel是否为空 if (channel == null) { // 判断是否添加了事务 if (isChannelTransacted()) { resourceHolder = ConnectionFactoryUtils. getTransactionalResourceHolder(connectionFactory, true, this.usePublisherConnection); channel = resourceHolder.getChannel(); if (channel == null) { ConnectionFactoryUtils.releaseResources(resourceHolder); throw new IllegalStateException("Resource holder returned a null channel"); } } else { //没有事务的逻辑 // 根据连接工厂返回ChannelCachingConnectionProxy connection = ConnectionFactoryUtils.createConnection(connectionFactory, this.usePublisherConnection); // NOSONAR - RabbitUtils closes if (connection == null) { throw new IllegalStateException("Connection factory returned a null connection"); } try { // 根据连接信息创建channel channel = connection.createChannel(false); if (channel == null) { throw new IllegalStateException("Connection returned a null channel"); } } catch (RuntimeException e) { RabbitUtils.closeConnection(connection); throw e; } } } else { invokeScope = true; } try { return invokeAction(action, connectionFactory, channel); } catch (Exception ex) { if (isChannelLocallyTransacted(channel)) { resourceHolder.rollbackAll(); } throw convertRabbitAccessException(ex); } finally { cleanUpAfterAction(channel, invokeScope, resourceHolder, connection); } }