2 Replies Latest reply: Nov 30, 2010 7:53 AM by 800330 RSS

    java.util.TimerTask

    819759
      Hi,

      I'm using timertask to schedule an activity on a daily basis; program is simple. Works finely for seconds, minutes or hours set as initial delay; however, doesn't work when I set next day as the start time. The steps I'm following to test the code is by advancing the system time; seems like it doesn't get picked up. Any light on this problem is greatly appreciated.

      Have tried alternate versions of the same, like passing initial delay in terms of milliseconds and using plain java.util.Date and Calendar.getInstance() etc.,
      Below is the code snippet:

      public final class FetchMail extends TimerTask {

      /**
      * Construct and use a TimerTask and Timer.
      */
      public static void main (String... arguments ) {
      TimerTask fetchMail = new FetchMail();

      //perform the task once a day at 4 a.m., starting tomorrow morning
      //(other styles are possible as well)
      Timer timer = new Timer();
      timer.scheduleAtFixedRate(fetchMail, getTomorrowMorning4am().getTime(), fONCE_PER_DAY);
      }

      /**
      * Implements TimerTask's abstract run method.
      */
      public void run(){
      //toy implementation
      System.out.println("Fetching mail...");
      }

      // PRIVATE ////

      //expressed in milliseconds
      private final static long fONCE_PER_DAY = 1000*1*1*1;

      private final static int fONE_DAY = 1;
      private final static int fFOUR_AM = 4;
      private final static int fZERO_AM = 0;
      private final static int fZERO_MINUTES = 0;

      private static Date getTomorrowMorning4am(){
      Calendar tomorrow = new GregorianCalendar();
      System.out.println("tom" + tomorrow.getTime());

      tomorrow.add(Calendar.DATE, 1);

      Calendar result = new GregorianCalendar(
      tomorrow.get(Calendar.YEAR),
      tomorrow.get(Calendar.MONTH),
      tomorrow.get(Calendar.DATE),
      fZERO_AM,
      fZERO_MINUTES
      );
      System.out.println("tom" + result.getTime());

      return result.getTime();
      }
      }


      Thanks in advance!
        • 1. Re: java.util.TimerTask
          800330
          Hi,

          Your problem is calculating the delay, not necessarily to do with concurrency. Secondly, you may want to use code tags around your snippets of code like [ code ] and [ /code ] (without the spaces) to get nicely formatted source code.

          I "hate" working on pieces of code that depend on the actual system time. To avoid using the system time, I code against an input parameter that represent the current time. At the point where I use the code a call it with the system clock time:
             MyClass.theMethod(new Date());
          or
             MyClass.theMethod(System.currentTimeMillis());
          and there are many variations on this idea (a no-arg method that calls a one arg method that does the real work)

          Perhaps this gives you an idea how to debug your own attempts a bit easier...
          • 2. Re: java.util.TimerTask
            800330
            Took a slightly closer look at your code, so far the only things wrong are the value for fONCE_PER_DAY (24*60*60*1000) and using fZERO_AM instead of fFOUR_AM. I really don't know how fiddling with the system's clock is perceived by Java. Are your changes propogated to the system, did you advance the date too when you go to 4AM, our did you put the clock on today's 4AM?

            But I'd be very willing to say that if this works for 09:45AM when I say so, it works just as well at 04:00AM while I am not watching... (assumptions are the roots of all evil)