你的位置:首页 > Java教程

[Java教程]Tomcat:自动部署、自动重加载、自动编译


        麻雀虽小,五脏俱全,Tomcat 提供的自动部署、自动重加载、自动编译功能,可谓是让人又爱又恨。接下来就对这三者做一个介绍,文章中会Copy一些官方文档中的说法。

If you are using the standard Host implementation, the following actions take place automatically when Catalina is first started, if the deployOnStartup property is set to true (which is the default value):

  • Any The docBase attribute of this <Context> element must only be set if the docBaseis outside the Host'sappBase. For web applications located inside theHost's appBase, the docBase will be the name of the The path attribute must not be set. Thecontext path used will be a slash character ("/") followed by thename of the
  • Anyweb application archive file within the Host's appBase directory that has not already been deployed as a result of acontext If the unpackWARs attribute is true, the web application archive file will be expanded to a directory of thesame name (without the ".war" extension".
    Note: If you re-deploy an updated WAR file while Tomcat is stopped, be sure todelete the associated expanded directory before restarting Tomcat, so that theupdated WAR file will be re-expanded when Tomcat restarts. 如果指定了<HostunpackWARs="true" />,部署应用时,就会将test.war解压成test目录。你要是想升级test应用,就需要将tomcat shutdown,并删除test.war和test目录。
    Any web application archive file within the Hosts's appBase directory that does not have acorresponding context
  • Finally,any sub-directory within the Host's appBase that has not already been deployed as a result of a context Any directory within the Hosts's appBase directorythat does not have a corresponding context

=================================

自动部署

Inaddition to the automatic deployment that occurs at startup time, you can alsorequest that new

  • Anupdate to the WEB-INF/web.
  • Deletinga WAR file will trigger an undeploy of the application with the removal of anyassociated expanded directory, context file and work directory. Any current usersessions will not be persisted. 如果删除了war包就会触发解部署操作。解部署时会删除应用解压后的目录、context描述符文件、work/[engine]/[host]/下的对应目录(也称为work目录)。
  • Deletinga directory will trigger an undeploy of the application with the removal of anyassociated context file and work directory. Any current user sessions will notbe persisted. If there is an associated WAR file, it will not be deleted andthe application will be redeployed from the WAR file the next time the autodeployer checks for changes. 删除host appbase下的目录,就会删除与之关联的context文件和work目录,session 信息就可能完蛋了。但是关联的war不会被移除。
  • Deletinga context file will trigger an undeploy of the application with the removal ofany associated work directory. Any current user sessions will not be persisted.If there is an associated WAR file and/or directory, they will not be deletedand the application will be redeployed from the WAR file (or from directory ifthere is no WAR file) the next timie the auto deployer checks for changes. 移除上下文文件时,也就触发解部署操作。但只会移除work目录,不会移除与之关联的应用目录和war包。
  • Updatinga WAR file will trigger an undeploy of the application with the removal of anyassociated expanded directory, context file and work directory. Any currentuser sessions will not be persisted. 更新war包(tomcat会周期性的检查文件或者目录的时间戳,从而加以判断的)会触发解部署操作。
  • Updatinga directory (not the directory contents) will trigger an undeploy of theapplication with the removal of any associated context file and work directory.Any current user sessions will not be persisted. The application will beredeployed the next time the auto deployer checks for changes.
  • Updatinga context file will trigger an undeploy of the application with the removal ofany associated work directory. Any current user sessions will not be persisted.The application will be redeployed the next time the auto deployer checks forchanges.

对上述几种情况做一个小小的总结:启用自动部署功能,只需要设置<Host autoDeploy=”true” />就可以了。另外要说明的是,如果在想要添加一个web应用,只需要将web应的目录或者war包加入的appBase目录下,就会自动的进行部署了。

自动部署功能,会监控web.

针对会触发解部署的操作中,根据重要性,有一个排序:war>web应用目录>context.

另外,如果想要了解这部分的源码,可以从HostConfig.java 中的public voidcheck(path)方法看起。

 

Whenusing automatic deployment, the docBase definedby an

Finally,note that if you are defining contexts explicitly in server.

 

 

自动重加载

自动重加载:是说tomcat中已经将web应的类文件加载完毕了,但是有些类文件发生了改变,在不重启tomcat的情况下,要应用到新的class。也就是热部署。想要启用这个功能,需要将<Contextreloadable=”true” />启用。

Set to true if you want Catalina to monitor classes in /WEB-INF/classes/ and /WEB-INF/libfor changes, and automatically reload the web application if a change isdetected. This feature is very useful during application development, but itrequires significant runtime overhead and is not recommended for use ondeployed production applications. That's why the default setting for thisattribute is false. Youcan use the Manager web application, however, to trigger reloads of deployed applications ondemand.

上面的说这个参数,管理的范围是WEB-INF/lib目录下的jar包,以及WEB-INF/classes目录下下的class文件。如果这些发生了变化,就会触发热部署(自动重加载)的。但是这在生产环境下是不推荐的。

 

另外说一点热部署相关的内容:因为ClassLoader有一个特点,就是一个class加载后,就会在ClassLoader中有记录的,不会再去加载第二遍的,所以要实现热部署功能,肯定是换了另外一个类加载器的。

 

自动编译

自动编译,是针对JSP文件的。开发过程中,修改了jsp文件,不需要重启tomcat,就能应用上最新的文件了,为开发节省了不少的时间。管理jsp文件的编译的是一个JspServlet。它有三个重要的参数:development、checkInterval、modificationTestInterval。

在说这个三个参数前,先了解一下jsp编译:

JSP后台编译:如果你修改了jsp文件后,就会进行后台编译,来将文件重新编译一下。

Recompile Jsp当inclued文件发生变化:一个JSP文件可以使用<include/>来引入其他的jsp文件(称为子jsp),例如a.jsp中使用<jsp:include />引入了b.jsp如果子b.jsp发生了改变,就会重编译a.jsp文件。

 

development:是控制是否开发模式或者生产模式。其实就是来控制jsp编译的。

Development=true时,就会参考modificationTestInterval的设置,这个值是为了延迟jsp文件的冲编译。这个参数的默认值是4s,当jsp文件发生了改变,4秒后就会重新编译。如果你将这个值设置为0,就不会延迟了,但是会在每一次访问就会编译一次。

Development=false时,就会参考checkInterval的设置了。如果这个参数的值是大于0,就会启动后台编译功能,按照指定的间隔对文件进行编译。默认值是0 。