I have a TimerTask
that calls a method every 1 minute, but the method is called twice. This method checks for some factors and sends an e-mail, as the method is called twice, and the e-mail 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 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();
}
}