Mina客户端连接-2

阅读更多
   private NioSocketConnector connector = new NioSocketConnector();


  ConnectFuture future1 = connector.connect(address);


AbstractIoConnector 中 
public final ConnectFuture connect(SocketAddress remoteAddress,
            SocketAddress localAddress, IoSessionInitializer sessionInitializer) 方法

到 AbstractPollingIoConnector

protected final ConnectFuture connect0(
            SocketAddress remoteAddress, SocketAddress localAddress,
            IoSessionInitializer sessionInitializer)

方法

 protected final ConnectFuture connect0(
            SocketAddress remoteAddress, SocketAddress localAddress,
            IoSessionInitializer sessionInitializer) {
        H handle = null;
        boolean success = false;
        try {
            //获取管道
            handle = newHandle(localAddress);
            //建立连接
            if (connect(handle, remoteAddress)) {
                ConnectFuture future = new DefaultConnectFuture();
                T session = newSession(processor, handle);
                initSession(session, future, sessionInitializer);
                // Forward the remaining process to the IoProcessor.
                session.getProcessor().add(session);
                success = true;
                return future;
            }

            success = true;
        } catch (Exception e) {
            return DefaultConnectFuture.newFailedFuture(e);
        } finally {
            if (!success && handle != null) {
                try {
                    close(handle);
                } catch (Exception e) {
                    ExceptionMonitor.getInstance().exceptionCaught(e);
                }
            }
        }

        ConnectionRequest request = new ConnectionRequest(handle, sessionInitializer);
        connectQueue.add(request);
        //建立连接
        startupWorker();
        wakeup();

        return request;
    }


startupWorker 中 启动一个 Connector 连接 服务器

你可能感兴趣的:(Mina,Java)