• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to i invoke a method every 24 hours

 
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use Quartz Scheduler you can use a cron-like trigger.
 
Greenhorn
Posts: 9
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The run method will be invoked by the Timer.
 
Michael Cropper
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try something like this:

 
Michael Cropper
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic