Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Java method at a set time each day

I'm relatively new to Java and I've pick up a project to work on. However, I've run into a block. I need a method to run at a certain times throughout the day. I've done quite a bit of searching but I can't find anything that seems like it would do the trick. I've run into the Timer class but it appears to run at certain intervals. The Scheduler class, appeared to have the same issue. I also came across Quartz but I think I need something more lightweight and I could only see how to do things at intervals.

Perhaps, just because I'm new, I've missed some things that could help me in these classes, but I'm really stuck and could use some help.

If someone could point me to a class that will run something at a certain time of day, everyday (bonus points for being able to cancel the event), and show me how to correctly use the class, that would be awesome!

TL;DR: Need a class that does something at a time of day, not at an interval because the program may be restarted multiple times throughout the day.

like image 499
Austin Moore Avatar asked Aug 06 '11 06:08

Austin Moore


People also ask

How do I schedule a task at a specific time?

One of the methods the Timer class is void scheduleAtFixedRate(TimerTask task, Date firstTime, long period). This method schedules the specified task for repeated fixed-rate execution, beginning at the specified time.

How do you run a specific task everyday at a particular time using ScheduledExecutorService?

getSeconds(); ScheduledExecutorService scheduler = Executors. newScheduledThreadPool(1); scheduler. scheduleAtFixedRate(new MyRunnableTask(), initialDelay, TimeUnit. DAYS.

How do you calculate time taken to run a method in Java?

The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method. The nanoTime() method returns the current time in nano seconds.


2 Answers

try the TimerTask class

for more info check out http://oreilly.com/java/archive/quartz.html

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public 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.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 ReportGenerator(),
      date.getTime(),
      1000 * 60 * 60 * 24 * 7
    );
  }//Main method ends
}//MainApplication ends
like image 57
dov.amir Avatar answered Oct 16 '22 16:10

dov.amir


I'd strongly suggest, if at all possible, that the best approach would be to invoke your script or Java application using your OS's scheduler: for example, "cron" in *nix, or "task scheduler" in Windows.

like image 5
paulsm4 Avatar answered Oct 16 '22 16:10

paulsm4