Keywords: Tomcat | Hot Deployment | Web Application Updates | Zero-Downtime Deployment | Parallel Deployment
Abstract: This paper provides a comprehensive analysis of various hot deployment techniques for Tomcat servers, addressing the service interruption issues caused by traditional restart-based deployment methods. The article begins by introducing the fundamental usage of the Tomcat Manager application, detailing how to dynamically deploy and undeploy WAR files using this tool. It then examines alternative approaches involving direct manipulation of the webapps directory, including operations such as deleting application directories and updating WAR files. Configuration recommendations are provided for file locking issues specific to Windows environments. The paper highlights Tomcat 7's parallel deployment feature, which supports running multiple versions of the same application simultaneously, enabling true zero-downtime updates. Additional practical techniques, such as triggering application reloads by modifying web.xml, are also discussed, offering developers a complete hot deployment solution.
Overview of Tomcat Hot Deployment
In web application development and testing, frequent code updates are inevitable. Traditional Tomcat deployment methods require restarting the entire server, which not only causes service interruptions but also destroys session states for all applications, creating significant inconveniences for development and testing workflows. Hot deployment techniques allow updating individual web applications without restarting the Tomcat service, greatly improving development efficiency and system availability.
Using the Tomcat Manager Application
Tomcat provides a web management application called Manager, specifically designed for managing the deployment status of web applications. Through this application, administrators can dynamically deploy, undeploy, start, and stop web applications without restarting the Tomcat server.
To use the Manager application, appropriate user roles must first be added to the tomcat-users.xml configuration file. For example:
<role rolename="manager-gui"/>
<user username="admin" password="password" roles="manager-gui"/>After configuration, access the management interface at http://localhost:8080/manager/html. To deploy a new application, simply select the WAR file and click the deploy button. For already deployed applications, click the "undeploy" button to remove them from the server, a process that does not affect other running applications.
Direct Manipulation of the webapps Directory
If the Manager application is not preferred, hot deployment can also be achieved by directly manipulating Tomcat's webapps directory. When a WAR file is deleted from this directory, Tomcat automatically detects the change and undeploys the corresponding application within a short period. Subsequently, copying a new WAR file into the directory will cause Tomcat to automatically deploy it as a new web application.
Although this method is straightforward, file locking issues may arise in Windows environments. To address this, set the antiResourceLocking and antiJARLocking properties in the application's context configuration:
<Context antiResourceLocking="true" antiJARLocking="true">
...
</Context>Tomcat 7 Parallel Deployment
For production environments requiring zero downtime, Tomcat 7 introduced parallel deployment functionality. This feature allows multiple versions of the same web application to run simultaneously, with the system intelligently routing requests to the appropriate version based on session state.
The version matching rules for parallel deployment are as follows:
- If the request contains no session information, the latest version of the application is used
- If the request contains session information, the system checks the session managers of each version and uses the version with a matching session
- If the request contains session information but no matching session is found, the latest version is used
To implement parallel deployment, simply add version identifiers to WAR filenames during deployment, such as myapp##1.war and myapp##2.war. Tomcat will automatically deploy these as different versions of the same application.
Other Practical Techniques
In addition to the methods above, several practical hot deployment techniques exist:
Modify the
web.xmlfile: Updating the timestamp of theWEB-INF/web.xmlfile can trigger Tomcat to reload the entire web application. For example, on Linux systems, use thetouchcommand:touch /path/to/webapp/WEB-INF/web.xmlUpdate JAR files: If only specific class files need updating, package them into a JAR file and place it in the
WEB-INF/libdirectory, then modify theweb.xmltimestamp. This avoids rebuilding the entire WAR file.Configure automatic reloading: Set the
reloadable="true"property in thecontext.xmlfile. Tomcat will monitor changes in theWEB-INF/classesandWEB-INF/libdirectories and automatically reload the application upon detecting modifications.
Summary and Recommendations
Tomcat offers multiple hot deployment solutions, allowing development teams to choose appropriate methods based on specific needs. For development environments, using the Manager application or directly manipulating the webapps directory are simple and effective choices. For production environments, especially systems with high availability requirements, Tomcat 7's parallel deployment feature is recommended, as it enables true zero-downtime updates.
Regardless of the chosen method, attention must be paid to issues such as file locking and session management. A well-planned hot deployment strategy not only enhances development efficiency but also significantly improves system availability and user experience.