There are several ways to solve this problem by considering a system 100% back end.
Use the system scheduler
Windows has a very flexible and powerful Task Scheduler. It may be simpler to configure it to call your program via command line at scheduled times.
Using the Java scheduler
If you want to schedule via Java yourself, use an API class that already does this for you. No need to count milliseconds, seconds, minutes and hours.
See, for example, the documentation of ScheduledExecutorService
. There is everything and even a functional example, which I adapted in the code below:
public class BeeperControl {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
//beep task
final Runnable beeper = new Runnable() {
public void run() {
System.out.println("beep");
}
};
//beep each hour
final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 0, 1, TimeUnit.HOURS);
//cancel beep task
final Runnable canceler = new Runnable() {
public void run() {
beeperHandle.cancel(false);
}
};
//stop beep after 1 day
scheduler.schedule(canceler, 1, TimeUnit.DAYS);
}
public static void main(String[] args) {
new BeeperControl().beepForAnHour();
}
}
Let’s look at the code above:
- The line
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1)
calls a method Factory to create a scheduled service performer (translation of class name).
- Within the method
beepForAnHour
, the first block declares an implementation of Runnable
calling for beeper
. This is the same API that implements threads. This one specifically prints beep
on the console.
- The command
scheduler.scheduleAtFixedRate(beeper, 0, 1, TimeUnit.HOURS)
is the most interesting here. It schedules the execution of our beeper (beeper) at regular intervals of one hour (parameters 3 and 4), the first execution being immediate (second parameter).
- The next section creates another
Runnable
called canceler
. This will be responsible, when executed, for canceling the periodic schedule previously made.
- The command
scheduler.schedule(canceler, 1, TimeUnit.DAYS);
schedule a single execution of our canceler
to exactly one day later. Then, the next day, the beep
will stop.
Critical scenario
In the case of scheduled tasks that are critical for the business, it is interesting to replicate the program on multiple servers. If one of them fails, the others will be able to perform the critical routine. However, if the processing is heavy, it is not convenient for it to run redundantly, that is, it runs multiple times on these different servers.
For such cases, there are scheduling libraries like the Quartz which have specific settings for clusters servers. That way, you can do the deploy application on several servers and, in this example, Quartz will manage the execution, ensuring that your task will be executed properly once.
Even with a single server, Quartz can ensure that a lost run when the server is in service will resume once the server is available.
I think I got something, but just ask me a question.
public static final long TEMPO = (1000 * 60 *60 );
that equals 1 hour?– DiegoAugusto
That’s an hour in milliseconds.
– bfavaretto
But why don’t you create a program that does the task once, and schedule it on the server to run hourly? See http://answall.com/questions/41275/tarefas-agendadas-na-web
– bfavaretto