tomcat小记

Engine, Host, Context, Wrapper的标准实现类在实例化时都会创建StandardPipeline, 并为StandardPipeline初始化一个对应的Valve

内嵌tomcat启动过程
new Tomcat().start(), 内部会确保Server, Service, Connector的创建, tomcat默认的Server, Service, Container的实现类为Standard开头, 调用server.start(), Server, Service和
Container都集成LifeCycle接口, 因此可以统一对象管理(具有相同的生命周期函数). StandardServer.start()检测server的状态, 正常的执行顺序为默认的server的state为LifecycleState.NEW,
1.首先进行StandardServer的初始化init(), 修改状态NEW->INITIALIZING并触发before_init事件, 默认的StandardServer并不持有任何LifecycleListener, 因此不会出发任何监听行为.
然后调用initInternal(), 主要目的初始化所有的service, 调用service.init() 同StandardServer, StandardService.initInternal()主要进行engine.init(), executors.init()和connector.init().
ContainerBase实现initInternal()为每个容器初始化一个ThreadPoolExecutor, StandardEngine.initInternal()只是对Realm的处理. 然后StandardEngine状态INITIALIZING->INITIALIZED,
StandardEngine.init()方法完成. connector.init()调用ProtocolHandler.init(), connector默认的ProtocolHandler为org.apache.coyote.http11.Http11NioProtocol.

2.StandardServer.init()完成后, 状态设置为INITIALIZED->STARTING_PREP, 调用startInternal(), 触发CONFIGURE_START_EVENT事件, 状态STARTING_PREP->STARTING, 启动所有service, service.start()
StandardService.startInternal()启动engine, 启动executor, connectors.start()
engine.start()方法 状态INITIALIZED->STARTING_PREP, ContainerBase实现startInternal(), 包括启动Cluster.start(), Realm.start(), 使用init阶段创建的线程池启动Container children,
启动Pipeline.start(), 容器状态STARTING_PREP->STARTING, 启动单个线程每隔backgroundProcessorDelay秒执行一次container.backgroundProcess()(包括子容器, backgroundProcessorDelay的值大于0)。
StandardHost.startInternal()多了一步设置ErrorReportValve. StandardContext重写了startInternal方法.
StandardContext.startInternal()重写了ContainerBase.startInternal(), 设置并启动webResource.start(), children.start(), Pipeline.start(), 实例化所有filter, Wrapper, 容器状态
STARTING_PREP->STARTING. Pipeline.start()同样会调用Valve.start()


Connector initInternal和startInternal过程
1.initInternal就是调用ProtocolHandler.init(), ProtocolHandler的实现类包括常用的Http11NioProtocol, Http11AprProtocol
Http11NioProtocol指定AbstractEndpoint为NioEndpoint, 指定AbstractEndpoint.Handler为ConnectionHandler. 调用endpoint.init(), NioEndpoint初始化ServerSocketChannel.open(),
绑定port, serverSocket.configureBlocking(true), 初始化acceptorThreadCount=1, pollerThreadCount=1, NioSelectorPool.open(), NioEndpoint.init()结束。Http11NioProtocol.init()结束。


2.startInternal就是调用ProtocolHandler.start(), Http11ProtocolHandler.start()调用NioEndpoint.start().NioEndpoint.start()-> NioEndpoint.startInternal(),
NioEndpoints.startInternal() 创建工作线程池, 初始化connection连接上限,创建Poller, 每个Poller启用一个守护线程启动执行



-----------------------
CoyoteAdapter, AbstractEndpoint, AbstractEndpoint.Handler, AbstractEndpoint.Acceptor, NioEndpoint.Poller, NioEndpoint, Processor
 
CoyoteAdapter.service方法参数为coyote.Request, coyote.Response, 而Valve.invoke的参数为HttpServletRequest, HttpServletResponse

AbstractEndpoint用来处理Socket接收和发送


