导读:不想细看就直接看红色部分,我最近会绘制图与仔细说哈自己的感受与MINA流程,欢迎探讨一起进步
刚刚读完MINA源码,解决了过去的困惑,但是多了一些新的困惑!
希望有研究的人指导探讨!
SocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setReuseAddress( true );
acceptor.setHandler(new EchoProtocolHandler());
acceptor.bind(new InetSocketAddress(PORT));
SocketAcceptor acceptor = new NioSocketAcceptor();
此行代码 通过NioSocketAcceptor构造器 构造了SimpleIoProcessorPool实例包含几个NioProcessor实例NioProcessor实例包含同一个Executor实例
acceptor.bind(new InetSocketAddress(PORT));
这样服务器接到请求,就从SimpleIoProcessorPool选择一个NioProcessor实例处理数据【当然是其包含的Executor实力构造内部类Runable接口执行】
是从AbstractPollingIoAcceptor类startupAcceptor()方法
private void startupAcceptor() {
// If the acceptor is not ready, clear the queues
// TODO : they should already be clean : do we have to do that ?
if (!selectable) {
registerQueue.clear();
cancelQueue.clear();
}
// start the acceptor if not already started
synchronized (lock) {
if (acceptor == null) {
acceptor = new Acceptor();
executeWorker(acceptor);
}
}
}
private class Acceptor implements Runnable {
public void run() {
int nHandles = 0;
while (selectable) {
try {
int selected = select();
nHandles += registerHandles();
if (selected > 0) {
processHandles(selectedHandles());
}
nHandles -= unregisterHandles();
if (nHandles == 0) {
synchronized (lock) {
if (registerQueue.isEmpty()
&& cancelQueue.isEmpty()) {
acceptor = null;
break;
}
}
}
} catch (Throwable e) {
ExceptionMonitor.getInstance().exceptionCaught(e);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
ExceptionMonitor.getInstance().exceptionCaught(e1);
}
}
}
// Cleanup all the processors, and shutdown the acceptor.
if (selectable && isDisposing()) {
selectable = false;
try {
if (createdProcessor) {
processor.dispose();
}
} finally {
try {
synchronized (disposalLock) {
if (isDisposing()) {
destroy();
}
}
} catch (Exception e) {
ExceptionMonitor.getInstance().exceptionCaught(e);
} finally {
disposalFuture.setDone();
}
}
}
}
processHandles(selectedHandles());
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);
System.out.print("bobo-->"+session.getProcessor().getClass());
// add the session to the SocketIoProcessor
session.getProcessor().add(session);
}
}
}
T session = accept(processor, handle);
session.getProcessor().add(session);
session.getProcessor()实例是
SimpleIoProcessorPool对象
public final void add(T session) {
getProcessor(session).add(session);
}
getProcessor(session)实例是NioProcessor他的父类AbstractPollingIoProcessor
public final void add(T session) {
if (isDisposing()) {
throw new IllegalStateException("Already disposed.");
}
// Adds the session to the newSession queue and starts the worker
newSessions.add(session);
startupProcessor();
}
private void startupProcessor() {
synchronized (lock) {
if (processor == null) {
processor = new Processor();
executor.execute(new NamePreservingRunnable(processor, threadName));
}
}
// Just stop the select() and start it again, so that the processor
// can be activated immediately.
wakeup();
}
就这样运行