我有一个在 Tomcat 中运行的 Wicket Web 应用程序。应用程序使用 Spring(通过 org.springframework.web.context.ContextLoaderListener)来初始化应用程序。这一切都很好,有利于启动,但我还希望收到某种通知,说明 Context 正在被销毁,以便我可以关闭生成的线程。有没有办法收到这样的通知?我已经包含了我的应用程序的摘录,以帮助您理解我的问题。
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>
春天应用上下文.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>
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 提取
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();
}
}