Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Scheduling Tasks in Java

Options
  • 22-11-2006 8:11pm
    #1
    Registered Users Posts: 1,112 ✭✭✭


    Hi,
    In works I had to create an application that would execute a task every 6 hours (configurable). The task itself was quite complex, but in terms of the scheduling didn't matter.
    My task extended TimerTask and I used Timer to start it.
    So my code looked like

    Timer t = new Timer("ENIQ-M Scheduler", true);
    //This read as, start the ScheduledTask, starting now, every 2 minutes
    t.scheduleAtFixedRate(new ScheduledTask(), 0, 60000);

    (I know the comment and code actually say 2 minutes, but that was for testing purposes, I wasn't going to wait 6 hours until the next execution)

    So, this would execute now (ie at startup), and then wait 2 minutes before executing again.

    Great.

    Now they want me to implement it in this way

    Execute now(at startup) and then every six hours aligned to midnight (ie 0000, 0600, 1200, 1800) or whatever depending on what the 6 hours is configured to.

    So that means if we startup at 1755, we have to run now, at 1800, 0000, 0600 etc, or if we start at 0001 we run now and at 0600, 1200, 1800 etc.

    This is driving me demented, such a stupid requirement. Anybody have any ideas on how this could be implemented simply.
    And I am not looking for actual code, although that would be nice, but a simple algorithm would be nice either.

    Thanks
    Ken


Comments

  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    I would run a task every 15 seconds or so, and check the system time, and if the time equals 00:00 or 6:00 ect.. run the main task...

    The task that checks the time should be very small, and as 99.99% of the time it's just checking the system time every 15seconds, it shouldn't take up any real amount of CPU resources...


  • Registered Users Posts: 4,188 ✭✭✭pH


    Have a look at this
    http://www.javapractices.com/Topic54.cjp

    Surely if you replace the get getTomorrowMorning4am() method with something that figures out the next 8 hour time boundary and use an 8 hour time it's done?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    As part of the "finishing" of your task, put in a block of code to make it reschedule itself (if needed).

    So if i ran the program right now, it would do it's thing immediately. In the closing steps of it doing whatever it does, it would call a method called "Reschedule()" which would check if the task needs to be rescheduled, if it does need to be rescheduled the method would then schedule the task to run at the next time interval of 6 hours (or whatever).

    Figuring out the time it needs to run at should be easy enough. Just check the current hour that you're on (and minute maybe) and then you know your boundaries are at 00:00, 06:00, 12:00, 18:00 and 24:00, so check which boundary happens next and schedule for that time.

    That should make things easy.


Advertisement