Can a web application be notified that a web container (e.g. Tomcat) is reloading, unloading, or terminating

I have a Wicket web application running on Tomcat. To initialize the application, the application uses Spring (via org.springframework.web.context.ContextLoaderListener). This is good and useful to run, but I would like to receive a notification that the Context is being destroyed so that I can disable the generated threads. Is there any way to get such a notification? I have included excerpts from my application to help you understand my question.

extract web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/mysite/web/spring/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

Spring applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="MyWebService" class="com.mysite.web.MyWebApp">
    </bean>
</beans>

Instance of MyWebApp.java

public class MyWebApp extends org.apache.wicket.protocol.http.WebApplication {
    private MyWebServiceServiceAPI webservice =
       MyWebServiceAppImpl.getMyWebService();

    public MyWebServiceWebApp() {
    }

    @Override
    public void init() {
        super.init();
        webservice.start();
    }
}

MyWebServiceAppImpl.java Instance

    public class MyWebServiceAppImpl  extends ServiceImpl
        implements MyWebServiceServiceAPI  {
        // The ServiceImpl implements the Callable<T> interface

    private static MyWebServiceServiceAPI instance;
    private List<Future<ServiceImpl>> results =
        new ArrayList<Future<ServiceImpl>>();
    private ExecutorService pool = Executors.newCachedThreadPool();


    private MyWebServiceAppImpl() {
        super(.....);
    }

    public synchronized static MyWebServiceServiceAPI getMyWebService() {
        if (instance == null) {
            instance = new MyWebServiceAppImpl();
            instance.start();
        }
        return instance;
    }

@Override
public synchronized void start() {

    if (!started()) {
        pool.submit(this);
        super.start();
    }
}
+3
1

, ContextListener.

package myapp;

public class MyContextListener implements ServletContextListener {



    public void contextInitialized(ServletContextEvent arg0) {
        //called when context is started
    }


    public void contextDestroyed(ServletContextEvent arg0) {
      //called context destroyed
    }   
}

web.xml

 <listener>
    <listener-class>myapp.MyContextListener</listener-class>
 </listener>
+2

Source: https://habr.com/ru/post/1733756/


All Articles