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

3

102

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.

Clowers answered 5/7, 2011 at 13:34 Comment(3)
Change your session management to something that persists to file? Sorry couldn't resist :)Wagon
@Wagon I've learned a great deal about Tomcat since I asked this question over six years ago. But thanks anyway.Clowers
Regarding hot swapping just classes, see #32459547Wagon
G
67

Have you tried to use Tomcat's Manager application? It allows you to undeploy / deploy war files without 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.
Geyserite answered 5/7, 2011 at 14:25 Comment(6)
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?Clowers
@Clowers - 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 :)Geyserite
@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 ?Disinterest
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!Clowers
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.Kinard
You don't need to delete the war to redeploy. Just copy over the old war and wait few seconds.Wini
C
37

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

Cafeteria answered 3/9, 2015 at 5:5 Comment(2)
Useful to know that the 'touch' method does not reload classes. IOW, it does not purge and reload the classloader hierarchy for the webappJeniferjeniffer
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.Mesquite
P
23

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

Peele answered 16/7, 2014 at 18:2 Comment(1)
Prefer the manager application instead. This is an expensive operation for the ram.Bustup

© 2022 - 2024 — McMap. All rights reserved.