Link to home
Start Free TrialLog in
Avatar of arjunarajan
arjunarajanFlag for India

asked on

How to schedule a task using Java

Hi Experts,

I have an application that gets inputs from some input files stored in a location and updates the DB. Now, I want to make this job using scheduler.

The important thing here is the schedule should be modifiable by an admin and the task must run at the scheduled time.

The application already existing is developed using Struts. The scheduler program should be a plug-in and should not affect other part of the application.

Can anyone please provide some code snippets and tell me how to do this.

Thanks.
Arjuna Rajan Rameshbabu
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

You could take a look at Opensymphony Quartz

http://www.opensymphony.com/quartz/
Avatar of shaz_
shaz_

yeh like bart has already suggested pensympony quartz.. this blog tutorial seemed easy to read for me when i first tried it

http://gadjah-mada.blogspot.com/2006/06/opensymphony-quartz-on-tomcat-and.html
ASKER CERTIFIED SOLUTION
Avatar of aman123_123
aman123_123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arjunarajan

ASKER

Hi Aman,

Thank you for your response.

I have a program that does the DB updating part. Now, I want the same to be scheduled and also want it as a separate module as it is now.

How can I do this if I use the TimerTask since the TODO part comes inside the 'run()' method.

Also, how can I make the schedule to be modified.

Can you pls. give me a simple example on this.

Thanks,
Arjuna Rajan Rameshbabu
The folling code will read from the configuration (properties) file.
class MainApplication {
 
  public static void main(String[] args) {
    Timer timer  new Timer();
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
        DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
        Date date = (Date)formatter.parse("properties.getProperty("starttime")");
        int interval = 1000 * 60 * 60 * Integer.parseInt(properties.getProperty("interval"));
   // Schedule to run on specified time
    timer.schedule(
      new ReadFileAndUpdateDb (),
      date.getTime(),      
    );
 
    } catch (IOException e) {
    }
    } catch (ParseException e) {
    }
 
   }
}

Open in new window

I forgot to mention that you will have to create a properties file which will contain the following entries.

starttime=//When you want your task to run for the first time.
interval=//How frequently you want the task to run

>> I have a program that does the DB updating part
If you call the function inside run method of ReadFileAndUpdateDb class. It will be scheduled the way you want it to.
I thank 'aman123_123' for his excellent support for the questions I asked on the solution he provided. I am very happy with the solution.

Thank you once again.