1 28 29 package com.caucho.server.dispatch; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.config.types.Period; 33 import com.caucho.log.Log; 34 import com.caucho.util.IntArray; 35 import com.caucho.util.L10N; 36 import com.caucho.util.QDate; 37 38 import java.util.logging.Logger ; 39 40 43 public class RunAt { 44 static L10N L = new L10N(RunAt.class); 45 private static final Logger log = Log.open(RunAt.class); 46 47 private QDate _cal = QDate.createLocal(); 48 49 private long _period = -1; 50 51 private IntArray _hourTimes; 52 private IntArray _minuteTimes; 53 54 57 public RunAt() 58 { 59 } 60 61 64 public void addText(String runAt) 65 throws ConfigException 66 { 67 configureRunAt(runAt); 68 } 69 70 73 public void setPeriod(Period period) 74 throws ConfigException 75 { 76 _period = period.getPeriod(); 77 } 78 79 82 public long getNextTimeout(long now) 83 { 84 _cal.setGMTTime(now); 85 long zone = _cal.getZoneOffset(); 86 87 if (_period > 0) 88 return Period.periodEnd(now + zone, _period) - zone; 89 90 now = now - now % 60000; 91 92 long local = now + zone; 93 94 long dayMinutes = (local / 60000) % (24 * 60); 95 long hourMinutes = dayMinutes % 60; 96 97 long nextDelta = Long.MAX_VALUE; 98 99 for (int i = 0; _hourTimes != null && i < _hourTimes.size(); i++) { 100 long time = _hourTimes.get(i); 101 long delta = (time - dayMinutes + 24 * 60) % (24 * 60); 102 103 if (delta == 0) 104 delta = 24 * 60; 105 106 if (delta < nextDelta && delta > 0) 107 nextDelta = delta; 108 } 109 110 for (int i = 0; _minuteTimes != null && i < _minuteTimes.size(); i++) { 111 long time = _minuteTimes.get(i); 112 long delta = (time - hourMinutes + 60) % 60; 113 114 if (delta == 0) 115 delta = 60; 116 117 if (delta < nextDelta && delta > 0) 118 nextDelta = delta; 119 } 120 121 if (nextDelta < Integer.MAX_VALUE) 122 return now + nextDelta * 60000L; 123 else 124 return Long.MAX_VALUE / 2; 125 } 126 127 130 private void configureRunAt(String string) 131 throws ConfigException 132 { 133 int len = string.length(); 134 char ch = 0; 135 int i = 0; 136 while (true) { 137 for (; 138 i < len && 139 (Character.isWhitespace(ch = string.charAt(i)) || ch == ','); 140 i++) { 141 } 142 143 if (i >= len) 144 return; 145 146 if (! (ch >= '0' && ch <= '9' || ch == ':')) 147 throw new ConfigException(L.l("illegal run-at time `{0}'. Run-at values are either hour (0:00, 6:30, 12:15) or minute (:15, :30, :45).", 148 string)); 149 150 int hour = 0; 151 int minute = 0; 152 boolean hasHour = false; 153 for (; i < len && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++) { 154 hasHour = true; 155 hour = 10 * hour + ch - '0'; 156 } 157 158 if (ch == ':') { 159 i++; 160 for (; i < len && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++) 161 minute = 10 * minute + ch - '0'; 162 163 } 164 165 if (hasHour) { 166 if (_hourTimes == null) 167 _hourTimes = new IntArray(); 168 _hourTimes.add(60 * hour + minute); 169 } 170 else { 171 if (_minuteTimes == null) 172 _minuteTimes = new IntArray(); 173 _minuteTimes.add(minute); 174 } 175 } 176 } 177 } 178 | Popular Tags |