Tomcat 连接器 Connector(Nio)(doing)

———— 9.0.13
words: 182    views:    time: 1min
I/O


….

Nio

bind()

org.apache.tomcat.util.net.NioEndpoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private volatile ServerSocketChannel serverSock = null;

private NioSelectorPool selectorPool = new NioSelectorPool();

@Override
public void bind() throws Exception {
initServerSocket();
setStopLatch(new CountDownLatch(1));

initialiseSsl(); // Initialize SSL if needed
selectorPool.open(getName());
}

protected void initServerSocket() throws Exception {
if (!getUseInheritedChannel()) {
serverSock = ServerSocketChannel.open();
socketProperties.setProperties(serverSock.socket());
InetSocketAddress addr = new InetSocketAddress(getAddress(), getPortWithOffset());
serverSock.socket().bind(addr,getAcceptCount());
} else {
// Retrieve the channel provided by the OS
Channel ic = System.inheritedChannel();
if (ic instanceof ServerSocketChannel) {
serverSock = (ServerSocketChannel) ic;
}
if (serverSock == null) {
throw new IllegalArgumentException(sm.getString("endpoint.init.bind.inherited"));
}
}
serverSock.configureBlocking(true); //mimic APR behavior
}
org.apache.tomcat.util.net.NioSelectorPool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
protected volatile Selector sharedSelector;

protected NioBlockingSelector blockingSelector;

public void open(String name) throws IOException {
enabled = true;
getSharedSelector();
if (shared) {
blockingSelector = new NioBlockingSelector();
blockingSelector.open(name, getSharedSelector());
}
}

protected Selector getSharedSelector() throws IOException {
if (shared && sharedSelector == null) {
synchronized (NioSelectorPool.class) {
if (sharedSelector == null) {
sharedSelector = Selector.open();
}
}
}
return sharedSelector;
}

startInternal()


参考:

  1. https://zhuanlan.zhihu.com/p/376074271