1
I have a TimerTask
which calls a method every 1 minute, but the method is called twice. This method checks some factors and sends an email, as the method is called twice the email is sent twice. Does anyone know where the problem might be?
private void tarefa() {
new Timer().schedule(
new TimerTask() {
@Override
public void run() {
alertaGeral();
System.out.println("Alertando..");
}
}, 0, 1000 * 60 * 1);
}
Method that is called in the TimerTask
:
private void alertaGeral() {
try {
DateFormat df = new SimpleDateFormat("HH:mm");
String hora = df.format(new Date());
SimpleDateFormat formatador = new SimpleDateFormat("HH:mm");
Date hour = formatador.parse(hora);
Time time = new Time(hour.getTime());
Calendar alertDate = Calendar.getInstance();
alertDate.setTime(time);
alertDate.add(Calendar.MINUTE, 0);
Date d = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String data = dateFormat.format(d);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date dataf = formatter.parse(data);
List<Compromisso> lista = atualizarDataInicial(s);
for (Compromisso c : lista) {
Time time1 = new Time(c.getHoraInicial().getTime());
java.util.Date utilDate = new java.util.Date(c.getDataInicio().getTime());
Calendar alertHour = Calendar.getInstance();
alertHour.setTime(time1);
alertHour.add(Calendar.MINUTE, -5);
if (alertHour.compareTo(alertDate) == 0 && utilDate.compareTo(dataf) == 0) {
EnviaEmail e = new EnviaEmail();
e.enviaEmail(c.getNomeBeneficiario(), c.getDataInicio(), c.getHoraInicial(), c.getCompromisso());
}
System.out.println("HORA INICIAL CORRETA: " + c.getHoraInicial());
System.out.println("HORA INICIAL ADIANTADA: " + alertHour.getTime());
System.out.println("HORA SISTEMA " + alertDate.getTime());
}
} catch (ParseException ex) {
ex.printStackTrace();
}
}
The way to schedule the execution of the tasks seems ok. The way to send the email also (provided that
lista
contains an element, or it will email more than once, because offor
). The question is whether the methodtarefa()
is being called more than once. Check by placing a breakpoint on the first line of it (new Timer().schedule
) and debugging, or else posting the class that calls this method for us to look at.– Piovezan
I call the method
tarefa()
in the constructor. And it only contains 1 element in the list after it enters theIF
. That is, the email sent are identical.– DiegoAugusto
Probably your problem is as follows: at 10:30:00 run "x". He runs by 10:30 - but it was so fast that it’s still 10:30 (it hasn’t passed 1 second yet) and he calls again the method. Try to stop your task after task "x". Put a 1-second Sleep in the task and make sure it runs twice.
– Dener
@Dener This would be a defect in the implementation of
Timer
. The version of the methodschedule()
which is being used is based on milliseconds and not seconds or a fixed date.– Piovezan
@Techies What is the class of this builder? And who calls this builder? We need the whole context to investigate if it is being called more than once. If you want to make sure it is not, debug as I suggested.
– Piovezan
This is the Constructor:
public void initialize(URL url, ResourceBundle rb) {
 init();
 tarefa();
 alertaInicial();

 }
– DiegoAugusto
It really is strange. I saw nothing problematic in your code. Despite this, it may be that the method
alertaGeral()
make any exception causing the Timer to stop working.– Ruben O. Chiavone
If I use the javafx Alert component in the error warning method also kk
– DiegoAugusto
But you went after error log to try to figure out what the problem is?
– Ruben O. Chiavone
Yes, the Alert error I agreed by sending emails. The error was this:
java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
– DiegoAugusto
Found the solution to this problem?
– Daniel
I haven’t solved @Danielgomes yet, it was a "test project"
– DiegoAugusto