接上一篇,说到XmlConfiguration ,XmlConfiguration 利用自己实现的IOC组装Server的全过程如下图所示:
这里可以看到3个关键的配置文件,jetty.xml、jetty-deploy.xml、以及contexts/xxx.xml
l Jetty.xml文件中定义了入口类Server,以及其所需要的线程池、Connector、Handler。
l Jetty-deploy.xml中则定义了部署web应用用到部署工具,在其中指定了部署web应用的两种方式,类似于tomcat, 如果采用webappProvider,则表示将web应用放在webapp下即可生效,如果采用ContextProvider,则需要定义Contexts目录所在位置,只要在该目录下放置任何应用的context配置文件就可以生效。
l Xxx.xml 这是一个用户自定义文件,表示采用ContextProvider时,在其中定义一个WebAppContext的handler,它指定了我们应用所在的位置,便于加载。
然后再调用open创建了一个blocking的阻塞channel,专门用于接受用户的新连接,我们看下:
public void open() throws IOException{synchronized(this){if (_acceptChannel == null){// Create a new server socket_acceptChannel = ServerSocketChannel.open();// Set to blocking mode_acceptChannel.configureBlocking(true);// Bind the server socket to the local host and port_acceptChannel.socket().setReuseAddress(getReuseAddress());InetSocketAddress addr = getHost()==null?new InetSocketAddress(getPort()):new InetSocketAddress(getHost(),getPort());_acceptChannel.socket().bind(addr,getAcceptQueueSize());_localPort=_acceptChannel.socket().getLocalPort();if (_localPort<=0)throw new IOException("Server channel not bound");}}}
随后从线程池中分配了N个(可以在配置文件中配置)线程用于启动SelectSet监听read事件。
synchronized (this){_acceptorThread = new Thread[getAcceptors()];for (int i = 0; i < _acceptorThread.length; i++)_threadPool.dispatch(new Acceptor(i));if (_threadPool.isLowOnThreads())Log.warn("insufficient threads configured for {}",this);}