java定时器
程序员文章站
2023-12-21 21:43:28
...
public class TimerListener implements ServletContextListener
{
private Timer containerTimer = null;
/**
* 定时器的执行
*/
public void contextInitialized(ServletContextEvent e)
{
try
{
e.getServletContext().log("定时器已经启动");
this.containerTimer = new Timer();
this.containerTimer.schedule(BusiContainer.getInstance(),
new Date(), (long) (30 * 1000));//系统从启动开始,每个一个小时执行任务一次
e.getServletContext().log("已经添加任务调度");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/**
* 定时器的销毁
* @param e
*/
public void contextDestroyed(ServletContextEvent e)
{
containerTimer.cancel();
e.getServletContext().log("定时器销毁");
}
}
public class BusiContainer extends TimerTask
{
private static BusiContainer busiContainer = null;
private BusiContainer()
{
}
public void run()
{
// try {
// AutoProcessSalesDataByPos salesProcess = new AutoProcessSalesDataByPos();
// salesProcess.dataProcess();
// } catch (Exception e) {
// e.printStackTrace();
// }
//
try
{
System.out.println("线程退货数据开始执行**********************************");
ReturnOrderPosJC returnPos = new ReturnOrderPosJC();
returnPos.dataProcess();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static BusiContainer getInstance() throws Exception
{
if (busiContainer == null)
{
busiContainer = new BusiContainer();
}
return busiContainer;
}
}