I have to invoke a method after every 24 hours, how do i do it. I will get the last invoke time for this method from the repository and if the current system time is greater than 24 hours, this method will be invoked. Do i need threads or there is some other class like cron jobs in Unix i can implement?
There is no cron-type scheduler in standard Java that I know of.
You can store the time the methodwas last invoked, and then compare that time to the current time; I would myself store and compare the time as a long of milliseconds, I think it just makes it easier. Then see if the difference is greater than 24*60*60*1000.
You only need thread if you have some independent bit of computing you want done while other things are going on. If you have a long-running method, for instance, and want to have something else done every 24 hours regardless of what it is doing, then you could have a thread computed how long it should have to wait from being started before it should wake up and execute, and then do a Thread.sleep() for that long, execute and repeat. So whether you need a thread depends on what else your program is doing.
As per what I understood, you want to run a task every 24 hours.
If you use inbuilt java scheduling, you don't need to store the time in repository. It will automatically do the job for you.
In essence, the "java scheduler = TimerTask + Timer;"
'TimeTask' is an abstract class which you extend in the class which does the processing.
'Timer' is the class which you will use in your scheduler class.
I just made a sample example for you to check:
Here you can make changes in the execution from 3 seconds to 24 hours.
the other option is Quartz scheduling: An open source API for scheduling, provides more features.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Sep 30, 2009
I have just set up the two example classes in my development environment and nothing is happening.
Not sure where I am going wrong here? As based on the explanations this should all work correctly since the class is called by the TimerTask automatically....but this doesn't seem to be working?
What I have had to do to get the above working is to initialise and call the Scheduler class from another class that has been accessed. Which is kind of defeating the point of having a scheduler (using this method).
Based on this, am I correct in saying that the only way to create some kind of cron job (similar principal) is to use Quartz? As I want the job to run totally independent of anything else, except the system clock.
Not quite sure which of the two files that would go into? As it appears to be doing a similar task (from a single file though) - which would still encounter the same issue of the class having to be called in the first place.
Tried adding the script into both files (one at a time) and the only way it would work was if I called the class from another class that was being run manually, which defeats the whole point.
I wrote that code before you edited your post but a different parts belong in different files.
Creating a Scheduler class is not defeating the point. That way you can hide how you implemented it.
So no creating something like a cron job isn't only possible using Quartz.
Joined: Sep 30, 2009
Can you be a little more descriptive on how this works please, as I am still not understanding how this works behind the scenes. (or point me in the direction of some reading so I will understand this more).
Incidentally I have found another solution to what I required, but I would still like to understand what has been discussed as I will no doubt need this at some point in the future.
If you write a Scheduler class which allows the application to schedule events then that hides the implementation details.
Internally you could use Quartz of java.util.Timer or write something yourself. That isn't visible for the application.