`
pan_java
  • 浏览: 280151 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Mina服务器端启动 -- 1

阅读更多
  SocketAcceptor acceptor = new NioSocketAcceptor();


 public NioSocketAcceptor() {
        
    	
    	super(new DefaultSocketSessionConfig(), NioProcessor.class);
        
        //初始化前面设置在AbstractIoService 中的  sessionConfig
        ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
 }


  protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
            Class<? extends IoProcessor<T>> processorClass) {
    	// new SimpleIoProcessorPool 新建 processor -- 处理线程
        this(sessionConfig, null, new SimpleIoProcessorPool<T>(processorClass),
                true);
}



//绑定端口  
acceptor.bind(new InetSocketAddress(PORT));


AbstractIoAcceptor 的 public final void bind(Iterable<? extends SocketAddress> localAddresses) 方法中

boundAddresses.addAll(bindInternal(localAddressesCopy));

    protected final Set<SocketAddress> bindInternal(
            List<? extends SocketAddress> localAddresses) throws Exception {
        // Create a bind request as a Future operation. When the selector
        // have handled the registration, it will signal this future.
        AcceptorOperationFuture request = new AcceptorOperationFuture(
                localAddresses);

        // adds the Registration request to the queue for the Workers
        // to handle
        //添加请求到注册列表
        registerQueue.add(request);

        // creates the Acceptor instance and has the local
        // executor kick it off.
        //开始用线程监听通道
        startupAcceptor();
        
        // As we just started the acceptor, we have to unblock the select()
        // in order to process the bind request we just have added to the 
        // registerQueue.
        wakeup();
        
        // Now, we wait until this request is completed.
        request.awaitUninterruptibly();

        if (request.getException() != null) {
            throw request.getException();
        }

        // Update the local addresses.
        // setLocalAddresses() shouldn't be called from the worker thread
        // because of deadlock.
        Set<SocketAddress> newLocalAddresses = new HashSet<SocketAddress>();
        
        for (H handle:boundHandles.values()) {
            newLocalAddresses.add(localAddress(handle));
        }

        return newLocalAddresses;
    }


startupAcceptor 中启动了一个 Acceptor 启动Socket服务




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics