Tomcat: Automatic deployment, automatic reload, automatic compilation, and tomcat deployment
Although Sparrow is small and dirty, Tomcat provides automatic deployment, automatic re-loading, and automatic compilation functions, which are both fascinating and annoying. Next, we will introduce the three. The article will Copy some official documents.
If you are using the standardHostImplementation, 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 XML file in $ CATALINA_BASE/conf/[engine_name]/[host_name] is assumed to be a context XML descriptor containing a Context element (and its associated sub-elements) for a single web application. the web applications associated with each of these context XML descriptor files will be deployed first. in the $ CATALINA_BASE/conf/[engine_name]/[host_name] Directory, any xml file must be an xml file whose <Context/> element is the root element, will be considered as a w Eb application, and the Application name is the name of the xml file. For example, server. <Engine name = "Catalina"> <Host name = "localhost"/> </Engine> in xml, after Tomcat is started, Catalina is created in the $ CATALINA_BASE/conf/directory. /localhost directory, the premise is that these two directories do not exist. If they exist, they do not need to be created. Then, the xml file in the directory will be scanned. If the xml file uses the Context element as the root element, the file will be considered as a web application for deployment.
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 XML file ". xml "replaced ". war "for a web application archive or the name of the XML file ". xml "removed for a directory. if your web application is placed outside the appBase directory of the scan directory of the host, you need to <Context docBase = "specify the location of the application"/>
The path attribute must not be set. thecontext path used will be a slash character ("/") followed by thename of the XML file (less. xml extension ). multi-level context paths maybe defined using #, e. g. foo # bar. xml for a context path of/foo/bar. the default web application that has acontext path of/may be defined by using a filecalled ROOT. xml. the path element does not need to be set. If your web application is test. war in the context descriptor directory. Generate a test. xml file. The corresponding path is/test. For a multi-level directory application, for example, placing the xml file in $ CATALINA_BASE/conf/Catalina/localhost/foo/bar. in xml,/foo/bar is used as the application access path.
- Anyweb application archive file within the Host's appBase directory that has not already been deployed as a result of acontext XML descriptor, does not have a corresponding directory of the samename (without ". war "extension), and is not excluded by deployIgnore will be deployed next. the context pathused will be a slash character ("/") followed by the web applicationarchive name less ". war "extension. the one exception to thisrule is that a web application archive named "ROOT. war "will bedeployed with a context path /. multi-levelcontexts may be defined by using #, e.g. use a WAR named foo # bar. war for a context path of/foo/bar.
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. if <HostunpackWARs = "true"/> is specified, test. decompress war to the test directory. To upgrade the test application, you need to shutdown tomcat and delete the test. war and test directories.
Any web application archive file within the Hosts appBase directory that does not have acorresponding context XML descriptor (with ". xml "extension ratherthan ". war "extension) in $ CATALINA_BASE/conf/[engine_name]/[host_name] will be scanned to see if it contains acontext XML descriptor (located at/META-INF/context. xml) and if one is found the descriptor will becopied to the $ CATALINA_BASE/con F/[engine_name]/[host_name] directory and renamed. The war package under the host 'sappbase directory will not generate a context descriptor file for it. If it is a web application directory, it will. Therefore, if you place test. war in the appBase directory and specify unpackWARS = true, a context descriptor file is generated.
- Finally, any sub-directory within the Host's appBase that has not already been deployed as a result of a context XMLdescriptor and is not excluded by deployIgnore will be deployed. the context path used will be a slash character ("/") followed by the directory name, unless the directory name isROOT, in which case the context path will /. multi-level contexts may be defined by using #, e.g. use a directory namedfoo # bar for a context path of/foo/bar.
Any directory within the Hosts appBase directorythat does not have a corresponding context XML descriptor in $ CATALINA_BASE/conf/[engine_name]/[host_name] will be scanned to see if it containsa context XML descriptor (located at/META-INF/context. xml) and if one is found the descriptor will becopied to $ CATALINA_BASE/conf/[engine_name]/[host_name] and renamed. in the appBase directory of the Host, you can also put the application under the multi-level directory. The generated context descriptor is in the corresponding multi-level directory, and the path is the same.
======================================
Automatic deployment
Inaddition to the automatic deployment that occurs at startup time, you can alsorequest that new XML configuration files, WAR files, or sub-directories thatare dropped in to the appBase (or $ CATALINA_BASE/conf/[engine_name]/[host_name] in the case of an XML configurationfile) directory while Tomcat is running will be automatically deployed, according to the rules described above. the auto deployer will also track webapplications for the following changes (automatic compilation is triggered in The following situations ):
- Anupdate to the WEB-INF/web. xml file will trigger a reload of the web application updates the web. xml
- 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. if the war package is deleted, the deploy operation is triggered. During deploy, the corresponding directories (also called the work directory) under the unzipped directory, context descriptor file, and work/[engine]/[host]/of the application will be deleted ).
- 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. if you delete the directory under the host appbase, the associated context file and work directory will be deleted, and the session information will be It may be finished. However, the associated war is not removed.
- 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. remove Context The deploy operation is triggered. However, the work directory is removed, and the associated application directory and war package are not removed.
- 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. updating the war package (tomcat periodically checks the timestamp of the file or directory to determine) triggers the deploy operation.
- 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.
Make a small summary of the above situations: To enable the automatic deployment function, you only need to set <Host autoDeploy = "true"/>. In addition, if you want to add a web application, you only need to add the directory or war package of the web application to the appBase directory.
The automatic deployment function monitors the web. xml, context. xml, application directories, and war packages. If the timestamp of the content changes, it is deemed that the application has been added, changed, or removed, and the deploy and deploy operations are triggered.
For operations that trigger deploy, There Is A sort by importance: war> web application directory> context. xml> work directory. The deploy operations triggered by war, web, and context. xml will remove the content after them.
For more information about the source code, see the public voidcheck (path) method in HostConfig. java.
Whenusing automatic deployment, the docBase definedby an XML Context file shocould beoutside of the appBasedirectory. if this is not the case, difficulties may be experienced deploying the web application or theapplication may be deployed twice. the deployIgnore attribute can be used to avoid this situation.
Finally, note that if you are defining contexts explicitly in server. xml, you shouldprobably turn off automatic application deployment or specify deployIgnore carefully. otherwise, the webapplications will each be deployed twice, and that may cause problems for theapplications.
Automatic Reload
Automatic Reload: it means that the web class file has been loaded in tomcat, but some class files have changed. without restarting tomcat, apply to the new class. That is, hot deployment. To enable this function, you must enable <Contextreloadable = "true"/>.
SettrueIf 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 isFalse. Youcan use the Manager web application, however, to trigger reloads of deployed applications ondemand.
The above said this parameter, the management scope is the jar package under the WEB-INF/lib directory, and the class file under the WEB-INF/classes directory. If these changes occur, hot deployment (Automatic reload) is triggered. However, this is not recommended in the production environment.
In addition, some hot deployment related content: Because ClassLoader has a feature that after a class is loaded, it will have records in the ClassLoader and won't load the second time, therefore, to implement the hot deployment function, it must have been replaced by another class loader.
Automatic compilation
Automatic compilation is for JSP files. During the development process, the jsp file is modified and the latest file can be applied without restarting tomcat, saving a lot of time for development. To manage jsp files, compile a JspServlet. It has three important parameters: development, checkInterval, and modificationTestInterval.
Before talking about the three parameters, let's take a look at jsp Compilation:
JSP background Compilation: If you modify the jsp file, it will be compiled in the background to re-compile the file.
Recompile Jsp when the received file changes: a JSP file can be used to introduce other jsp files (called sub-jsp), such as. <jsp: include/> introduced B. jsp if sub-B. if jsp changes,. jsp file.
Development: whether to control the development mode or production mode. In fact, it is to control jsp compilation.
When Development = true, we will refer to the modificationTestInterval settings. This value is used to delay the rush compilation of jsp files. The default value of this parameter is 4 s. When the jsp file is changed, it will be re-compiled after 4 seconds. If you set this value to 0, it will not be delayed, but it will be compiled once every access.
When Development = false, we will refer to the checkInterval settings. If the value of this parameter is greater than 0, the background compilation function is enabled to compile the file at the specified interval. The default value is 0.