`
highping
  • 浏览: 44124 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类

使用Timmer定时加载Struts的struts-config.xml文件

阅读更多

        在做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> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics