Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does any function like beforeCrash() or beforeExit() exist in Tomcat or Java

I have tomcat server running. and it crashes suddenly for some reason... however I am trying to find the error.

Is there a function in tomcat or java like beforeExit() or ifCrashed() which I can override and write some code there like notifying myself if server crashes for some reason.

like image 577
sanjeev Avatar asked Aug 02 '17 09:08

sanjeev


2 Answers

You could try using shut down hooks which are executed on system exit. It's not guaranteed though that it will be executed on a hard crash like SIGKILL but this may be an option in some cases:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        // Implementation goes here..;
    }
});
like image 196
Danylo Zatorsky Avatar answered Sep 18 '22 23:09

Danylo Zatorsky


I don't think that crashed server can manage something. You can consider this steps:

  • use servletContextListeners in order to run code before servlet context will shutdown. It will work if shutdown is done gracefully
  • if it is JVM that is crushing you can try to find tools that provide JVM monitoring services and reporting. Also I recommend to provide flag in order to get reports from OutOfMemoryError and open JMX ports for monitoring
  • you can right some mock rest endpoint and run some external job that will call that endpoint periodically and if it returns HTTPStatus.OK then it working, if not - then you can report yourself someway.

It could be better if you can provide logs and some information about what is really happening with server. Maybe here people will help but I gues this is topic of another questions.

like image 30
Izbassar Tolegen Avatar answered Sep 20 '22 23:09

Izbassar Tolegen