1主方法 服务器启动时开始调用启动
package com.casco.csmis.szdc.jxkp.task;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 类说明:
*
* @ClassName: 码灵
* @author ht_wangjun 在 Servlet API 中有一个 ServletContextListener 接口,它能够监听
* ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期。 当Servlet 容器启动或终止Web
* 应用时,会触发ServletContextEvent 事件,该事件由ServletContextListener 来处理。在
* ServletContextListener 接口中定义了处理ServletContextEvent 事件的两个方法。
*/
public class PerformanceTaskListener implements ServletContextListener {
Log logger = LogFactory.getLog(PerformanceTaskListener.class);
@Override
public void contextDestroyed(ServletContextEvent arg0) {
logger.debug("tomcat 开始结束");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
logger.debug("tomcat 开始");
Timer timer = new Timer();
CreatePerformanceTask task = new CreatePerformanceTask(arg0);
// 这里每天执行一次,首次执行时间为200秒后
timer.schedule(task, 1000 * 200, 1 * 24 * 3600 * 1000);
}
}
2执行方法
package com.casco.csmis.szdc.jxkp.task;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.casco.csmis.szdc.jhgl.utils.CalUtils;
import com.casco.csmis.szdc.jxkp.dao.PerformanceInfoDao;
import com.casco.csmis.szdc.jxkp.model.PerformanceInfo;
/**
* 类说明: 定时器
* @ClassName: CreatePerformanceTask
*/
@Component
public class CreatePerformanceTask extends TimerTask {
/**
* 引入dao层
* @param arg0
*/
private PerformanceInfoDao performanceInfoDao;
public CreatePerformanceTask(ServletContextEvent arg0) {
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
this.setPerformanceInfoDao((PerformanceInfoDao)ctx.getBean("performanceInfoDao"));
}
@Override
public void run() {
System.out.println("自动生成开始----------------------------:");
String pararm;
List<PerformanceInfo> infoList = performanceInfoDao.findPerformanceListByYearsAndQuarter(pararm);
}
public PerformanceInfoDao getPerformanceInfoDao() {
return performanceInfoDao;
}
public void setPerformanceInfoDao(PerformanceInfoDao performanceInfoDao) {
this.performanceInfoDao = performanceInfoDao;
}
}
3配置: