在项目中使用到定时任务,除了使用Spring自带的调度之外,还可以使用Servlet上下文实现

一、创建一个Listener 类,实现ServletContextListener 接口,实现接口中的方法:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 侦听器类 和Timer 结合起来定时执行任务
 */
public class ContextListener implements ServletContextListener {
    private java.util.Timer timer = null;
    public void contextInitialized(ServletContextEvent arg0) {
        timer = new java.util.Timer(true);// 创建一个新计时器,指定其相关的线程作为守护程序运行。
        //System.out.println("启动定时器");
        //调度器,EarlyWarningTask() 为自定义调度任务
        //初始化时就执行一次任务
        timer.schedule(new EarlyWarningTask(),10, 60*60*1000);//执行任务前延迟10毫秒,执行任务的间隔60*60*1000毫秒
        //System.out.println("调度任务添加完成");
    }
    public void contextDestroyed(ServletContextEvent arg0) {
        timer.cancel();
        //System.out.println("定时任务销毁");

    }

}

二、获得Spring 的ApplicationContext配置信息,以便在调度任务中读取配置文件中的相关业务方法

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppContext {
    private static AppContext instance;

    private AbstractApplicationContext appContext;

    public synchronized static AppContext getInstance() {
        if (instance == null) {
            instance = new AppContext();
        }
        return instance;
    }

    private AppContext() {
        this.appContext = new ClassPathXmlApplicationContext(
                "/applicationContext.xml");
    }

    public AbstractApplicationContext getAppContext() {
        return appContext;
    }
}

三、自定义调度任务类,继承TimerTask 类

import java.util.TimerTask;

public class EarlyWarningTask extends TimerTask {
    private static final int C_SCHEDULE_HOUR = 23;//执行时间23时
    private static boolean isRunning = false;
    //通过AppContext 类,获得配置文件中相关业务方法
    //inStorageDetailImp
    protected IInStorageDetailService getInStorageDetailService(){
        return (IInStorageDetailService)AppContext.getInstance().getAppContext()
        .getBean("inStorageDetailImp");
    }
    //    goodsWarningImp
    protected IGoodsWarningService getGoodsWarningService(){
        return (IGoodsWarningService)AppContext.getInstance().getAppContext()
        .getBean("goodsWarningImp");
    }
    //    lendOutAccountImp
    protected ILendOutAccountService getLendOutAccountService(){
        return (ILendOutAccountService) AppContext.getInstance().getAppContext()
                .getBean("lendOutAccountImp");
    }
    //lendOutWarningImp
    protected ILendOutWarningService getLendOutWarningService(){
        return (ILendOutWarningService)AppContext.getInstance().getAppContext()
        .getBean("lendOutWarningImp");
    }
    //任务在run()方法中实现
    @Override
    public void run() {
        Calendar cal = Calendar.getInstance() ;
        if(!isRunning){
            if(C_SCHEDULE_HOUR==cal.get(Calendar.HOUR_OF_DAY)){//HOUR_OF_DAY 用于 24 小时制时钟
                isRunning = true;
                System.out.println("执行定时任务");
                //任务代码
                //…………………………
                isRunning = false;
                System.out.println("定时任务结束");
            }else {
                System.out.println("未到定时任务时间");
            }
        }else{
            System.out.println("上一个定时任务未结束");
        }
    }

四、web.xml配置监听

<listener>
        <listener-class>com.cl.gdb.util.ContextListener</listener-class>
</listener>