Tomcat学习之Engine

来源:互联网 发布:汤臣集团 知乎 编辑:程序博客网 时间:2024/06/04 17:44
Enginetomcat engine是一个完整的Servlet容器,其下面拥有多个虚拟主机,它的责任就是将用户请求分配给一个虚拟上机处理。接口Engine代表一个Servlet引擎,其实现类是StandardEngine,先来看看构造方法

    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方法

 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的配置

<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等,后面会单独分析.


原创粉丝点击