I have got a requirement in which i need to execute a function at specified time set by the user.The function contains the code to generate a pdf file from database and save it into local drive of the machine.. Here the time needs to be set by the user and it may be changed depending on his requirement.
I have never used timer class..
I have a code snippet which i got from google but i am not getting how to set the user specific time details for function execution in this code ..
Here is the code snippet..
class ReportGenerator extends TimerTask {
public void run() {
System.out.println("Generating report");
//TODO generate report
}
}
class MainApplication {
public static void main(String[] args) {
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
date.set(
Calendar.DAY_OF_WEEK,
Calendar.SATURDAY);
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 ReportGenerator(),
date.getTime(),
1000 * 60 * 60 * 24 * 7);
}
}
I am also not getting the meaning of this code snippet..what does this means??
timer.schedule(
new ReportGenerator(),
date.getTime(),
1000 * 60 * 60 * 24 * 7);
}
In the above code snippet the given function will execute on every saturday .
Please help me .Any idea is heartely welcomed ... Thanks in advance..
Okay, so I looked a bit into the Timer class and found your approach to be almost correct. You'll need to edit your code a little bit to get the wanted functionality.
This will start a timer that launches the "ReportGenerator" at the stated time. You'll have to figure out yourself how to get the input from the user (should be fairly simple!)
There is a good library called quartz that I can recommend. Its main purpose is starting jobs at a given time.