Here is my example of using the TimerTask
class and my problem is as follows:
I want to make this routine automatically every 12 hours. Currently nothing is happening, is something missing? And this class just give a tomcat deploy that works without instantiating it?
public class PausarTempo{
//INTERVALO DE TEMPO EM MILESEGUNDOS REFERENTE A 24 HORAS
public static final long TEMPO = (1000*60*60*20);
public void pararTempo(){
//definindo a hora qua a tarefa sera executada pela primeira vez
Calendar dataHoraInicio = Calendar.getInstance();
dataHoraInicio.set(Calendar.HOUR_OFDAY,12);
dataHoraInicio.set(Calendar.MINUTE,0);
dataHoraInicio.set(Calendar.SECOND,0);
Timer timer = null;
if (timer == null) {
timer = new Timer();
TimerTask tarefa = new TimerTask() {
@Override
public void run() {
try {
System.out.println("Començando...");
// MINHA REGRA
System.out.println("Fim.");
} catch (Exception e) {
e.printStackTrace();
}
}
};
timer.scheduleAtFixedRate(tarefa, dataHoraInicio.geteTime(), TEMPO);
}
}