RabbitMQ客户端源码分析(四)之BlockingCell

RabbitMQ-java-client版本

  1. com.rabbitmq:amqp-client:4.3.0
  2. RabbitMQ版本声明: 3.6.15

BlockingCell

  1. BlockingCell,代码文档注释描述为”简单的一次性IPC机制“,其实就是一个Future对象,大多数长连接里异步处理获取响应值都会采用Future模式。

  2. uml关联

BlockingCell源码分析

  1. 完整代码,我们从代码结构来看其实就是一个Future

    /**
     * Simple one-shot IPC mechanism. Essentially a one-place buffer that cannot be emptied once filled.
     * 简单的一次性IPC机制。 基本上是一个缓冲区,一旦填满就不能清空。
     * 从代码上来看其实就是一个Future对象
     */
    public class BlockingCell<T> {
         
        /** Indicator of not-yet-filledness
         *  尚未填充满的标示
         * */
        private boolean _filled = false;
    
        /** Will be null until a value is supplied, and possibly still then. */
        private T _value;
    
        private static final long NANOS_IN_MILLI = 1000 * 1000;
    
        private static final long INFINITY = -1;
    
        /** Instantiate a new BlockingCell waiting for a value of the specified type. */
        public BlockingCell() {
         
            // no special handling required in default constructor
        }
    
        /**
         * Wait for a value, and when one arrives, return it (without clearing it). If there's already a value present, there's no need to wait - the existing value
         * is returned.
         * 一直等待,直到拿到响应数据之后调用set(T newValue),设置值之后调用notifyAll(),此时get()不再阻塞。
         * @return the waited-for value
         *
         * @throws InterruptedException if this thread is interrupted
         */
        public synchronized T get() throws InterruptedException {
         
            while (!_filled) {
         
                wait();
            }
            return _value;
        }
    
        /**
         * Wait for a value, and when one arrives, return it (without clearing it). If there's
         * already a value present, there's no need to wait - the existing value is returned.
         * If timeout is reached and value hasn't arrived, TimeoutException is thrown.
         * 
         * @param timeout timeout in milliseconds. -1 effectively means infinity
         * @return the waited-for value
         * @throws InterruptedException if this thread is interrupted
         */
        public synchronized T get(long timeout) throws InterruptedException, TimeoutException {
         
            //如果设置为-1,则表示一直等待下去
            if (timeout == INFINITY) return get();
    
            if (timeout < 0) {
         
                throw new AssertionError("Timeout cannot be less than zero");
            }
    
            long now = System.nanoTime() / NANOS_IN_MILLI;
            long maxTime = now + timeout;
            while (!_filled && (now = (System.nanoTime() / NANOS_IN_MILLI)) < maxTime) {
         
                wait(maxTime - now);
            }
    
            if (!_filled)
                throw new TimeoutException();
    
            return _value;
        }
    
        /**
         * As get(), but catches and ignores InterruptedException, retrying until a value appears.
         * @return the waited-for value
         */
        public synchronized T uninterruptibleGet() {
         
            boolean wasInterrupted = false;
            try {
         
                while (true) {
         
                    try {
         
                        return get();
                    } catch (InterruptedException ex) {
         
                        // no special handling necessary
                        wasInterrupted = true;
                    }
                }
            } finally {
         
                if (wasInterrupted) {
         
                    Thread.currentThread().interrupt();
                }
            }
        }
    
        /**
         * As get(long timeout), but catches and 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值