`
hanxinyu
  • 浏览: 127066 次
  • 来自: henan china
社区版块
存档分类
最新评论

java定时器

阅读更多

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

分享到:
评论
1 楼 wangqichn 2008-12-09  
这个东西,估计也只能留在自己的博客里面了。太不整齐啦 !

相关推荐

Global site tag (gtag.js) - Google Analytics