Mina运行流程解析(二)

一、NioSOcketConnector的初始化都干了什么。

     
           //初始化客户端的连接类
           IoConnector connector = new NioSocketConnector();
           
    
public NioSocketConnector() {
        //继续看父类
        super(new DefaultSocketSessionConfig(), NioProcessor.class);
        ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
//父类AbstractPollingIoConnector的构造方法做了2两件事。1、调用重载的构造方法,2、生成IoProcessorPool的线程池。
protected AbstractPollingIoConnector(IoSessionConfig sessionConfig, Class<? extends IoProcessor<t>> processorClass) {
        this(sessionConfig, null, new SimpleIoProcessorPool<t>(processorClass), true);
}
 
//重载的构造方法
private AbstractPollingIoConnector(IoSessionConfig sessionConfig, Executor executor, IoProcessor<t> processor,
            boolean createdProcessor) {
        //继续调用父类构造
        super(sessionConfig, executor);


        if (processor == null) {
            throw new IllegalArgumentException("processor");
        }
        //设置processor,processor用于操作IoSession
        this.processor = processor;
        this.createdProcessor = createdProcessor;


        try {
            //NIO的第一步!Selector.open();
            init();
            selectable = true;
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeIoException("Failed to initialize.", e);
        } finally {
            if (!selectable) {
                try {
                    destroy();
                } catch (Exception e) {
                    ExceptionMonitor.getInstance().exceptionCaught(e);
                }
            }
        }
    }
 
//祖父类AbstractIoService
protected AbstractIoService(IoSessionConfig sessionConfig, Executor executor) {
        if (sessionConfig == null) {
            throw new IllegalArgumentException("sessionConfig");
        }


        if (getTransportMetadata() == null) {
            throw new IllegalArgumentException("TransportMetadata");
        }


        if (!getTransportMetadata().getSessionConfigType().isAssignableFrom(sessionConfig.getClass())) {
            throw new IllegalArgumentException("sessionConfig type: " + sessionConfig.getClass() + " (expected: "
                    + getTransportMetadata().getSessionConfigType() + ")");
        }


        //生成IoServiceListenerSupport对象,该对象为IoServiceListener的辅助类,提供对IoServiceListener的增加和移除以及触发事件的操作
        listeners = new IoServiceListenerSupport(this);
        //添加监听对象,用于监听service的激活。
        listeners.add(serviceActivationListener);


        // 设置sessionConfig为DefaultSessionConfig
        this.sessionConfig = sessionConfig;


        ExceptionMonitor.getInstance();
        //初始化线程池
        if (executor == null) {
            this.executor = Executors.newCachedThreadPool();
            createdExecutor = true;
        } else {
            this.executor = executor;
            createdExecutor = false;
        }


        threadName = getClass().getSimpleName() + '-' + id.incrementAndGet();
    }
</t>
总结一下步骤1都干了什么:初始化SessionConfig,初始化IoProcessor的线程池,进行了Selector.open()操作,IoServiceListener对IoService的激活进行监听,初始化了I/O事件的线程池。具体初始化的东西后面用到,会详细讲解。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值