mina一些理解

男人总是寂寞的,尤其是周末的晚上!
今夜寂寞难耐下了mina源码,写了写mina的小程序,顺便把源码也设置进去,写完后就开始ctrl+t,ctrl+左机,看看这个mina是怎么回事


5个接口:
1.IoConnector  理解成客户端好了
2.IoAcceptor   服务器端
3.IoSession    链接实例
4.IoHandler    业务处理
5.IoFilter     过滤器,悬接通讯层与业务层


先说服务端NioSocketAcceptor,声明为final,继承自AbstractPollingIoAccptor,并实现SocketAcceptor,是mina实现通信的实现类之一,还有DatagramAccepter,VmPipeAccepter.

mina2.0的线程池,我只看了NioSocketAccptor这块,
public NioSocketAcceptor(int processorCount) {
    super(new DefaultSocketSessionConfig(), NioProcessor.class, processorCount);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
    }


调用父类的构造方法,将要设置的容量传近去

AbstractPollingIoAccptor再调用自己的私有构造方法
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
            Class<? extends IoProcessor<T>> processorClass, int processorCount) {
        this(sessionConfig, null, new SimpleIoProcessorPool<T>(processorClass,
                processorCount), true);
    }

//调用父类的构造方法

private AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
            Executor executor, IoProcessor<T> processor,
            boolean createdProcessor) {
        super(sessionConfig, executor);

        if (processor == null) {
            throw new NullPointerException("processor");
        }

        this.processor = processor;
        this.createdProcessor = createdProcessor;

        try {
            // Initialize the selector
            init();
            
            // The selector is now ready, we can switch the
            // flag to true so that incoming connection can be accepted
            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);
                }
            }
        }
    }


//AbstractIoAcceptor

    protected AbstractIoAcceptor(IoSessionConfig sessionConfig, Executor executor) {
        super(sessionConfig, executor);
        defaultLocalAddresses.add(null);
    }


//AbstractIoService
    protected AbstractIoService(IoSessionConfig sessionConfig, Executor executor) {
        if (sessionConfig == null) {
            throw new NullPointerException("sessionConfig");
        }

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

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

        // Create the listeners, and add a first listener : a activation listener
        // for this service, which will give information on the service state.
        listeners = new IoServiceListenerSupport(this);
        listeners.add(serviceActivationListener);

        // Stores the given session configuration
        this.sessionConfig = sessionConfig;

        // Make JVM load the exception monitor before some transports
        // change the thread context class loader.
        ExceptionMonitor.getInstance();

        if (executor == null) {
            this.executor = Executors.newCachedThreadPool();
            createdExecutor = true;
        } else {
            this.executor = executor;
            createdExecutor = false;
        }

        threadName = getClass().getSimpleName() + '-' + id.incrementAndGet();
    }


终于明白2.0说的线程池不需要设置的原因。。。。

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