Is there a way to run a method/class only on Tomcat/Wildfly/Glassfish startup?

后端 未结 3 1053
遥遥无期
遥遥无期 2020-11-29 20:50

I need to remove temp files on Tomcat startup, the pass to a folder which contains temp files is in applicationContext.xml.

Is there a way to run a method/class only

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 21:35

    You could write a ServletContextListener which calls your method from the contextInitialized() method. You attach the listener to your webapp in web.xml, e.g.

    
       my.Listener
    
    

    and

    package my;
    
    public class Listener implements javax.servlet.ServletContextListener {
    
       public void contextInitialized(ServletContext context) {
          MyOtherClass.callMe();
       }
    }
    

    Strictly speaking, this is only run once on webapp startup, rather than Tomcat startup, but that may amount to the same thing.

提交回复
热议问题