0

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..

Adi
  • 1,395
  • 11
  • 37
  • 61
  • What's the problem that you face with this code? What does the user input look like? –  Nov 09 '13 at 11:29
  • @NishantShreshth how to use the user input timing details to execute the function – Adi Nov 09 '13 at 11:32

2 Answers2

2

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.

Calendar calendar = Calendar.getInstance();
date.set(Calendar.DAY_OF_WEEK, getUserInputDay()); //
calendar.set(Calendar.HOUR_OF_DAY, getUserInputHour());
calendar.set(Calendar.MINUTE, getUserInputMinute());
calendar.set(Calendar.SECOND, getUserInputSeconds());
Date time = calendar.getTime();

timer = new Timer();
timer.schedule(new ReportGenerator(), time);

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!)

Chikilah
  • 490
  • 2
  • 8
  • Thank you sir for ur answer i a doubt on this code snippet..1000 * 60 * 60 * 24 * 7); what does this code do in the given code..Please see my second code snippet in the post – Adi Nov 09 '13 at 15:25
  • 2
    Basically all that little snippet does is take int value, turn it into a second -> a minute -> an hour -> a day -> 7 days (So a week). My guess is it takes the current date and adds 7 days to it, making the timer be 7 days. – Chikilah Nov 09 '13 at 15:50
  • @Chikilah a nitpick for sure, but won't this code use the current milliseconds offset instead of the exact user entered time? For instance if `Calendar.getInstance()` returns time 2013-11-22 09:25:04.732 then the .732 will be preserved. – Russell Silva Nov 22 '13 at 17:25
  • Nice catch - Yes, the milliseconds will be preserved in this case. To get around this you would simply have to set the milliseconds to 0 as well, like so: calendar.set((Calendar.MILLISECOND, 0); – Chikilah Nov 24 '13 at 12:35
1

There is a good library called quartz that I can recommend. Its main purpose is starting jobs at a given time.

phlogratos
  • 13,234
  • 1
  • 32
  • 37