RabbitMQ客户端源码分析(八)之NIO

RabbitMQ-java-client版本

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

NioLoopContext

  1. 主要用于NIO事件循环的管理,根据配置参数初始化读写Buffer以及启动事件循环。初始化分析参见RabbitMQ客户端源码分析(二)之Frame与FrameHandler

  2. 构造方法:根据NioParams配置的readByteBufferSizewriteByteBufferSize生成读写创建读写Buffer,默认大小32768

        public class NioLoopContext {
         
        
            private static final Logger LOGGER = LoggerFactory.getLogger(NioLoopContext.class);
        
            private final SocketChannelFrameHandlerFactory socketChannelFrameHandlerFactory;
        
            private final ExecutorService executorService;
        
            private final ThreadFactory threadFactory;
        
            final ByteBuffer readBuffer, writeBuffer;
            
            SelectorHolder readSelectorState;
            SelectorHolder writeSelectorState;
        
            public NioLoopContext(SocketChannelFrameHandlerFactory socketChannelFrameHandlerFactory,
                NioParams nioParams) {
         
                this.socketChannelFrameHandlerFactory = socketChannelFrameHandlerFactory;
                this.executorService = nioParams.getNioExecutor();
                this.threadFactory = nioParams.getThreadFactory();
                this.readBuffer = ByteBuffer.allocate(nioParams.getReadByteBufferSize());
                this.writeBuffer = ByteBuffer.allocate(nioParams.getWriteByteBufferSize());
            }
        
         
          }
    
    
    
  3. 初始化方法initStateIfNecessary():创建读写Selector,启动NIO事件循环

       void initStateIfNecessary() throws IOException {
         
            if (this.readSelectorState == null) {
         
                //Selector.open() 创建一个Selector
                this.readSelectorState = new SelectorHolder(Selector.open());
                this.writeSelectorState = new SelectorHolder(Selector.open());
    
                startIoLoops();
            }
        }
    
        private void startIoLoops() {
         
            if (executorService == null) {
         
                Thread nioThread = Environment.newThread(
                    threadFactory,
                    new NioLoop(socketChannelFrameHandlerFactory.nioParams, this),
                    "rabbitmq-nio"
                );
                nioThread.start();
            } else {
         
                this.executorService.submit(new NioLoop(socketChannelFrameHandlerFactory.nioParams, this));
            }
        }
    
    
    

NioLoop

  1. NioLoop的代码写的相对比较晦涩,而且整体代码的逻辑不够清晰。主要就是对读事件和写事件的处理
        public class NioLoop implements Runnable {
         
        
            private static final Logger LOGGER = LoggerFactory.getLogger(NioLoop.class);
        
            private final NioLoopContext context;
        
            private final NioParams nioParams;
        
            public NioLoop(NioParams nioParams, NioLoopContext loopContext) {
         
                this.nioParams = nioParams;
                this.context = loopContext;
            }
        
            @Override
            public void run() {
         
                final SelectorHolder selectorState = context.readSelectorState;
                final Selector selector = selectorState.selector;
                final Set<SocketChannelRegistration> registrations = selectorState.registrations;
        
                final ByteBuffer buffer = context.readBuffer;
        
                final SelectorHolder writeSelectorState = context
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值