JAVA定时执行

LoadResourceTimerTask.java
package cobble.ge.tools;


import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;


/**
 * 定时加载配置文件任务。
 *
 */
public class LoadResourceTimerTask extends TimerTask {

    private ServletContext context = null;

    public LoadResourceTimerTask(ServletContext context) {
        this.context = context;
    }

    public void run() {
        try {
            reloadResource();
        } catch (Exception e) {
        }
    }

    /**
     * 自动重载strust配置文件,
     *
     * @param context
     * @throws javax.servlet.ServletException
     */
    public static void reloadResource()
            throws ServletException {
        System.out.println("定时执行,开始时间为: " + new Date());
        try {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            System.out.println("当前 年=" + year + " 月=" + month);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("定时执行,结束时间为: " + new Date());
    }
}

ReloadResourceListener.java
package cobble.ge.tools;

import java.util.Calendar;
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运行一次
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(year, month, day, 10, 00, 00);
        loadResource.scheduleAtFixedRate(loadResourceTimerTask, calendar.getTime(), PERIOD);
    }

    /**
     * 实现ServletContextListener的contextDestroyed方法。
     *
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
    }
}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <listener>
        <listener-class>cobble.ge.tools.ReloadResourceListener</listener-class>
    </listener>
</web-app>

你可能感兴趣的:(java)