项目要求做个定时器,可是该怎么执行呢,是tomcat已启动就执行还是其他呢,如果已启动就执行,那启动多次不就执行多次了么要求的效果是:一天只执行一次有经验的高手说说怎么弄吧,最好详细的,新手,还有好多东西要学呢,感谢 java

解决方案 »

  1.   

    schedule(TimerTask task, long delay)的注释:Schedules the specified task for execution after the specified delay。大意是在延时delay毫秒后执行task。并没有提到重复执行schedule(TimerTask task, long delay, long period)的注释:Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay。大意是在延时delay毫秒后重复的执行task,周期是period毫秒。
      

  2.   

    看我博客   http://bbs.csdn.net/topics/390389840?page=1#post-393891876
      

  3.   

    是否使用框架   如果有用spring  可以用spring的定时器
    想自己写的话 就自己写吧启动项目就加载
      

  4.   

    使用的Spring框架的话,就是用Spring内定时器把:TimerTask或者Quartz ,用法可见我的博客:http://blog.csdn.net/zhujianpengzha/article/details/8140419
    http://blog.csdn.net/zhujianpengzha/article/details/8140372如果没用框架,可以使用schedule
      

  5.   

    web.xml里面配个listen ,用来程序启动触发定时器,
    <listener>
           <listener-class>com.whty.business.JobTimer</listener-class>
      </listener>
    JobTimer类实现 ServletContextListener接口,其中有个初始化方法,在里面调用你用TimerTask写的定时器即可 //time1,启动时的延时;time2每隔多长时间重复执行一次
            timer.schedule(task, time1, time2);
    如果是每天10点执行一次,可以在TimerTask run()中判断当前小时 是否是 10点即可,
    Calendar calendar = Calendar.getInstance();
                 int hour = calendar.get(Calendar.HOUR_OF_DAY);
                 if(hour==10){
                    //调用你的定时程序                }