Tomcat学习之Engine
2016-07-25 08:45
561 查看
http://blog.csdn.net/aesop_wubo/article/details/7657231
Enginetomcat engine是一个完整的Servlet容器,其下面拥有多个虚拟主机,它的责任就是将用户请求分配给一个虚拟上机处理。接口Engine代表一个Servlet引擎,其实现类是StandardEngine,先来看看构造方法
[html] view
plain copy
print?
public StandardEngine() {
super();
pipeline.setBasic(new StandardEngineValve());
/* Set the jmvRoute using the system property jvmRoute */
try {
setJvmRoute(System.getProperty("jvmRoute"));
} catch(Exception ex) {
log.warn(sm.getString("standardEngine.jvmRouteFail"));
}
// By default, the engine will hold the reloading thread
backgroundProcessorDelay = 10;
}
这个方法在Digester解析Server.xml时会调用,它首先设置一个基本阀门,然后设置JVMRoute,这个在cluster中会用到,然后为backgroundProcessorDelay赋初值,backgroundProcessorDelay的含义在前面有讲过。
来看看这个基本阀门(StandardEngineValve)做了哪些事,这里面最重要的是它的invoke方法
[html] view
plain copy
print?
public final void invoke(Request request, Response response)
throws IOException, ServletException {
// Select the Host to be used for this Request
Host host = request.getHost();
if (host == null) {
response.sendError
(HttpServletResponse.SC_BAD_REQUEST,
sm.getString("standardEngine.noHost",
request.getServerName()));
return;
}
if (request.isAsyncSupported()) {
request.setAsyncSupported(host.getPipeline().isAsyncSupported());
}
// Ask this Host to process this request
host.getPipeline().getFirst().invoke(request, response);
}
只做了两件事:
(1)选择一个主机来处理用户请求
(2)如果请求是异步的,设置异步标志位
至于引擎的初始化和启动很简单,前面有讲过Service的初始化会先初始化容器,再初始化连接器。对容器的初始化首先就会初始化顶级容器,也就是engine,引擎的启动也是在Service的启动中完成的。对于容器的整个初始化和启动过程到时会单独分析。
来看看engine的配置
[html] view
plain copy
print?
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
</Host>
</Engine>
name属性是引擎的逻辑名称,在日志和错误消息中会用到,在同一台服务器上有多个Service时,name必须唯一。
defaultHost指定默认主机,如果没有分配哪个主机来执行用户请求,由这个值所指定的主机来处理,这个值必须和<Host>元素中的其中一个相同。
Engine里面除了可以指定属性之外,还可以有其它组件,比如:log,listener,filter,realm等,后面会单独分析.
Enginetomcat engine是一个完整的Servlet容器,其下面拥有多个虚拟主机,它的责任就是将用户请求分配给一个虚拟上机处理。接口Engine代表一个Servlet引擎,其实现类是StandardEngine,先来看看构造方法
[html] view
plain copy
print?
public StandardEngine() {
super();
pipeline.setBasic(new StandardEngineValve());
/* Set the jmvRoute using the system property jvmRoute */
try {
setJvmRoute(System.getProperty("jvmRoute"));
} catch(Exception ex) {
log.warn(sm.getString("standardEngine.jvmRouteFail"));
}
// By default, the engine will hold the reloading thread
backgroundProcessorDelay = 10;
}
这个方法在Digester解析Server.xml时会调用,它首先设置一个基本阀门,然后设置JVMRoute,这个在cluster中会用到,然后为backgroundProcessorDelay赋初值,backgroundProcessorDelay的含义在前面有讲过。
来看看这个基本阀门(StandardEngineValve)做了哪些事,这里面最重要的是它的invoke方法
[html] view
plain copy
print?
public final void invoke(Request request, Response response)
throws IOException, ServletException {
// Select the Host to be used for this Request
Host host = request.getHost();
if (host == null) {
response.sendError
(HttpServletResponse.SC_BAD_REQUEST,
sm.getString("standardEngine.noHost",
request.getServerName()));
return;
}
if (request.isAsyncSupported()) {
request.setAsyncSupported(host.getPipeline().isAsyncSupported());
}
// Ask this Host to process this request
host.getPipeline().getFirst().invoke(request, response);
}
只做了两件事:
(1)选择一个主机来处理用户请求
(2)如果请求是异步的,设置异步标志位
至于引擎的初始化和启动很简单,前面有讲过Service的初始化会先初始化容器,再初始化连接器。对容器的初始化首先就会初始化顶级容器,也就是engine,引擎的启动也是在Service的启动中完成的。对于容器的整个初始化和启动过程到时会单独分析。
来看看engine的配置
[html] view
plain copy
print?
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
</Host>
</Engine>
name属性是引擎的逻辑名称,在日志和错误消息中会用到,在同一台服务器上有多个Service时,name必须唯一。
defaultHost指定默认主机,如果没有分配哪个主机来执行用户请求,由这个值所指定的主机来处理,这个值必须和<Host>元素中的其中一个相同。
Engine里面除了可以指定属性之外,还可以有其它组件,比如:log,listener,filter,realm等,后面会单独分析.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- i-jetty环境搭配与编译
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 实现单Tomcat多Server配置
- 生产环境下的Tomcat配置
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- Linux部署Tomcat服务器
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器