ioProcessor:
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
Class<? extends IoProcessor<T>> processorClass) {
this(sessionConfig, null, new SimpleIoProcessorPool<T>(processorClass),
true);
}
这里其实是一个组合模式,SimpleIoProcessorPool和NioProcessor都实现了 Processor接口,一个是组合形成的Processor池,而另一个是单独的类。调用的 SimpleIoProcessorPool的构造函数是这样:
private static final int DEFAULT_SIZE = Runtime.getRuntime().availableProcessors() + 1;
public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType) {
this(processorType, null, DEFAULT_SIZE);
}
可以看到,默认的池大小是cpu个数+1,也就是创建了cpu+1个的Selector对 象。它的重载构造函数里是创建了一个数组,启动一个 CachedThreadPool来运 行NioProcessor,通过反射创建具体的Processor对象,这里就不再列出了。
Mina当有一个新连接建立的时候,就创建一个NioSocketSession,并且传入 上面的SimpleIoProcessorPool,当连接初始化的时候将Session加入 SimpleIoProcessorPool:
protected NioSession accept(IoProcessor<NioSession> processor,
ServerSocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor (selector);
if ((key == null) || (!key.isValid()) || (! key.isAcceptable()) ) {
return null;
}
// accept the connection from the client
SocketChannel ch = handle.accept();
if (ch == null) {
return null;
}
return new NioSocketSession(this, processor, ch);
}
private void processHandles(Iterator<H> handles) throws Exception {
while (handles.hasNext()) {
H handle = handles.next();
handles.remove();
// Associates a new created connection to a processor,
// and get back a session
T session = accept(processor, handle);
if (session == null) {
break;
}
initSession(session, null, null);
// add the session to the SocketIoProcessor
session.getProcessor().add (session);
}
}
nio崇尺嶄議謹倖Selector潤更(3)
扮寂:2010-12-19 BlogJava dennis
紗秘議荷恬頁弓奐匯倖屁侏延楚旺拝庁方怏寄弌朔斤哘議NioProcessor廣過 欺session戦?
private IoProcessor<T> nextProcessor() {
checkDisposal();
return pool[Math.
|