001 package org.hackystat.telemetry.service.prefetch; 002 003 import java.util.Date; 004 import java.util.Timer; 005 import javax.xml.datatype.XMLGregorianCalendar; 006 import org.hackystat.utilities.tstamp.Tstamp; 007 008 009 /** 010 * Provides a timer task that runs each day at the specified number of minutes past midnight, 011 * and then invokes the run() method of all of the tasks associated with its TimerTaskManager. 012 * 013 * @author Philip M. Johnson 014 */ 015 public class DailyTimer { 016 /** Milliseconds in a day. */ 017 private long millisecondsInADay = (1000 * 60 * 60 * 24); 018 /** Task manager. */ 019 private Date triggerTime; 020 021 /** 022 * Creates a new DailyTimer, which runs each day at minutesPastMidnight. 023 * @param minutesPastMidnight The time when the tasks in the associated TimerTaskManager run. 024 * @param prefetchTask The PrefetchTask associated with this timer. 025 */ 026 public DailyTimer (int minutesPastMidnight, PrefetchTask prefetchTask) { 027 Timer timer = new Timer(true); 028 this.triggerTime = this.getTomorrowTriggerTime(minutesPastMidnight); 029 timer.schedule(prefetchTask, this.triggerTime, this.millisecondsInADay); 030 } 031 032 /** 033 * Create a date corresponding to tomorrow's time at which this DailyTimer will be triggered. 034 * @param minutesPastMidnight The number of minutes past midnight for this trigger. 035 * @return The required tomorrow's time. 036 */ 037 private Date getTomorrowTriggerTime(int minutesPastMidnight) { 038 XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(); 039 tstamp = Tstamp.incrementDays(tstamp, 1); 040 tstamp.setHour(0); 041 tstamp.setMinute(0); 042 tstamp.setSecond(0); 043 long tstampLong = tstamp.toGregorianCalendar().getTimeInMillis(); 044 tstampLong += minutesPastMidnight * 60L * 1000L; 045 return new Date(tstampLong); 046 } 047 048 /** 049 * Returns the Date indicating the time this task is next scheduled to run. 050 * @return The trigger time. 051 */ 052 public Date getTriggerTime() { 053 return new Date(this.triggerTime.getTime()); 054 } 055 } 056