------------------------
NioEndpoint.Poller events(), register(), run()
register方法将NioChannel封装为PollerEvent事件并添加到Poller同步队列SynchronizedQueue中,唤醒selector.wakeup(), run方法从Poller的同步队列SynchronizedQueue获取PollerEvent, 执行
PollerEvent.run, 注册socketChannel到Poller的Selector中, 对应事件SelectionKey.OP_READ, 当没有PollerEvent事件时执行Selector.select()获取可以read的SocketChannel, selector.selectKeys,
对所有可读SocketChannel执行processKey(selectionKey, NioSocketWrapper), processKey方法首先将SelectionKey.interestOps(intops) (intops=~intops即~OP_READ), 接着调用
EndPoint.processSocket(SocketWrapperBase, SocketEvent, boolean dispatch), 如果Endpoint.getExecutor不为空, 使用线程池运行NioEndPoints.SocketProcessor, 否则当前线程直接运行,
最终委托Endpoint.handler处理, AbstractEndpoint默认为ConnectionHandler, connectionHandler.process调用Processor.process, 此处默认为Http11Processor.

------------------------
Http11Processor
ConnectionHandler.process方法处理NioSocketWrapper, 会为每个NioSocketWrapper.NioChannel分配一个Http11Processor, 这样可以确保每个NioChannel可以被Http11Processor读取完整的内容
(可能一次读取部分内容, 下次可以继续读取, 确保SocketChannel数据被完全读取). Http11Processor, Http11InputBuffer持有coyoteRequest对象, Http11InputBuffer还持有NioSocketWrapper对象,
Http11Processor.process方法内部执行service(SocketWrapperBase), service方法会首先读取请求头信息到Http11InputBuffer.ByteBuffer中, 然后到用CoyoteAdapter.service方法, 
CoyoteAdapter.service方法会调用Connector.createRequest createResponse 创建HttpServletRequest和HttpServletResponse, 然后调用connector.getService().getContainer().getPipeline()
.getFirst().invoke(request, response)


------------------------
NioEndpoint.Acceptor run()方法ServerSocket.accept()获取连接SocketChannel, 调用NioEndpoint.setSocketOptions(socket)封装SocketChannel为NioChannel或者SecureNioChannel,
NioChannel拥有SocketBufferHandler, 然后调用poller.register(nioChannel)

------------------------
LifecycleBase implements LifeCycle, Lifecycle及基本实现抽象类, 对所有的生命周期函数做了内部的抽象, xxInternal() (startInternal, initInternal),
基本按照Lifecycle状态转变图示来实现的. setStateInternal函数会设置LifecycleBase state并触发对应state的事件(LifecycleState包含boolean available, String event),

------------------------
Connector初始化(init()->initInternal()),创建CoyoteAdapter, protocolHandler.setAdapter(Http11ProtocolHandler在Connector创建时已经构造完成) , 初始化Http11ProtocolHandler.init() -> NioEndpoint.init()
NioEndpoint.init()创建ServerSocketChannel, 绑定端口, 初始化acceptorThreadCount和pollerThreadCount, NioEndpoint初始化selectorPool

Connector启动(start()->startInternal()), 启动protocolHandler.start(), 启动endpoint.start(), 然后启动守护线程执行AsyncTimeout。 NioEndpoint.start()->startInternal()
初始化SocketProcessor栈、 PollerEvent栈、 NioChannel栈, NioEndpoint初始化线程池, 初始化Poller守护线程并启动(Poller主要从同步队列获取PollerEvent并执行,也就是获取SocketChannel
并注册OP_READ事件, Selector位于Poller中, 当PollerEvent队列中没有数据时, 调用selector.select(), 获取所有SelectionKey, 调用AbstractEndPint.processSocket方法-> NioEndPoint.getExecutor()
运行SocketProcessor. executor创建的也是守护线程), 初始化Acceptor, 并为每个Acceptor创建守护线程并运行(daemon状态可以设置), Acceptor主要是获取连接的SocketChannel,调用NioEndpoint.setSocketOptions
封装成NioChannel, 循环从Poller数组中获取一个调用register封装成NioSocketWrapper设置interestOps为SelectionKey.OP_READ并添加到Poller的PollerEvent队列中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值