jetty源码概览

jetty启动时,会执行org.mortbay.jetty.Server.doStart()方法,Server其实就是jetty容器的抽象,doStart()方法会依次启动线程池QueuedThreadPool, handler(处理具体的客户端请求),connectors(监听浏览器请求并把他作为job加入到线程池后续处理)

线程池中有两类线程

第一种是acceptor线程,两个acceptor线程(cpu核心数)持续运行,此线程用来监听客户端请求,主要通过调用connector来完成,具体代码在方法org.mortbay.io.nio.SelectorManager.SelectSet.doSelect(),此方法实现是典型的nio server实现,

                			else if (o instanceof SocketChannel)
                			{
                				final SocketChannel channel=(SocketChannel)o;

                				if (channel.isConnected())
                				{
                					key = channel.register(selector,SelectionKey.OP_READ,null);
                					SelectChannelEndPoint endpoint = newEndPoint(channel,this,key);
                					key.attach(endpoint);
                					endpoint.dispatch();
                				}
 

此方法有一段这样的代码,是将客户端的socketchannel加入线程池,这就是线程池中第二种线程

被dispatch到线程池如下

总结下:

jetty启动创建QueuedThreadPool,handler,connector

QueuedThreadPool里面会放入acceptor线程同时初始化其他用来处理后续具体请求的线程,也就是执行handler

acceptor线程最先开始运行,初始化服务端的ServerSocketChannel在connector中,然后接受客户端SocketChannel并放入线程池,由业务线程交由handler处理下去

猜你喜欢

转载自xuhang1128.iteye.com/blog/1743344
今日推荐