A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

web.xml

<listener>
        <listener-class>com.hw.util.BeginRun</listener-class>
    </listener>

/**
* @program: Maven
* @description: 项目启动就执行的类
* @author: hw
**/
public class BeginRun implements ServletContextListener {
    java.util.Timer timer = Time.getSingle();

    //启动执行   
    public void contextInitialized(ServletContextEvent event) {
        timer = new java.util.Timer(true);
        //设置时间格式
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //设置从此刻时间开始  建议设置未来时间,不然项目启动的话就会先执行一次
            Date myDate2 = dateFormat2.parse("2018-11-23 08:43:00");
            //第二个参数为从这个时间开始,第三个参数为间隔的时间,NFDFlightDataTimerTask为执行具体方法的类
            timer.scheduleAtFixedRate(new NFDFlightDataTimerTask(event.getServletContext()), myDate2, 60 * 60 * 24 * 7 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    //销毁执行的方法
    public void contextDestroyed(ServletContextEvent event) {
        timer.cancel();
        System.out.println("*************定时器销毁");
        event.getServletContext().log("定时器销毁");
    }
}

class Time {
    private Time() {
    }
    private static java.util.Timer timer = null;

    public static java.util.Timer getSingle() {
        if (timer == null) {
            timer = new java.util.Timer();
        }
        return timer;
    }
}
NFDFlightDataTimerTask类

public class NFDFlightDataTimerTask extends TimerTask {

private ServletContext context = null;
public NFDFlightDataTimerTask(ServletContext context) {
        this.context = context;
    }
/**
     * @Description: 执行的方法
     * @Param: []
     * @return: void
     * @Author: hw
     * @Date: 2018/11/25
     */
    public void run() {

}

---------------------
【转载】
作者:beyond丿灬
原文:https://blog.csdn.net/qq_41594146/article/details/84494611


2 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马