Solved

How to schedule a task using Java

Posted on 2008-06-25
7
1,045 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:arjunarajan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 21863598
You could take a look at Opensymphony Quartz

http://www.opensymphony.com/quartz/
0
 
LVL 3

Expert Comment

by:shaz_
ID: 21863641
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
0
 
LVL 4

Accepted Solution

by:
aman123_123 earned 400 total points
ID: 21864308
Hi,

From what you have written I can say that you can make this a seperate application altogether. This application need not plug-in to your already running Stuts application.
You can use the Java Timer and Timer task classes to achieve the same.
For example the following code schedules to run every Sunday midnight.
You can also make this configurable.


import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
 
public class ReadFileAndUpdateDb extends TimerTask {
 
  public void run() {
    //TODO  Code To  Read File And Update Db 
 }
 
}
 
class MainApplication {
 
  public static void main(String[] args) {
    Timer timer  new Timer();
    Calendar date = Calendar.getInstance();
    date.set(
      Calendar.DAY_OF_WEEK,
      Calendar.SUNDAY
    );
    date.set(Calendar.HOUR, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    // Schedule to run every Sunday in midnight
    timer.schedule(
      new ReadFileAndUpdateDb (),
      date.getTime(),
      1000 * 60 * 60 * 24 * 7
    );
  }
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:arjunarajan
ID: 21873180
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
0
 
LVL 4

Expert Comment

by:aman123_123
ID: 21881005
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

0
 
LVL 4

Expert Comment

by:aman123_123
ID: 21881026
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.
0
 

Author Closing Comment

by:arjunarajan
ID: 31470476
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.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 41
jsp insert to database example 2 59
Selenium docs api java index 3 64
Bot application - advice 3 59
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question