RabbitMQ如何创建缓存

  1. CachingConnectionFactory(缓存工厂类)

    • 缓存模式:

      	public enum CacheMode {	
      		CHANNEL,CONNECTION;
      	}
      spring:
        rabbitmq:
          cache:
            connection:
            // 缓存模式: connection channel
              mode: connection
              size: 10
            channel:
              checkout-timeout: 10000
              size: 100
      
      
  • 创建连接:

    @Override
    	public final Connection createConnection() throws AmqpException {
    		if (this.stopped) {
    			throw new AmqpApplicationContextClosedException(
    					"The ApplicationContext is closed and the ConnectionFactory can no longer create connections.");
    		}
    		//上锁,防止线程不安全
    		synchronized (this.connectionMonitor) {
    			if (this.cacheMode == CacheMode.CHANNEL) {
    				if (this.connection.target == null) {
    					this.connection.target = super.createBareConnection();
    					// invoke the listener *after* this.connection is assigned
    					if (!this.checkoutPermits.containsKey(this.connection)) {
    						this.checkoutPermits.put(this.connection, new Semaphore(this.channelCacheSize));
    					}
    					this.connection.closeNotified.set(false);
    					getConnectionListener().onCreate(this.connection);
    				}
    				//返回ChannelCachingConnectionProxy
    				return this.connection;
    			}
    			else if (this.cacheMode == CacheMode.CONNECTION) {
    				return connectionFromCache();
    			}
    		}
    		return null; // NOSONAR - never reach here - exceptions
    	}
    
    • 创建channel:

      // createConnection()方法中获取到的connection	
      private Channel getChannel(ChannelCachingConnectionProxy connection, boolean transactional) {
          // 信号量
      		Semaphore permits = null;
      		if (this.channelCheckoutTimeout > 0) {
      			permits = obtainPermits(connection);
      		}
          // 获取通道集合
      		LinkedList<ChannelProxy> channelList = determineChannelList(connection, transactional);
      		ChannelProxy channel = null;
      		if (connection.isOpen()) {
                  //查询可以用的channel
      			channel = findOpenChannel(channelList, channel);
      			if (channel != null && logger.isTraceEnabled()) {
      				logger.trace("Found cached Rabbit Channel: " + channel.toString());
      			}
      		}
      		if (channel == null) {
      			try {
      				channel = getCachedChannelProxy(connection, channelList, transactional);
      			}
      			catch (RuntimeException e) {
      				if (permits != null) {
      					permits.release();
      					if (logger.isDebugEnabled()) {
      						logger.debug("Could not get channel; released permit for " + connection + ", remaining:"
      								+ permits.availablePermits());
      					}
      				}
      				throw e;
      			}
      		}
      		return channel;
      	}
      
      //获取许可证
      	private Semaphore obtainPermits(ChannelCachingConnectionProxy connection) {
      		Semaphore permits;
      		permits = this.checkoutPermits.get(connection);
      		if (permits != null) {
      			try {
      				if (!permits.tryAcquire(this.channelCheckoutTimeout, TimeUnit.MILLISECONDS)) {
      					throw new AmqpTimeoutException("No available channels");
      				}
      				if (logger.isDebugEnabled()) {
      					logger.debug(
      							"Acquired permit for " + connection + ", remaining:" + permits.availablePermits());
      				}
      			}
      			catch (InterruptedException e) {
      				Thread.currentThread().interrupt();
      				throw new AmqpTimeoutException("Interrupted while acquiring a channel", e);
      			}
      		}
      		else {
      			throw new IllegalStateException("No permits map entry for " + connection);
      		}
      		return permits;
      	}
      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值