I’m using a timer to run a Cron Job in a Tomcat Server, it runs on startup and waits whichever amount of minutes I assign. Right now, Im assigning it to run every 24 hours.
web-xml configuration:
<env-entry>
<description>Minutes to Parse - 1440 minutes = 1 day</description>
<env-entry-name>Minutes</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>1440</env-entry-value>
</env-entry>
Cron Job configuration:
private void startScheduler() {
try {
timer.schedule(new TimerTask() {
public void run() {
scheduleParse();
//timer.cancel();
}
private void scheduleParse() {
notificaciones();
System.out.println("Lo esta haciendo");
}
}, 0, this.minutes * 60 * 1000);
}
catch (Exception e) {
}
}
But I need my function to run at midnight every day and by the way we are doing it, it is running every 24 hours taking as a reference the time the deploy was made. For example, if we deploy our web application at 10 am today, it will be running every day at 10 am.
I found a solution, but I want to know if there is a better one for this. The time interval will be every 60 minutes… if the actual hour is 12 am, the process will be made. If it is not, nothing will be done.
Is there a better solution for this?
You should use Timer.scheduleAtFixedRate() instead of Timer.schedule().
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html#scheduleAtFixedRate%28java.util.TimerTask,%20java.util.Date,%20long%29