apache mina 基础类分析(三):AbstractIoConnector

AbstractIoConnector:这个类继承了AbstractIoService,实现了IoConnector接口,AbstractIoService已经介绍过了,在这个类中,主要实现了一个connect()(以及重载)方法:
public final ConnectFuture connect(SocketAddress remoteAddress, SocketAddress localAddress,
            IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
        if (isDisposing()) {
            throw new IllegalStateException("The connector has been disposed.");
        }


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


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


        if (localAddress != null && !getTransportMetadata().getAddressType().isAssignableFrom(localAddress.getClass())) {
            throw new IllegalArgumentException("localAddress type: " + localAddress.getClass() + " (expected: "
                    + getTransportMetadata().getAddressType() + ")");
        }


        if (getHandler() == null) {
            if (getSessionConfig().isUseReadOperation()) {
                setHandler(new IoHandler() {
                    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
                        // Empty handler
                    }


                    public void messageReceived(IoSession session, Object message) throws Exception {
                        // Empty handler
                    }


                    public void messageSent(IoSession session, Object message) throws Exception {
                        // Empty handler
                    }


                    public void sessionClosed(IoSession session) throws Exception {
                        // Empty handler
                    }


                    public void sessionCreated(IoSession session) throws Exception {
                        // Empty handler
                    }


                    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
                        // Empty handler
                    }


                    public void sessionOpened(IoSession session) throws Exception {
                        // Empty handler
                    }
                });
            } else {
                throw new IllegalStateException("handler is not set.");
            }
        }


        return connect0(remoteAddress, localAddress, sessionInitializer);
    }
1.这个方法返回一个ConnectFuture对象,也就是说,调用这个方法后,会阻塞线程,在if(getHandler() == null)中,如果handler是空的,则默认实现一个空的handler,那也就不难理解,在开发时,在开发时,必须要自定义一个实现了IoHandler的类。
  这个类需要实现IoHandler的七个方法,再仔细看看这个接口中的几个方法,也就不难得出,这个IoHandler是一个顶层的阻塞类,等待接收消息。这个IoHandler接口跟android中的Handler接口意义上差不多。
2.接着在这个类中调用了connect0方法,这个方法在这个类中是个抽象类,由其子类实现,下一篇将有这个类的分析


总结:这个类是一个实现接收消息阻塞功能的定义类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值