How to schedule a Java Program for Background processing
Dear Forum Members,
Can anybody help me to schedule a Java program in background? That is, for example, I want to run a Java Program every day once at 3 AM (early in the morning).
Kindly tell me process involved to achieve this.
Thanks and regards,
Ranga
Java Programmer
Re: How to schedule a Java Program for Background processing
Well, do you mean you want to create a cron job that runs the actual java program every so often or do you mean you want to start a java program that stays up and running all the time but only executes a certain block of code every so often?
// Json
Re: How to schedule a Java Program for Background processing
We11 !
I will explain the scenario. I have written a data download program in Java to download records of tables from a database to text files. Writing the download program is not a issue. But if I execute the program, it takes more than half-an-hour to finish executing due to data volume in tables. In fact, we want to download 15 tables from the database to text files. We cannot give this program to the end-user to run in foreground.
To sort out this, I want my Java Program to start running automatically everyday once at 3 AM or 4 AM or 11 PM so that data download happens without end-user manually executing it.
How can I schedule my data download Java program in background so that everyday we can ensure that all records from those 15 tables are downloaded without manually executing the program.
We request you to help us with appropriate process of implementing this business requirement.
Thanks and regards,
Ranga.
Re: How to schedule a Java Program for Background processing
In that case I take it you should run a cron job.
https://help.ubuntu.com/community/CronHowto
That page there explains how to get up and running using crontab. Dont know what operating system you are running on though, but if you're on windows I'm sure there is a similar solution to setting up timed tasks, maybe even easier.
// Json
Re: How to schedule a Java Program for Background processing
There are two simple methods:
1. Use the native OS's task scheduler (if there is one) to start the task at the specified time. A quick google search for which OS you are interested in should do the trick here. This will not require your program to be constantly running in the background (though it is in a sleep state, it is still using up recources), and depending on the OS/implementation of task scheduling software, you can even get the OS to power-up the system and perform the task required.
2. Use the Java Timer class to schedule tasks at either certain time intervals or at certain dates/times. Note that I think this method requires your Java program be running in the background constantly, but don't quote me on this.
Re: How to schedule a Java Program for Background processing
Thanks a lot for your suggestion. I will also search in Google to find any better way of doing it.
Regards,
Ranga.