Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch the event of shutting down of tomcat?

Tags:

java

jsp

tomcat

What I want is to start a thread every time the tomcat server starts.For this I need to catch the event of shutting down of tomcat.How can I do this?I tried to do it using sessions but sometimes the session even persists after shutting down and restating tomcat?what are my options?

like image 472
cooljohny Avatar asked Jul 16 '14 15:07

cooljohny


People also ask

Why Tomcat is getting stopped automatically?

try to clean your elipse projects because you could have tried to add another server which used port 8080 then when you try to execute the tomcat server externally that defaulty uses port 8080 the tomcat server automatically shutdowns after cleaning the project copy the new war file and paste it in bin it works fine ...

Where is Apache Tomcat logs?

The main Apache Tomcat configuration file is at /opt/bitnami/tomcat/conf/server. xml. Once Apache Tomcat starts, it will create several log files in the /opt/bitnami/tomcat/logs directory.

Why does Tomcat shutdown port?

The shutdown port provides an OS neutral, scriptable way to shutdown a Tomcat instance. Once you remove the shutdown port you are almost certainly into the realms of OS specific code (or at least different code for Windows vs Unix plus derivatives).

How do you check the Tomcat is running or not?

Use a browser to check whether Tomcat is running on URL http://localhost:8080 , where 8080 is the Tomcat port specified in conf/server. xml. If Tomcat is running properly and you specified the correct port, the browser displays the Tomcat homepage.


1 Answers

You can try to catch an JVM shutdown event in this way:

    Runtime.getRuntime().addShutdownHook(new Thread() {

        public void run() {
            System.out.println("BYE BYE");
        }
    });

The other option is to implement ServletContextListener by using @WebListener Annotation. No xml configuration is required in this case.

@WebListener
public class MyLifeCycleListener implements ServletContextListener {

      public void contextInitialized(ServletContextEvent event) {
          //TODO ON START
      }

      public void contextDestroyed(ServletContextEvent event) {
          //TODO ON DESTROY
      }
}
like image 82
dieter Avatar answered Oct 03 '22 08:10

dieter