0

I want to run 1 webapp per 1 tomcat service/cluster in Ubuntu for the reasons listed here: Should you run one or multiple applications per tomcat cluster?

Is the only way of killing the tomcat service via try & finally in java code as described here: How to shutdown tomcat service when an app/war deployment fails?

I would assume that a configurable option for Tomcat to do this automatically would make more sense. Does one exist?

Ideally it could be a parameter for the service object? https://tomcat.apache.org/tomcat-7.0-doc/config/service.html But I couldn't find anything like it

Paku
  • 455
  • 1
  • 4
  • 15
  • What do you mean by "fails"? Failure to deploy? Getting unrecoverable errors and exceptions like OOM? – Thomas Nov 15 '21 at 07:43
  • 2
    Btw, since you're going for a 1:1 relation between Tomcat and application, did you consider an embedded version, i.e. making Tomcat part of your application? That might be easier to handle and maintain as you'd have more control over versions used, deployment and integration with your app. – Thomas Nov 15 '21 at 07:45
  • By fail i mean error out. If I were to run `systemctl status tomcat7.service` then it would say it's in failed state. – Paku Nov 15 '21 at 07:50
  • Part of the application as in a docker container? – Paku Nov 15 '21 at 07:51
  • 2
    No, part of the application as in embedded into the Jar you're building (like Spring Boot and other frameworks are doing it). Have a look at [Tomcat embedded](https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/startup/Embedded.html). – Thomas Nov 15 '21 at 08:23
  • The problem in my case is that I have an old legacy app, where a embedded solution won't work without a bigger rewrite. – Paku Nov 15 '21 at 09:03
  • 1
    I see. However, I'm still not quite sure I understand what "error out" means. Do you mean the app crashes and thus Tomcat should stop or at least report a failure? Do you realize that `systemctl` is a OS facility and thus you need to configure the service differently? It's been a while since I used it so I can't give any details but maybe if you'd add examples of what failures you expect to lead to what state (reported state and what state Tomcat should be in, e.g. should it be shut down?) it might help others to better understand what you're trying to achieve. – Thomas Nov 15 '21 at 11:51
  • I realize that `systemctl` is an OS facility. Ideally yes when the app crashes I would like the Tomcat service to stop. Even more ideally when Tomcat doesn't respond to REST requests and they don't get logged also Tomcat service stops. In Tomcat7.service Ubuntu default installed unit file the process that gets run is `ExecStart=/opt/tomcat/bin/startup.sh`. This means that when the startup.sh script along all it's child processes (due to `Type=forking` in the unit file) stop, then the Tomcat service stops. – Paku Nov 15 '21 at 16:45
  • I assume that the best solution is try/finally in the application code to catch failing exceptions, and then sending the System.exit() command. – Paku Nov 15 '21 at 16:46
  • The origin of my problem is actually some non-deterministic pipelines, which I would hope to solve by getting a stronger mapping from app to systemd service. – Paku Nov 15 '21 at 16:52
  • 1
    You can register a default exception handler and implement the "exit" logic there. If the exceptions don't bubble up that far then the handling within the application might need to be refactored anyway. Also, it might be good to provide a health check endpoint that your system could call periodically and if this fails or doesn't respond the service is killed (I'd not try to watch for missing logs as this is fragile). – Thomas Nov 15 '21 at 17:38
  • 1
    @Paku: Tomcat almost never shuts down, when a context fails to start (with some exceptions like in [this question](https://stackoverflow.com/q/67721077/11748454)). You can however write a [`LifecycleListener`](https://tomcat.apache.org/tomcat-9.0-doc/api/org/apache/catalina/LifecycleListener.html) that stops the server if the context transitions to a `FAILED` state. – Piotr P. Karwasz Nov 16 '21 at 05:13

0 Answers0