1

iam trying to make a task to be executed EVERYDAY at 8am (example).

Currently what i have done is make a task, execute it automatically but only for a period of time (in this case 50 seconds) i want it to be executed every day at 8am.

my current code :

    Timer time = new Timer(); 
    certif_envia_notificacion st = new certif_envia_notificacion(); 
    time.schedule(st, 0, 50000);

where certif_envia_notificacion is the task that i want to execute, and it does execute every 50 seconds.

Currently the only way i can figure this out is in the task certif_envia_notificacion ask for the time and execute if its 8am, but that seems like a huge waste of server resources, since ill have to execute the task at least 24 times a day.

Thanks in advance.

Shocklo
  • 467
  • 2
  • 17
  • Possible duplicate - You may find this helpful: http://stackoverflow.com/questions/20387881/how-to-run-certain-task-every-day-at-a-particular-time-using-scheduledexecutorse – Sh4d0wsPlyr Sep 03 '15 at 15:39
  • What kind of naming convention is `certif_envia_notificacion`? Capitalise the first letter of each word, then it's obvious where each word begins and you don't need underscores: `CertifEnviaNotificacion`. – bcsb1001 Sep 03 '15 at 15:43
  • @bcsb1001 its none convention, its just an example – Shocklo Sep 03 '15 at 15:53
  • and @Sh4d0wsPlyr ill look at it, but iam usin java 1.4.2 (its old, i know, but i have to work with this....) – Shocklo Sep 03 '15 at 15:56
  • @Shocklo Best of luck with the older version, I understand the frustrations it will have. Its not an optimal solution, but it you want it to run at the same time every single day why not just set the schedule to run every day, and then start the program at the correct time? e.g. set the delay to 86400000 (24 hours). Its not a great solution, but simple and quick. – Sh4d0wsPlyr Sep 03 '15 at 17:02
  • @Sh4d0wsPlyr Thanks!, but the problem that iam facing is that the server its frecuently restarted (1 time every week or more), so restarting it every day at the same time its not an option, anyway ill post the answer that i did. if anyone know a better way ill be glad to read ya! – Shocklo Sep 03 '15 at 17:25

2 Answers2

1

This is kind of a hack solution that I thought up, but feel free to give comments/feedback based on your requirements.

Basically, from my understanding you have a Java program that runs automatically when the computer starts up (if it restarts), and will then check every hour to see if it is the correct time to run the application. Why not keep it running this way, but instead of running every hour, calculate the time required to get to the next time required (e.g. 8PM the next day).

So for example, lets say you restart it at 3:15PM. When the java program runs, get it to get the current time (System.currentTimeMil...) and then calculate the time required to get to 8PM. You can do some simple math on this (e.g. 20 - current time = time required to wait) and if its negative, simply add 24 to it (e.g. if its 20 - 23, the answer is 24 - 3 = 21, thus wait 21 hours for the next 8PM).

This process can be used every time you run the code, or on startup. I'd recommend just creating a simple function to calculate the required sleep time.

Sh4d0wsPlyr
  • 948
  • 12
  • 28
  • well that actually might work... let me give it a try, thanks for your feedback ;) – Shocklo Sep 03 '15 at 19:01
  • I've always been a fan of the most elegant and simple solution possible. :) – Sh4d0wsPlyr Sep 03 '15 at 19:02
  • indeed, not the "best" way of doing it, but works, finally i start a procedure automatically for the first time that only sets the time for the upcoming procedures, do a calculation and thats it, so THANKS! – Shocklo Sep 03 '15 at 19:47
0

What i finally did was execute the procedure every one hour and make a question of the hour and select the hour that i wanted, i know its not the best way but i needed to finish it fast....

    Calendar cal = Calendar.getInstance(); 
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if(hour == DesiredHour){
    //do stuff
    }else if(hour == DesiredHour2){
    //do stuff 2
    }

Thanks for the comments.

Shocklo
  • 467
  • 2
  • 17
  • I hate to ask this - but based on your comment previously it sounds like your server is restarted frequently. Do you have a job setup to restart the process automatically? – Sh4d0wsPlyr Sep 03 '15 at 17:59
  • we are currently developing new functionalities, so yeah, its restarted, frecuently, and yes, the process starts automatically, all i had to do was place 1 on the web.xml (working with jboss 4.4.2) – Shocklo Sep 03 '15 at 18:36