private Channel getChannel(ChannelCachingConnectionProxy connection, boolean transactional) { if (this.channelCheckoutTimeout > 0) { Semaphore checkoutPermits = this.checkoutPermits.get(connection); if (checkoutPermits != null) { try { if (!checkoutPermits.tryAcquire(this.channelCheckoutTimeout, TimeUnit.MILLISECONDS)) { throw new AmqpTimeoutException("No available channels"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AmqpTimeoutException("Interrupted while acquiring a channel", e); } } } LinkedList<ChannelProxy> channelList; if (this.cacheMode == CacheMode.CHANNEL) { channelList = transactional ? this.cachedChannelsTransactional : this.cachedChannelsNonTransactional; } else { channelList = transactional ? this.openConnectionTransactionalChannels.get(connection) : this.openConnectionNonTransactionalChannels.get(connection); } if (channelList == null) { channelList = new LinkedList<ChannelProxy>(); if (transactional) { this.openConnectionTransactionalChannels.put(connection, channelList); } else { this.openConnectionNonTransactionalChannels.put(connection, channelList); } } ChannelProxy channel = null; if (connection.isOpen()) { synchronized (channelList) { while (!channelList.isEmpty()) { channel = channelList.removeFirst(); if (logger.isTraceEnabled()) { logger.trace(channel + " retrieved from cache"); } if (channel.isOpen()) { break; } else { try { Channel target = channel.getTargetChannel(); if (target != null) { target.close(); // to remove it from auto-recovery if so configured } } catch (AlreadyClosedException e) { if (logger.isTraceEnabled()) { logger.trace(channel + " is already closed"); } } catch (IOException e) { if (logger.isDebugEnabled()) { logger.debug("Unexpected Exception closing channel " + e.getMessage()); } } catch (TimeoutException e) { if (logger.isWarnEnabled()) { logger.warn("TimeoutException closing channel " + e.getMessage()); } } channel = null; } } } if (channel != null) { if (logger.isTraceEnabled()) { logger.trace("Found cached Rabbit Channel: " + channel.toString()); } } } if (channel == null) { channel = getCachedChannelProxy(connection, channelList, transactional); } return channel; }