How do I update a Tomcat webapp without restarting the entire service?

136,904

Solution 1

Have you tried to use Tomcat's Manager application? It allows you to undeploy / deploy war files with out shutting Tomcat down.

If you don't want to use the Manager application, you can also delete the war file from the webapps directory, Tomcat will undeploy the application after a short period of time. You can then copy a war file back into the directory, and Tomcat will deploy the war file.

If you are running Tomcat on Windows, you may need to configure your Context to not lock various files.

If you absolutely can't have any downtime, you may want to look at Tomcat 7's Parallel deployments You may deploy multiple versions of a web application with the same context path at the same time. The rules used to match requests to a context version are as follows:

  • If no session information is present in the request, use the latest version.
  • If session information is present in the request, check the session manager of each version for a matching session and if one is found, use that version.
  • If session information is present in the request but no matching session can be found, use the latest version.

Solution 2

There are multiple easy ways.

  1. Just touch web.xml of any webapp.

    touch /usr/share/tomcat/webapps/<WEBAPP-NAME>/WEB-INF/web.xml
    

You can also update a particular jar file in WEB-INF/lib and then touch web.xml, rather than building whole war file and deploying it again.

  1. Delete webapps/YOUR_WEB_APP directory, Tomcat will start deploying war within 5 seconds (assuming your war file still exists in webapps folder).

  2. Generally overwriting war file with new version gets redeployed by tomcat automatically. If not, you can touch web.xml as explained above.

  3. Copy over an already exploded "directory" to your webapps folder

Solution 3

In conf directory of apache tomcat you can find context.xml file. In that edit tag as <Context reloadable="true">. this should solve the issue and you need not restart the server

Share:
136,904
cbmeeks
Author by

cbmeeks

Object.const_set("Love", Class.new { def ruby() true end });i = Love.new;i.ruby

Updated on July 08, 2022

Comments

  • cbmeeks
    cbmeeks almost 2 years

    I'm new to Tomcat. We have a dev machine with about 5 apps running. Even though it's dev, it's used by our clients pretty heavily during testing.

    So say we need to make one small change on one class file. Right now, we have to shutdown Tomcat (affecting the other four apps), delete the WAR file (and web app directory), redeploy the new WAR file and restart Tomcat.

    Of course, this upsets a few people because it destroys all logged in sessions for all apps.

    Is there a better way to do this? I mean, is there a way to only reload the CLASS that changed instead of everything on the dev machine?

    Thanks.

    • rogerdpack
      rogerdpack over 6 years
      Change your session management to something that persists to file? Sorry couldn't resist :)
    • cbmeeks
      cbmeeks over 6 years
      @rogerdpack I've learned a great deal about Tomcat since I asked this question over six years ago. But thanks anyway.
    • rogerdpack
      rogerdpack over 6 years
      Regarding hot swapping just classes, see stackoverflow.com/questions/32459047/…
  • cbmeeks
    cbmeeks almost 13 years
    That's a good suggestion too. I never heard of the manager app until today. Yes, we use Windows. One concern I have with deleting the WAR file and waiting is that our clients are expecting "small fixes" frequently. Would those suggestions work for that scenario?
  • MacAnthony
    MacAnthony almost 13 years
    @cbmeeks - It depends on how often these small fixes happen, and how long before someone notices. In either case, there will be a small amount of time that the application is unavailable. It depends on your hardware / application how long that time is. But it's measured in seconds, I'd say usually 5-10 on our applications. But your mileage may vary :)
  • coding_idiot
    coding_idiot over 11 years
    @SteveK I'm using tomcat+IIS, so the webapps that are deployed are on different context (different websites) and definitely not within tomcat's webapps folder and hence the manager app can't see them. Any solution ?
  • cbmeeks
    cbmeeks over 11 years
    Wow. I've learned so much since I asked this question. Yeah, we now deploy Tomcat apps without taking Tomcat completely down. One thing I did was make sure all logging (app and Tomcat) do not record logs INSIDE the webapps/application folder. Or, configuring Tomcat to not lock also works. So we put logging in a central place outside of Tomcat's folder. Works like a dream. But your answer was the most helpful. Thanks!
  • Neeme Praks
    Neeme Praks about 11 years
    When redeploying without restart, you'll have to be careful with memory leaks - very often the classloaders will leak references and you need to restart the whole Tomcat process in order to free memory from the old version of the application. LiveRebel (mentioned in another answer) can automate all that for you.
  • peceps
    peceps over 10 years
    You don't need to delete the war to redeploy. Just copy over the old war and wait few seconds.
  • vishal
    vishal almost 10 years
    Hi, If we use tomcat manager app as it uploads the war file, it takes lot of time to deploy if the war file is large. When I do it manually, I git pull my project on the server, mvn install it and copy war file from target folder to tomcat webapps. Its much faster. But for this I have to restart the tomcat. Is there any way to do the same but without restarting tomcat ?
  • Rondo
    Rondo about 8 years
    Useful to know that the 'touch' method does not reload classes. IOW, it does not purge and reload the classloader hierarchy for the webapp
  • Kevin
    Kevin about 8 years
    Seeing why this works can read in $CATALINA_BASE/conf/context.xml. The WEB-INF/web.xml is watched for changes. Other resources can easily be listed there as needed.
  • Sorter
    Sorter over 5 years
    Prefer the manager application instead. This is an expensive operation for the ram.