https://github.com/wuyinxian124/nettybook2.git
使用com.phei.netty.frame.delimiter.EchoServer做实验
使用这个工程时要处理一下pom的包冲突,否则调试的时候回显"alternative"之类让你选择代码的操作
pom文件见文末附件
几个比较重要的方法
1、io.netty.util.concurrent.ThreadPerTaskExecutor#execute
创建新线程
2、sun.nio.ch.WindowsSelectorImpl#implRegister
为keys(这是一个集合,后面会介绍)添加新元素
3、向集合keys添加新的元素
4、向集合selectedKeys添加新的元素sun.nio.ch.WindowsSelectorImpl$SubSelector#processFDSet
基本概念
selector有仨集合 keys selected-keys cancel-keys
名称 | 说明 |
---|---|
keys | 保存被轮询的key |
selected-keys | 有待处理事件的key |
cancel-keys | 下一次select()时需要删除的key |
本文要讨论的就是keys这个集合的初始化流程
流程
一、向boss的NioEventLoop中注册任务
register:400, AbstractChannel$AbstractUnsafe (io.netty.channel)
initAndRegister:276, AbstractBootstrap (io.netty.bootstrap)
doBind:234, AbstractBootstrap (io.netty.bootstrap)
bind:230, AbstractBootstrap (io.netty.bootstrap)
bind:205, AbstractBootstrap (io.netty.bootstrap)
bind:64, EchoServer (com.phei.netty.frame.delimiter)
main:84, EchoServer (com.phei.netty.frame.delimiter)
上图中调用了io.netty.util.concurrent.SingleThreadEventExecutor#execute(Runnable task)
这个方法,有三点需要注意
1、SingleThreadEventExecutor是NioEventLoop的父类,所以NioEventLoop可以调用SingleThreadEventExecutor的一些方法(比如SingleThreadEventExecutor#execute(Runnable task)
)
2、SingleThreadEventExecutor#execute(Runnable task)
中会先创建线程,然后将添加task
@Override
public void execute(Runnable task) {
。。。。。。。。。。。
startThread();//先创建线程,这个创建线程的步骤下文会详细介绍
addTask(task);//然后添加task
。。。。。。
}
3、addTask(task)
会将任务添加到SingleThreadEventExecutor的taskQueue这个字段中,这个字段是私有的,NioEventLoop不能直接修改
private final Queue<Runnable> taskQueue;
二、startThread();创建线程
跳转到eventLoop#execute里面,可以看到主线程创建boss线程
doStartThread:783, SingleThreadEventExecutor (io.netty.util.concurrent)
startThread:776, SingleThreadEventExecutor (io.netty.util.concurrent)
execute:654, SingleThreadEventExecutor (io.netty.util.concurrent)
register:400, AbstractChannel$AbstractUnsafe (io.netty.channel)
initAndRegister:276, AbstractBootstrap (io.netty.bootstrap)
doBind:234, AbstractBootstrap (io.netty.bootstrap)
bind:230, AbstractBootstrap (io.netty.bootstrap)
bind:205, AbstractBootstrap (io.netty.bootstrap)
bind:64, EchoServer (com.phei.netty.frame.delimiter)
main:84, EchoServer (com.phei.netty.frame.delimiter)
任务提交之后立刻创建新线程执行,有一点需要注意,runnable对象中的SingleThreadEventExecutor.this.run()
实际上执行的是NioEventLoop的run方法(如下图)
三、添加task
eventLoop#execute中创建完线程,紧接着添加任务(就是register0这个任务)。
execute:655, SingleThreadEventExecutor (io.netty.util.concurrent)
register:400, AbstractChannel$AbstractUnsafe (io.netty.channel)
initAndRegister:276, AbstractBootstrap (io.netty.bootstrap)
doBind:234, AbstractBootstrap (io.netty.bootstrap)
bind:230, AbstractBootstrap (io.netty.bootstrap)
bind:205, AbstractBootstrap (io.netty.bootstrap)
bind:64, EchoServer (com.phei.netty.frame.delimiter)
main:84, EchoServer (com.phei.netty.frame.delimiter)
taskQueue中那个新元素的promise字段正式在步骤一中所用的promise
此外boss线程启动(具体的启动时间与底层相关,有可能再次实验的时候在添加新task之前就已经启动了)
至此,boss线程启动,有关初始化keys的task已添加到boss的NioEventLoop中
四、boss执行已注册的任务
疑问1
promise的channel字段中包含很多重要信息。其中ch
字段和channel
本身将作为新key创建时的入参
新key创建时除了上述俩入参,还有一个WindowSelectorImpl作为入参。
五、将新key添加到keys集合中
至此selector中的keys集合初始化完成
附件
pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nettybook2</groupId>
<artifactId>nettybook2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha1</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-example</artifactId>
<version>5.0.0.Alpha1</version>
<exclusions>
<exclusion>
<artifactId>netty-transport</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-codec</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-common</artifactId>
<groupId>io.netty</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling</artifactId>
<version>1.4.10.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-serial</artifactId>
<version>1.4.10.Final</version>
</dependency>
<dependency>
<groupId>org.apache.bcel</groupId>
<artifactId>bcel</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
<version>3.2.9</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-bind</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-run</artifactId>
<version>1.2.5</version>
<exclusions>
<exclusion>
<artifactId>joda-time</artifactId>
<groupId>joda-time</groupId>
</exclusion>
<exclusion>
<artifactId>xpp3</artifactId>
<groupId>xpp3</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-extras</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-schema</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.jibx</groupId>
<artifactId>jibx-tools</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
<version>1.1.4c</version>
</dependency>
<dependency>
<groupId>org.ogce</groupId>
<artifactId>xpp3</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>1.12.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>