Perform a task on a specific date and time

Asked

Viewed 399 times

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 or while?

  • Send only one. I just want to send it every Tuesday at 8:00 am, IE, the system will run non-stop

  • 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)

  • Aa yes, that would be by condition, it should send only one but it continues to be executed in Task.

  • But is something going wrong with this code? What is the problem you need to solve?

  • 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

Show 1 more comment

1 answer

1


The method Schedule(Timertask task, Date time) only performs the task once. I will recommend you to use the method: scheduleAtFixedRate(Timertask task, Date firsttime, long period) for the method to be repeated day by day. Remembering that the period is measured in milliseconds.

It can also be done a loop manually that waits a day to check the day of the week and send the email.

Source: http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html

  • Thank you @Shura16. I do a check every 1 hour to see if the day is Tuesday and if it’s 8:00. This way the system is always running and checking.

  • It can also be, would be careful to check only the hour and not the minutes and seconds. If for any reason there is a delay in the system and pass these seconds or minutes the task will not be executed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.