*
The moose likes Beginning Java and the fly likes How to i invoke a method  every 24 hours Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to i invoke a method  every 24 hours" Watch "How to i invoke a method  every 24 hours" New topic
Author

How to i invoke a method every 24 hours

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hi Folks

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?

Thanks
Adi
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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.

rc
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
If you use Quartz Scheduler you can use a cron-like trigger.
Deep Purohit
Greenhorn

Joined: Mar 29, 2010
Posts: 9

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:

Processing class

Scheduler class

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.
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Thanks Guys
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Following on from this thread, as I am looking to do the exact same thing.

Just have one question from the information posted by Deep Purohit which is, how would this class be called?

Quartz seems like a full solution, but may be overly complex to setup for my requirements.

Thanks
Michael
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The run method will be invoked by the Timer.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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?

Edit:
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.

Thanks
Michael
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Try something like this:

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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.

Thanks
Michael

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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.

Thanks
Michael
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to i invoke a method every 24 hours