RabbitTemplte源码解析

#### 发送信息总流程
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
         
  1. 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);
    		}
    	}
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值