如何在关闭web服务时进行一些清理操作(Spring mvc)
背景
目前正在替一家500强企业开发系统,因为系统众多所以他们使用ESB对各个系统之间的服务进行管理,同样也要求我们的系统进行对接。要求在我们的系统启动时进行注册,在系统关闭时进行注销。根据要求同事写了一个serverlet在系统启动的时候进行注册操作,但是不知道在系统关闭时进行相应的操作。 目前正在替一家500强企业开发系统,因为系统众多所以他们使用ESB对各个系统之间的服务进行管理,同样也要求我们的系统进行对接。要求在我们的系统启动时进行注册,在系统关闭时进行注销。根据要求同事写了一个serverlet在系统启动的时候进行注册操作,但是不知道在系统关闭时进行相应的操作。
解决方法
因为项目使用spring mvc 项目,所以我知道可以通过spring的监听器完成相应的工作。 因为项目使用spring mvc 项目,所以我知道可以通过spring的监听器完成相应的工作。
步骤1:实现ApplicationListener 步骤1:实现ApplicationListener
package com.efuture.vpm.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; public class ApplicationEventListener implements ApplicationListener { protected final Log log = LogFactory.getLog(getClass()); public void onApplicationEvent(ApplicationEvent event) { //容器关闭时触发的事件 if(event instanceof ContextClosedEvent ){