netty学习及源码分析(一)

本文详细介绍了Netty的学习与源码分析,重点解析了ServerBootstrap的使用,NioServerSocketChannelFactory的创建,以及ChannelPipeline和Channel的初始化过程。通过bind方法启动ServerBootstrap,探讨了DefaultChannelPipeline的链表结构和事件传递机制,最后展示了从fireChannelOpen方法到Handler的事件处理流程。
摘要由CSDN通过智能技术生成

netty学习及源码分析(一)

一直以来都想要研究下netty,苦于平时工作中很少有用到netty的场景,一直拖到现在才有时间学习一下netty,废话不多说,直接进入正题:
源码查看工具为idea,使用的netty版本为netty4。本节主要分析netty的server端一些简单的源码执行过程分析。
首先,创建netty server时需要先启动一个ServerBootstrap,它的构造方法为:
/**
* Creates a new instance with the specified initial {@link ChannelFactory}.
*/
public ServerBootstrap(ChannelFactory channelFactory) {
super(channelFactory);
}
/**
* Creates a new instance with the specified initial {@link ChannelFactory}.
*/
protected Bootstrap(ChannelFactory channelFactory) {
setFactory(channelFactory);
}

Bootstrap中有个private volatile ChannelFactory factory属性,该属性是用来创建channel使用的。
ChannelFactory有很多实现类,以NioServerSocketChannelFactory为例,其构造方法为:
/**
* Creates a new instance.
*
* @param bossExecutor
* the {@link Executor} which will execute the boss threads
* @param workerExecutor
* the {@link Executor} which will execute the I/O worker threads
* @param workerCount
* the maximum number of I/O worker threads
*/
public NioServerSocketChannelFactory(
Executor bossExecutor, Executor workerExecutor,
int workerCount) {//需要传入两个初始的Executor即需要构造两个线程池,并传入worker的数量
if (bossExecutor == null) {
throw new NullPointerException("bossExecutor");
}
if (workerExecutor == null) {
throw new NullPointerException("workerExecutor");
}
if (workerCount <= 0) {
throw new IllegalArgumentException(
&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值