Como executar determinada tarefa todos os dias em um determinado momento usando o ScheduledExecutorService?
Eu estou tentando executar uma certa tarefa todos os dias às 5 da manhã. Então eu decidi usarScheduledExecutorService
para isso, mas até agora eu tenho visto exemplos que mostra como executar tarefas a cada poucos minutos.
E não consigo encontrar nenhum exemplo que mostre como executar uma tarefa todos os dias em uma determinada hora (5h da manhã) e também considerar o horário de verão também -
Abaixo está o meu código, que será executado a cada 15 minutos -
public class ScheduledTaskExample {
private final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
public void startScheduleTask() {
/**
* not using the taskHandle returned here, but it can be used to cancel
* the task, or check if it's done (for recurring tasks, that's not
* going to be very useful)
*/
final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(
new Runnable() {
public void run() {
try {
getDataFromDatabase();
}catch(Exception ex) {
ex.printStackTrace(); //or loggger would be better
}
}
}, 0, 15, TimeUnit.MINUTES);
}
private void getDataFromDatabase() {
System.out.println("getting data...");
}
public static void main(String[] args) {
ScheduledTaskExample ste = new ScheduledTaskExample();
ste.startScheduleTask();
}
}
Existe alguma maneira, eu posso agendar uma tarefa para executar todos os dias 05:00 da manhã usandoScheduledExecutorService
considerando o fato do horário de verão também?
E tambémTimerTask
é melhor para isso ouScheduledExecutorService
?