在做struts应用的时候,经常学要修改struts-config.xml文件,在每次修改完之后只有重新启动服务器才能让修改生效。因此做了一个Listener,在应用启动的时候开始,每隔一段时间就去检查一下struts-config.xml文件的最后修改时间,如果修改时间变化了,就重新读取struts-config.xml,将对应的配置放到ServletContext中去。
一.实现ServletContextListener监听器的代码如下:
package com.divitone.util.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Timer;
/**
* ServletContextListener监听器,重新加载配置文件
*
*/
public class ReloadResourceListener implements ServletContextListener
{
// 执行任务前的延迟时间,单位是毫秒。
private static long DELAY = 0l;
// 执行各后续任务之间的时间间隔,单位是毫秒。
private static long PERIOD = 30 * 1000l;
/**
* 实现ServletContextListener的contextInitialized方法。 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。
*
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent event)
{
Timer loadResource = new Timer();
// 获取ServletContext
ServletContext servletContext = event.getServletContext();
// 创建一个LoadResourceTimerTask 的实例
LoadResourceTimerTask loadResourceTimerTask = new LoadResourceTimerTask(servletContext);
// 将刚创建的TimerTask的实例的运行计划定为:马上开始,每隔30×1000ms运行一次
loadResource.schedule(loadResourceTimerTask, DELAY, PERIOD);
}
/**
* 实现ServletContextListener的contextDestroyed方法。
*
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0)
{
}
}
二.定时加载配置文件任务的代码如下:
package com.divitone.util.listener;
import java.io.File;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionServlet;
/**
* 定时加载配置文件任务。
*
*/
public class LoadResourceTimerTask extends TimerTask {
private ServletContext context = null;
public LoadResourceTimerTask(ServletContext context) {
this.context = context;
}
public void run() {
try {
reloadResource(context);
} catch (Exception e) {
}
}
/**
* 得到所有strust配置文件的修改时间戳的和,以此来确定有没有修改过配置文件。
*
* @param FileNames
* 文件名数组。
* @param webRootPath
* 文件路径。
* @return 时间戳的和。
*/
public static long getFileTimestamp(String[] FileNames, String webRootPath) {
long fileTimestamp = 0L;
for (int i = 0; i < FileNames.length; i++) {
String fileName = FileNames[i].trim();
File file = new File(webRootPath, fileName);
if (file.exists())
fileTimestamp += file.lastModified();
}
return fileTimestamp;
}
/**
* 自动重载strust配置文件,
*
* @param context
* @throws javax.servlet.ServletException
*/
public static void reloadResource(ServletContext context)
throws ServletException {
Long currentTimeStamp = (Long) context
.getAttribute("STRUST_CONFIG_FILE_LASTMODIFY");
ActionServlet servlet = (ActionServlet) context
.getAttribute(Globals.ACTION_SERVLET_KEY);
String configString = servlet.getInitParameter("config");
String[] configFileNames = configString.split(",");
String realPath = context.getRealPath("/");
Long strutsConfigTimestamp = new Long(getFileTimestamp(configFileNames,
realPath));
if (currentTimeStamp != null
&& !currentTimeStamp.equals(strutsConfigTimestamp)) {
// 如果不相等重新载入配置文件
servlet.destroy();
context.removeAttribute(Globals.REQUEST_PROCESSOR_KEY);
servlet.init();
}
context.setAttribute("STRUST_CONFIG_FILE_LASTMODIFY",
strutsConfigTimestamp);
}
}
三.在WEB-INF目录下的web.xml中添加如下代码:
<listener>
<listener-class>com.divitone.util.listener.ReloadResource</listener-class>
</listener>
分享到:
相关推荐
jonas_timer-1.4.3.jar 、jotm-1.4.3.jar、jta-1.0.1B.jar
资源分类:Python库 所属语言:Python 资源全名:django-view-timer-0.1.3.macosx-10.10-intel.exe 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
spring与mybatis集成jar包 当前包解决了org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/这个报错问题 mybatis-spring-2.0.2.jar mybatis-3.1.1.jar spring-aop-3.2.16.RELEASE....
Arduino-arduino-timer.zip,用于延迟函数调用的非阻塞库arduino timer-用于延迟函数调用的库,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,深受电子制造商的欢迎,Arduino通过开源系统提供了很多...
jquery.timers-1.2.js 定时器插件
Remanent On-delayTimer for S7-300400.zip西门子PLC编程实例程序源码下载Remanent On-delayTimer for S7-300400.zip西门子PLC编程实例程序源码下载Remanent On-delayTimer for S7-300400.zip西门子PLC编程实例程序...
python库。 资源全名:new_timer-0.0.2-py3-none-any.whl
USB4 Re-Timer Specification Rev 0.96 - 20200615 - CLEAN.pdf
python库。 资源全名:magic_timer-0.0.9-py3-none-any.whl
boost159-timer-1.59.0-2.el7.x86_64.rpm
jquery timer, -.1.2.js
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用