0
I’m trying to develop a small system that sends a e-mail every Tuesday at 08:00 in the morning.
But I’m a little confused, I can already send the email and check if it’s Tuesday, but I don’t know if it’s correct.
The system keeps running, however as I am still not checking if it is 08:00 in the morning it should not send several emails non-stop, since the only condition for sending emails is to be Tuesday?
Here is the code:
public void gerarAviso() {
Timer timer = null;
if (timer == null) {
timer = new Timer();
Calendar data = Calendar.getInstance();
TimerTask tarefa = new TimerTask() {
@Override
public void run() {
System.err.println("DATA: " + data);
if (data.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY){
System.out.println("É terça feira");
EnviaEmail e = new EnviaEmail();
e.enviaEmail("[email protected]");
}else {
System.out.println("Não é terça feira!");
}
}
};
timer.schedule(tarefa, data.getTime());
}
}
Send multiple emails? This code block is being called inside some loop like
for
orwhile
?– Jéf Bueno
Send only one. I just want to send it every Tuesday at 8:00 am, IE, the system will run non-stop
– Diego
I’m referring to your question
ele não deveria enviar vários emails sem parar, já que a única condição para envio de emails é ser Terça?
... Surely he shouldn’t send several e-mails, the code block will only be executed once. (Unless it is being called inside a repeat loop)– Jéf Bueno
Aa yes, that would be by condition, it should send only one but it continues to be executed in Task.
– Diego
But is something going wrong with this code? What is the problem you need to solve?
– Jéf Bueno
The problem is that I do not know if it is correct, he makes the first check and checks if it is Tuesday but I do not know a way to test to see if it continues running. The only way I could see would be to let it run until tomorrow
– Diego