现在的位置: 首页 > 综合 > 正文

How Tomcat Works 12

2018年05月22日 ⁄ 综合 ⁄ 共 2046字 ⁄ 字号 评论关闭

standardContext!

它表示一个web app,所以用于app的组件它都要有,比如manager和loader。

 

一,StandardContext Configuration

当一个context被创建后,它的start方法会被立即调用,在start方法中会做很多事情。

 

  •  Fires the BEFORE_START event.
  •  Sets the availability property to false.
  •  Sets the configured property to false.
  •  Sets the resources.
  •  Sets a loader
  •  Sets a manager
  •  Initializes the character set mapper.
  •  Starts other components associated with this context
  •  Starts child containers (wrappers)
  •  Starts the pipeline
  •  Starts the manager
  •  Fires the START event. Here the listener (ContextConfig) will perform some configuration operations (discussed in Chapter 15). Upon a successful configuration, the ContextConfig will set the StandardContext instance's configured property to true.
  •  Checks the value of the configured property. If it is true, do the following: call the postWelcomePages method, load child wrappers that need to be loaded at start-up, and set the availability property to true. If the configured variable is false, call the stop method.
  •  Fire the AFTER_START event.

在START_EVENT被抛出时,一个listener会执行一些重要操作,这个listener是ContextConfig。

它解析%CATALINA_HOME%/conf下的默认web.xml和app里的web.xml,此外添加authenticator valve和certificate valve.

经过实验,如果把%CATALINA_HOME%/conf下的web.xml删除,那么启动时会报错,如果修改这个文件里的配置,会产生不同的效果。例如删除其中的DefaultServlet,server将不会处理静态请求。至于DefaultServlet是怎么映射到所有请求上的还不知道,还有个jspservlet,不知道干吗用。

 

context的invoke方法总是调用ContainerBase的invoke方法。

 

二,StandardContextMapper

在tomcat5中,已经没有这个mapper了,wrapper被绑定到request对象中。Wrapper wrapper = request.getWrapper();

 

 

三,Support for Reloading

当web.xml改变或class下的类被重编译,或web-inf/lib下的jar包改变,context会reload。

 

四,The backgroundProcess Method

与之前的每个组件建立自己的thread不同,tomcat 5中把所有需要隔一段时间执行一次的任务放到同一个thread里。这样就不必加那么多的thread了,只需要一个就可以完成所有类似任务。

ContainerBase中有个私有变量thread,每次container的start方法中都会调用threadStart方法,这个方法检查thread是否已经存在,和backgroundProcessorDelay是否小于等于0,来判断是否new 一个thread。

在tomcat中,ContainerBase里的backgroundProcessorDelay默认是小于0的,所以默认情况下,不会new这个背景thread。但在standardEngine中,把backgroundProcessorDelay重写成了10,所以,在standardEngine的start方法调用后,threadStart方法会启动一个新的thread,来每过10秒钟跑一次任务。而任务是如何跑的?是通过把standardEngine的所有child container的backgroundProcess方法跑一遍。而在ContainerBase的backgroundProcess方法中,把所有需要间隔时间完成的任务组件的backgroundProcess方法跑了一遍。

 

 

【上篇】
【下篇】

抱歉!评论已关闭.