欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
    }
}

 

相关标签: Java

上一篇:

下一篇: