netty is packaged in nio basis.
We get some use cases.
Code:
We look at these two source source
Our analysis focused on two words:
Execution break point at our:
In fact, this is the thread we look at the group:
We look at this sentence:
Go chase:
Here it was initialized.
Here to get the maximum value.
We catch up again down here:
This is the beginning of.
We look dependencies:
Then we look at the real type:
We look inheritance:
Further down:
Plus the listener.
-------------------
And then the next breakpoint:
Enter this Group
We look at what time channel is created.
channelFactory reflection to create NioSocketChannel.
We look bind method:
Chase:
At this time, the channel is NioServerSocketChannel.
--------------------------------92-------------------------------------------
We focus facie this method:
parameter:
Creating actuator array specified number of threads.
Initialize the thread
Add a closed listener thread pool of each single embodiment.
Speak all singleton thread pool to set to go.
---
We look at a piece of code:
Inside there are many attributes.
Parameter is how management?
What options are?
--------------------------------93-------------------------------------------
Focus, we look at how to do is to bind to port?
It has two core ways:
with
We look at doBind method, the return is the result of an asynchronous execution.
final ChannelFuture initAndRegister() {
Channel channel = null;
try {
channel = channelFactory.newChannel();// 这个是创建
init(channel);
} catch (Throwable t) {
if (channel != null) {
// channel can be null if newChannel crashed (eg SocketException("too many open files"))
channel.unsafe().closeForcibly();
// as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
}
// as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE).setFailure(t);
}
ChannelFuture regFuture = config().group().register(channel);
if (regFuture.cause() != null) {
if (channel.isRegistered()) {
channel.close();
} else {
channel.unsafe().closeForcibly();
}
}
return regFuture;
}
Next, look at the init method:
Add handler can see when first create context, and the context is added to the end of the list.
private void addLast0(AbstractChannelHandlerContext newCtx) {
AbstractChannelHandlerContext prev = tail.prev;
newCtx.prev = prev;
newCtx.next = tail;
prev.next = newCtx;
tail.prev = newCtx;
}
------------------------
We doBind chase:
private ChannelFuture doBind(final SocketAddress localAddress) {
final ChannelFuture regFuture = initAndRegister();
final Channel channel = regFuture.channel();
if (regFuture.cause() != null) {
return regFuture;
}
if (regFuture.isDone()) {
// At this point we know that the registration was complete and successful.
ChannelPromise promise = channel.newPromise();
doBind0(regFuture, channel, localAddress, promise);
return promise;
} else {
// Registration future is almost always fulfilled already, but just in case it's not.
final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
regFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
Throwable cause = future.cause();
if (cause != null) {
// Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
// IllegalStateException once we try to access the EventLoop of the Channel.
promise.setFailure(cause);
} else {
// Registration was successful, so set the correct executor to use.
// See https://github.com/netty/netty/issues/2586
promise.registered();
doBind0(regFuture, channel, localAddress, promise);
}
}
});
return promise;
}
}
We have been chasing down can catch
Chase:
--------------------------------94-------------------------------------------