Two Laptop Bag*
The moose likes Java in General and the fly likes need an application to run at  a particular moment in the day Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need an application to run at  a particular moment in the day" Watch "need an application to run at  a particular moment in the day" New topic
Author

need an application to run at a particular moment in the day

Sarah Gaikwad
Ranch Hand

Joined: Feb 26, 2004
Posts: 35
Hi,
I need to schedule a java application to run at a particular point in the day. I am aware of the Timer & Timer task classes,but am not sure/confident abt using it.
If anyone of a good way of doing it plz do let me know
i desperately need to know
thanks in advance
Sharun
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140
Don't know if this is just for running on Windows? If it is just add it as a Scheduled Task in the OS.
Sarah Gaikwad
Ranch Hand

Joined: Feb 26, 2004
Posts: 35
i actually need my class file to run at a specific time during the day.
Steve Leach
Ranch Hand

Joined: Sep 24, 2003
Posts: 46
You could give your java class a "main()" method, and then invoke it using the OS scheduler as an application.
Alternatively, have an application that creates a new thread. The thread will check the time and then sleep for a while. If the time is appropriate it can do it's stuff. If you do this, you should set a "date last run" variable so that it doesn't run twice on the same day.
Does this help ?
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
I recently started using a 3rd party library called Quartz and it is GREAT!
http://www.quartzscheduler.org/quartz/
It's free, open-source, and is VERY well documented. I highly recommend it. In your case, you would just have to create a new class that extends Job and in the execute() method does your work or just calls yours class. Then you setup a Trigger with the settings for the time you want it to run. Then you add those to the Scheduler tell the Scheduler to start.
It's really simple - give it a try. Read the tutorial they have online.
brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

This is a feature of the operating system. Otherwise you program must run continuously, and only fire certain methods at the appointed time.
Both windows and Linux have facilities to run programs at certain times. That is what you need.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Write a program to run as follows:
1) Calculate the difference between the start time and the current time
2) Sleep for that amount of time
3) Start doing its thing.
Geoffrey


Sun Certified Programmer for the Java 2 Platform
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Geoffrey Falk:
Write a program to run as follows:
1) Calculate the difference between the start time and the current time
2) Sleep for that amount of time
3) Start doing its thing.
Geoffrey

Not the preferred way to do this. Preferred way is to use the OS facilities. That way you have better error handling and consistency. Your program must keep checking the clock else your time count will be off when your program gets preempted. No need to keep program in RAM when its not needed. TSRs sucketh.
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
I agree, leaving a Java programme running constantly in order that it can do something at a particular time is not really good practice. If you're using windows for example, write your Java class complete with a main() method.
Then you can simply create a .bat file containing the java command to run the application.

Now in windows, using 'scheduled tasks' you can set the batch file to run once a day or whatever and your Java program will run.
I think


SCJP 1.4, www.gsi3d.org.uk
frisode jonge
Ranch Hand

Joined: Dec 30, 2003
Posts: 34
Depends which environment this needs to run. On backend unix systems, it usually works with a cronjob. just do a google search for this. This can run at any time you want.
Then create an executable jar, with the right manifest and main class and there you go....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need an application to run at a particular moment in the day
 
Similar Threads
TimerTask Execution
What Platform/Java Technology - Please Help
Timer IllegalStateException
Make sure that Timer run everyday at a Specific Time
Timer in java