Changeset 323 for kraken-cron/src
- Timestamp:
- 08/11/09 14:50:09 (13 months ago)
- Location:
- kraken-cron/src/main/java/org/krakenapps/cron
- Files:
-
- 4 added
- 11 modified
-
CronScript.java (modified) (10 diffs)
-
CronService.java (modified) (1 diff)
-
Schedule.java (modified) (7 diffs)
-
impl/CronConfig.java (modified) (8 diffs)
-
impl/CronField.java (modified) (6 diffs)
-
impl/CronScriptFactory.java (modified) (1 diff)
-
impl/CronServiceImpl.java (modified) (7 diffs)
-
impl/IllegalTypeException.java (added)
-
impl/Job.java (modified) (4 diffs)
-
impl/NextOccurence.java (added)
-
impl/QueryStringGenerator.java (added)
-
impl/Scheduler.java (modified) (8 diffs)
-
test/JobTest.java (added)
-
test/NextOccurTest.java (modified) (3 diffs)
-
test/ScheduleBuildTest.java (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
kraken-cron/src/main/java/org/krakenapps/cron/CronScript.java
r322 r323 4 4 5 5 import org.krakenapps.api.Script; 6 import org.krakenapps.api.ScriptArgument; 6 7 import org.krakenapps.api.ScriptContext; 8 import org.krakenapps.api.ScriptUsage; 7 9 import org.krakenapps.cron.impl.CronField; 8 import org.krakenapps.cron.impl.CronServiceImpl;9 10 import org.osgi.framework.BundleContext; 10 11 import org.osgi.framework.InvalidSyntaxException; 11 12 import org.osgi.framework.ServiceReference; 12 13 14 /** 15 * Provides cron management command scripts. 16 * 17 * @author periphery 18 * @since 1.0.0 19 */ 13 20 public class CronScript implements Script { 14 21 … … 27 34 } 28 35 36 @ScriptUsage(description = "list all cron schedules") 29 37 public void list(String[] args) { 30 38 context.println("=================="); … … 37 45 } 38 46 47 @ScriptUsage(description = "view current state of cron scheduling queue") 39 48 public void queue(String[] args) { 40 49 context.println("=================="); … … 47 56 } 48 57 58 @ScriptUsage(description = "register new cron schedule", arguments = { 59 @ScriptArgument(name = "min", type = "string", description = "(0 - 59)"), 60 @ScriptArgument(name = "hour", type = "string", description = "(0 - 23)"), 61 @ScriptArgument(name = "day_of_month", type = "string", description = "(1 - 31)"), 62 @ScriptArgument(name = "month", type = "string", description = "(1 - 12)"), 63 @ScriptArgument(name = "day_of_week", type = "string", description = "(0 - 6) (Sunday=0)"), 64 @ScriptArgument(name = "task", type = "string", description = "Runnable alias")}) 49 65 public void register(String[] args) { 50 if (args.length != 6) {51 printUsage();52 return;53 }54 66 try { 55 67 Schedule.Builder builder = new Schedule.Builder(args[5]); … … 69 81 } 70 82 83 @ScriptUsage(description = "unregister cron schedule", arguments = { 84 @ScriptArgument(name = "id", type = "string", description = "cron schedule id")}) 71 85 public void unregister(String[] args){ 72 if(args.length < 1) {73 context.println("USAGE : SCHEDULE_ID");74 return;75 }76 86 try { 77 87 manager.unregisterSchedule(Integer.parseInt(args[0])); … … 86 96 } 87 97 88 private void printUsage() { 89 context.println("USAGE :"); 98 @ScriptUsage(description = "print cron schedule syntax") 99 public void usage(String[] args) { 100 context.println("SYNTAX :"); 90 101 context.println("* * * * * RUNNABLE_ALIAS\n\r" + "- - - - -\n\r" + "| | | | |\n\r" 91 102 + "| | | | +----- day of week (0 - 6) (Sunday=0)\n\r" … … 94 105 + "| +----------- hour (0 - 23)\n\r" 95 106 + "+------------- min (0 - 59)\n\r"); 107 context.println("EXAMPLES : \n\r" 108 + "Run once a year | 0 0 1 1 * \n\r" 109 + "Run once a week | 0 0 * * 0 \n\r" 110 + "Run every five minute | */5 * * * * \n\r" 111 + "Run once a day from monday to friday | 0 0 * * 1-5 \n\r" 112 + "Run once a day on saturday and sunday| 0 0 * * 0,6 \n\r"); 96 113 97 114 } … … 99 116 100 117 101 118 @ScriptUsage(description = "list all active Runnables") 102 119 public void test(String[] args) throws InvalidSyntaxException { 103 if (args.length == 0) { 104 context.println("input runnable alias"); 105 return; 106 } 107 108 109 context.println("cron test started"); 120 context.println("===================="); 121 context.println("Active Runnable List"); 122 context.println("===================="); 110 123 ServiceReference[] refs; 111 124 try { … … 113 126 114 127 refs = bundleContext.getServiceReferences(Runnable.class.getName(), 115 "(alias="+args[0]+")");128 null); 116 129 if (refs == null || refs.length == 0) { 117 context.println(" can't get " + args[0]);130 context.println("empty list"); 118 131 return; 119 132 } … … 123 136 } 124 137 Runnable task = ((Runnable) bundleContext.getService(refs[0])); 125 126 context.println("test success");127 138 128 139 129 140 } catch (InvalidSyntaxException e) { 130 // TODO Auto-generated catch block131 141 e.printStackTrace(); 132 142 } 133 // manager.getRef(args[0]);134 135 143 } 136 144 -
kraken-cron/src/main/java/org/krakenapps/cron/CronService.java
r322 r323 3 3 import java.util.List; 4 4 5 import org.osgi.framework.BundleContext; 6 import org.osgi.framework.InvalidSyntaxException; 7 5 /** 6 * cronService interface 7 * 8 * @author periphery 9 * @since 1.0.0 10 */ 8 11 public interface CronService { 9 12 void registerSchedule(Schedule schedule) throws Exception; -
kraken-cron/src/main/java/org/krakenapps/cron/Schedule.java
r322 r323 7 7 import java.util.Map; 8 8 9 import javassist.expr.Instanceof;10 11 9 import org.krakenapps.cron.impl.CronField; 12 import org.krakenapps.cron.impl.CronServiceImpl; 13 import org.krakenapps.cron.test.DummyBundleContext; 14 import org.osgi.framework.BundleContext; 10 import org.krakenapps.cron.impl.NextOccurence; 11 import org.krakenapps.cron.impl.CronField.Type; 15 12 import org.osgi.framework.InvalidSyntaxException; 16 13 14 /** 15 * Schedule used for registering cron jobs. 16 * 17 * @author periphery 18 * @since 1.0.0 19 */ 17 20 public final class Schedule { 21 private static final int MONTH_TYPE = 1; 22 private static final int DAY_TYPE = 2; 23 private static final int HOUR_TYPE = 3; 24 private static final int MINUTE_TYPE = 4; 25 private static final int None = -1; 18 26 private final Map<String, CronField> map; 19 27 private final String taskAlias; … … 38 46 this.taskAlias = builder.taskAlias; 39 47 } 48 49 private void setAllFirstValue(Calendar base, NextOccurence next, int type) 50 throws Exception { 51 for (int i = type; i < 5; i++) { 52 next.set(i, getFirstValue(i, base)); 53 } 54 } 55 56 /* 57 * returns the first date corresponding to the schedule, given current time 58 */ 59 public Date getNextOccurence(Date current) { 60 try { 61 Calendar base = Calendar.getInstance(); 62 NextOccurence next = new NextOccurence(); 63 64 base.setTime(current); 65 base.set(Calendar.SECOND, 0); 66 67 for (int type = MINUTE_TYPE; type > 0; type--) 68 adjustNextOccurence(base, next, type); 69 70 next.set(base.get(Calendar.YEAR), None, None, None, None); 71 72 return newDate(next); 73 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 return null; 78 } 40 79 41 public Map<String, CronField> getFields() { 42 // TODO Auto-generated method stub 43 return new HashMap<String, CronField>(map); 44 } 45 46 public String getTaskAlias(){ 80 private static final int calendarTypes[] = { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE }; 81 private static final int upperCalendarTypes[] = { 0, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY}; 82 83 private void adjustNextOccurence(Calendar base, NextOccurence next, int type) 84 throws Exception { 85 int test = getNextValue(type, base); 86 if (test == None) { 87 base.add(upperCalendarTypes[type], 1); 88 if (type == DAY_TYPE && getFirstValue(DAY_TYPE, base) == None) 89 base.add(upperCalendarTypes[type], 1); 90 91 if (type == MONTH_TYPE) 92 base.set(Calendar.MONTH, getFirstValue(type, base)); 93 94 setAllFirstValue(base, next, type); 95 } else if (test > base.get(calendarTypes[type])) { 96 if (type == MONTH_TYPE) 97 base.set(Calendar.MONTH, test); 98 99 next.set(type, test); 100 setAllFirstValue(base, next, type + 1); 101 } else { 102 next.set(type, test); 103 } 104 } 105 106 private int getFirstValue(int type, Calendar base) throws Exception { 107 switch (type) { 108 case MONTH_TYPE: return map.get("Month").first(); 109 case DAY_TYPE: return map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 110 case HOUR_TYPE: return map.get("Hour").first(); 111 case MINUTE_TYPE: return map.get("Minute").first(); 112 } 113 throw new Exception(" unsupported first value type"); 114 } 115 116 private int getNextValue(int type, Calendar base) throws Exception { 117 switch (type) { 118 case MONTH_TYPE: return map.get("Month").next(base.get(Calendar.MONTH)); 119 case DAY_TYPE: return map.get("DayOfMonth").next(base, map.get("DayOfWeek")); // merge-or 120 case HOUR_TYPE: return map.get("Hour").next(base.get(Calendar.HOUR_OF_DAY)); 121 case MINUTE_TYPE: return map.get("Minute").next(base.get(Calendar.MINUTE)); 122 default: 123 return -2; 124 } 125 } 126 127 private Date newDate(NextOccurence next) { 128 Calendar result = Calendar.getInstance(); 129 result.set(Calendar.SECOND, 0); 130 result.set(Calendar.MINUTE, next.minute); 131 result.set(Calendar.HOUR_OF_DAY, next.hour); 132 result.set(Calendar.DAY_OF_MONTH, next.day + 1); // offset 133 result.set(Calendar.MONTH, next.month); 134 result.set(Calendar.YEAR, next.year); 135 return result.getTime(); 136 } 137 138 /* 139 * returns task alias 140 */ 141 public String getTaskAlias() { 47 142 return this.taskAlias; 48 143 } 49 144 50 145 @Override 51 146 public String toString() { 52 147 return String.format("%8s %8s %8s %8s %8s / %8s", map.get("Minute"), … … 55 150 } 56 151 152 /* 153 * returns members of the given cron field 154 */ 57 155 public String fieldMembers(CronField.Type type) { 58 156 return map.get(type.toString()).debugString(); 157 } 158 159 /* 160 * returns expression string of given cron field 161 */ 162 public Object get(Type type) { 163 return type.toString(); 59 164 } 60 165 … … 69 174 return this.toString().hashCode(); 70 175 } 71 176 177 /* 178 * builder class for schedule class 179 */ 72 180 public static class Builder { 73 181 private final Map<String, CronField> map; 74 182 private final String taskAlias; 75 183 76 public Builder(String taskAlias) throws InvalidSyntaxException { 184 /* 185 * create new builder object. default scheduling rule is "* * * * *"(minutely). 186 */ 187 public Builder(String taskAlias){ 77 188 this.map = new HashMap<String, CronField>(); 78 189 this.taskAlias = taskAlias; … … 88 199 CronField.Type.Day_of_Week, null)); 89 200 } catch (ParseException e) { 90 // TODO Auto-generated catch block 91 e.printStackTrace(); 201 //must succeed. ignored. 92 202 } 93 203 } 94 204 205 // 206 // Set cron field with given expression. 207 // Following expressions are supported. 208 // 1. comma(',') as list. e.g:"1,3,4,8" (space inside the list must not be used) 209 // 2. dash('-') as range. e.g:"1-6", which means 1 to 6 210 // 3. asterisk('*') as wild. e.g:"*", which means every~ 211 // 4. slash('/') as interval. e.g:"*/5" which means every five~ 212 // 95 213 public Builder set(CronField.Type type, String exp) 96 214 throws ParseException { … … 99 217 } 100 218 219 /* 220 * returns schedule object representing scheduling rule of current build object. 221 * e.g. new Schedule.Builder("test").set(CronField.Type.Minute,"5").build(); 222 * represents schedule of "5 * * * * / test" 223 */ 101 224 public Schedule build() throws Exception { 102 225 return new Schedule(this); 103 226 } 104 227 228 /* 229 * returns schedule object representing scheduling rule of given expression. 230 * cron fields previously set by set() method are ignored. 231 */ 105 232 public Schedule build(String exp) throws Exception { 106 233 String[] splited = exp.split(" "); … … 116 243 } 117 244 118 public Schedule buildYearly() throws Exception { 119 Schedule result = null; 245 /* 246 * same as build("0 0 1 1 *") 247 */ 248 public Schedule buildYearly() { 249 return mustSuccessBuild("0 0 1 1 *"); 250 } 251 252 /* 253 * same as build("0 0 1 * *") 254 */ 255 public Schedule buildMonthly() { 256 return mustSuccessBuild("0 0 1 * *"); 257 } 258 259 /* 260 * same as build("0 0 * * 0") 261 */ 262 public Schedule buildWeekly() { 263 return mustSuccessBuild("0 0 * * 0"); 264 } 265 266 /* 267 * same as build("0 0 * * *") 268 */ 269 public Schedule buildDaily() { 270 return mustSuccessBuild("0 0 * * *"); 271 } 272 273 /* 274 * same as build("0 * * * *") 275 */ 276 public Schedule buildHourly() { 277 return mustSuccessBuild("0 * * * *"); 278 } 279 280 private Schedule mustSuccessBuild(String expression) { 120 281 try { 121 re sult = build("0 0 1 1 *");122 } catch ( ParseException e) {123 // TODO Auto-generated catch block124 e.printStackTrace();282 return build(expression); 283 } catch (Exception e) { 284 // ignore all, not reachable 285 return null; 125 286 } 126 return result; 127 } 128 129 public Schedule buildMonthly() throws Exception { 130 Schedule result = null; 131 try { 132 result = build("0 0 1 * *"); 133 } catch (ParseException e) { 134 // TODO Auto-generated catch block 135 e.printStackTrace(); 136 } 137 return result; 138 } 139 140 public Schedule buildWeekly() throws Exception { 141 Schedule result = null; 142 try { 143 result = build("0 0 * * 0"); 144 } catch (ParseException e) { 145 // TODO Auto-generated catch block 146 e.printStackTrace(); 147 } 148 return result; 149 } 150 151 public Schedule buildDaily() throws Exception { 152 Schedule result = null; 153 try { 154 result = build("0 0 * * *"); 155 } catch (ParseException e) { 156 // TODO Auto-generated catch block 157 e.printStackTrace(); 158 } 159 return result; 160 } 161 162 public Schedule buildHourly() throws Exception { 163 Schedule result = null; 164 try { 165 result = build("0 * * * *"); 166 } catch (ParseException e) { 167 // TODO Auto-generated catch block 168 e.printStackTrace(); 169 } 170 return result; 171 } 172 173 } 174 175 public static class QueryStringGenerator { 176 public static final String db_name = "cron_schedules"; 177 public static final String day_of_week = "day_of_week"; 178 public static final String month = "month"; 179 public static final String day_of_month = "day_of_month"; 180 public static final String hour = "hour"; 181 public static final String minute = "minute"; 182 public static final String task = "task"; 183 184 public static String createQuery() { 185 return String 186 .format( 187 "CREATE TABLE %s (id INTEGER IDENTITY,%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256),%s VARCHAR(256))", 188 db_name, minute, hour, day_of_month, month, 189 day_of_week, task); 190 } 191 192 public static String selectAllQuery() { 193 return String.format( 194 "select id, %s, %s, %s, %s, %s, %s from %s order by id", 195 minute, hour, day_of_month, month, day_of_week, task, 196 db_name); 197 } 198 199 public static String insertQuery(Schedule sche) { 200 return String 201 .format( 202 "insert into %s (%s, %s, %s, %s, %s, %s) values (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\')", 203 db_name, minute, hour, day_of_month, month, 204 day_of_week, task, sche.map.get("Minute"), sche.map 205 .get("Hour"), sche.map.get("DayOfMonth"), 206 sche.map.get("Month"), sche.map.get("DayOfWeek"), 207 sche.taskAlias); 208 } 209 210 public static String deleteQuery(int id) { 211 return String.format("delete from %s where id = %d", db_name, id); 212 } 213 } 214 215 216 217 218 219 287 } 288 } 289 220 290 221 291 } 292 293 -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronConfig.java
r322 r323 1 1 package org.krakenapps.cron.impl; 2 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 3 import java.sql.*; 9 4 import java.util.HashMap; 10 5 import java.util.Map; … … 14 9 import org.slf4j.LoggerFactory; 15 10 11 /** 12 * This class handles jdbc operations associated with cron schedules. 13 * 14 * @author periphery 15 * @since 1.0.0 16 */ 16 17 public class CronConfig { 17 18 // jdbc operations … … 31 32 32 33 public int addEntry(Schedule schedule) { 33 return update( Schedule.QueryStringGenerator.insertQuery(schedule));34 return update(QueryStringGenerator.insertQuery(schedule)); 34 35 } 35 36 36 37 public void removeEntry(int id) { 37 update( Schedule.QueryStringGenerator.deleteQuery(id));38 update(QueryStringGenerator.deleteQuery(id)); 38 39 } 39 40 … … 44 45 connect(); 45 46 46 st = connection.prepareStatement( Schedule.QueryStringGenerator.selectAllQuery());47 st = connection.prepareStatement(QueryStringGenerator.selectAllQuery()); 47 48 48 49 ResultSet rs = st.executeQuery(); … … 79 80 80 81 private void initializeSchema() { 81 logger. info("Cron: initializing cron config table schema.");82 logger.debug("Cron: initializing cron config table schema."); 82 83 createTable(); 83 84 } … … 90 91 91 92 private void createTable() { 92 update( Schedule.QueryStringGenerator.createQuery());93 update(QueryStringGenerator.createQuery()); 93 94 } 94 95 … … 103 104 if (rs.next()) { 104 105 autoIncKeyFromApi = rs.getInt(1); 105 logger.debug("CronConfig : inserted. id=" + autoIncKeyFromApi);106 logger.debug("CronConfig : " + expression); 106 107 } 107 108 rs.close(); 108 109 connection.commit(); 109 110 } catch (Exception e) { 110 e.printStackTrace(); 111 logger.info("EXCEPTION RAISED DURING " + expression + " !! BUT WAS IGNORED."); 111 logger.debug("CronConfig : exception raised during " + expression + ". IGNORED."); 112 112 rollback(); 113 113 } finally { … … 145 145 } 146 146 } 147 148 149 147 } -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronField.java
r322 r323 5 5 import java.util.Calendar; 6 6 7 /** 8 * component of {@link Schedule}. 9 * 10 * @author periphery 11 * @since 1.0.0 12 */ 7 13 public final class CronField { 8 14 private final Type type; … … 142 148 public int next(int start) throws Exception{ 143 149 if(this.type.equals(CronField.Type.Day_of_Month) || this.type.equals(CronField.Type.Day_of_Week)) 144 throw new Exception("not allowed for day_of_month and day_of_week. use next(Calendar, CronField) instead.");150 throw new IllegalTypeException("not allowed for day_of_month and day_of_week. use next(Calendar, CronField) instead."); 145 151 return this.bits.nextSetBit(start); 146 152 } 147 153 148 154 public int first() throws Exception { 149 // TODO Auto-generated method stub 155 if(this.type.equals(CronField.Type.Day_of_Month) || this.type.equals(CronField.Type.Day_of_Week)) 156 throw new IllegalTypeException("not allowed for day_of_month and day_of_week. use next(Calendar, CronField) instead."); 150 157 return this.next(0); 151 158 } … … 155 162 public int next(Calendar base, CronField dow) throws Exception { 156 163 if(!this.type.equals(CronField.Type.Day_of_Month)) 157 throw new Exception("only for day_of_month. use next(Calendar, CronField) instead."); 164 throw new IllegalTypeException("only for day_of_month. use next(Calendar, CronField) instead."); 165 return dow2month(base, dow).nextSetBit(base.get(Calendar.DAY_OF_MONTH) -1); 166 } 167 168 public int first(Calendar base, CronField dow) throws Exception{ 169 if(!this.type.equals(CronField.Type.Day_of_Month)) 170 throw new IllegalTypeException("only for day_of_month. use next(Calendar, CronField) instead."); 171 return dow2month(base, dow).nextSetBit(0); 172 } 173 174 private BitSet dow2month(Calendar base, CronField dow){ 158 175 Calendar clone = (Calendar) base.clone(); 159 176 clone.set(Calendar.DAY_OF_MONTH, 1); … … 167 184 for(int i = lastDayOfMonth; i<this.type.bitLength; i++) 168 185 dow2month.clear(i); 169 return dow2month.nextSetBit(base.get(Calendar.DAY_OF_MONTH) -1); 170 } 171 172 public int first(Calendar base, CronField dow) throws Exception{ 173 if(!this.type.equals(CronField.Type.Day_of_Month)) 174 throw new Exception("only for day_of_month. use next(Calendar, CronField) instead."); 175 Calendar clone = (Calendar) base.clone(); 176 clone.set(Calendar.DAY_OF_MONTH, 1); 177 int weekDayOf_1 = clone.get(Calendar.DAY_OF_WEEK) - 1; //1 for offset 178 //generate 31 length bitmap by duplicating dow bitmap. 179 BitSet dow2month = mergeBits(weekDayOf_1, this.bits, dow.bits); 180 //turn off for surplus days of month. (e.g. turn off 30 and 31 from Feb.) 181 return dow2month.nextSetBit(0); 182 } 183 184 public static BitSet mergeBits(int weekDayOf_1, BitSet dom, BitSet dow){ 186 return dow2month; 187 } 188 189 private static BitSet mergeBits(int weekDayOf_1, BitSet dom, BitSet dow){ 185 190 BitSet dow2month = new BitSet(); 186 191 for(int i=0; i<CronField.Type.Day_of_Month.getBitLength(); i++){ 187 192 int weekDayOf_i = (i + weekDayOf_1)%7; 188 193 dow2month.set(i, dow.get(weekDayOf_i)); 189 //System.out.println("weekDayOPf_i = " + weekDayOf_i);190 194 } 191 195 dow2month.or(dom); //dow bits and dom bits are merged by OR operation. … … 193 197 } 194 198 195 196 199 public String toString(){ 197 200 return this.exp; … … 201 204 return this.bits.toString(); 202 205 } 203 204 206 } -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronScriptFactory.java
r316 r323 9 9 import org.osgi.framework.ServiceReference; 10 10 11 /** 12 * The script factory class for cron service. 13 * 14 * @author periphery 15 * @since 1.0.0 16 */ 11 17 public class CronScriptFactory implements ScriptFactory { 12 18 private BundleContext context; -
kraken-cron/src/main/java/org/krakenapps/cron/impl/CronServiceImpl.java
r322 r323 10 10 import org.krakenapps.cron.CronService; 11 11 import org.krakenapps.cron.Schedule; 12 12 13 import org.osgi.framework.BundleContext; 13 14 import org.osgi.framework.InvalidSyntaxException; 14 15 import org.osgi.framework.ServiceReference; 15 16 17 /** 18 * This class provides implementation for the {@link CronService} 19 * interface. 20 * 21 * @author periphery 22 * @since 1.0.0 23 */ 16 24 public class CronServiceImpl implements CronService { 17 25 private static BundleContext bundleContext; … … 23 31 bundleContext = context; 24 32 this.config = new CronConfig(); 33 refreshMap(); 25 34 } 26 35 … … 28 37 public void registerSchedule(Schedule schedule) throws Exception { 29 38 int id = config.addEntry(schedule); 30 refreshMap();39 this.map.put(id, schedule); 31 40 this.scheduler.insertToQueue(id, schedule); 32 41 //have to change implementation to only refresh the entry that had just been registered. … … 37 46 public void unregisterSchedule(int i) throws Exception { 38 47 config.removeEntry(i); 39 refreshMap();48 this.map.remove(i); 40 49 this.scheduler.deleteFromQueue(i); 41 50 //have to change implementation to only remove the entry that had just been unregistered. … … 44 53 45 54 public void validate() throws Exception { 46 refreshMap();47 55 this.scheduler = new Scheduler(getMap()); 48 56 } … … 75 83 76 84 public static Runnable getRef(String alias) throws InvalidSyntaxException { 77 System.out.println(Runnable.class.getName());85 //System.out.println(Runnable.class.getName()); 78 86 return getRef(bundleContext, alias); 79 87 } … … 86 94 throw new NullPointerException(); 87 95 } 88 89 for (ServiceReference ref : refs) {90 System.out.println(context.getService(ref).getClass()91 .getName());92 }93 94 96 Runnable task = ((Runnable) context.getService(refs[0])); 95 97 return task; 96 98 } 97 98 99 100 101 102 103 104 105 99 } -
kraken-cron/src/main/java/org/krakenapps/cron/impl/Job.java
r322 r323 6 6 7 7 import org.krakenapps.cron.Schedule; 8 import org.osgi.framework.BundleContext;9 8 import org.osgi.framework.InvalidSyntaxException; 10 9 10 /** 11 * Job class used as a component of cron scheduler 12 * 13 * @author periphery 14 * @since 1.0.0 15 */ 11 16 public class Job implements Comparable<Job> , Cloneable{ 12 17 public final int scheduleId; … … 20 25 } 21 26 22 public boolean timeToDo() throws Exception{27 public boolean timeToDo(){ 23 28 return timeToDo(new Date()); 24 29 } 25 30 26 public boolean timeToDo(Date now) throws Exception{ 27 //System.out.println("now : " + now.getTime()/1000 + ", date : " + date.getTime()/1000); 28 if(now.getTime()/1000 - date.getTime()/1000 > 60){ 29 //time passed more than one minute. raise exception 30 throw new Exception("time passed too much. skips this time"); 31 } 31 //return true for jobs whose time passed 32 public boolean timeToDo(Date now){ 32 33 if(now.getTime()/1000 - date.getTime()/1000 < 0){ 33 34 return false; … … 47 48 48 49 public void setNextOccurence(Date now) { 49 this.date = getNextOccurence(schedule,now);50 this.date = schedule.getNextOccurence(now); 50 51 } 51 52 52 53 public void setNextOccurence() { 53 this.date = getNextOccurence(schedule,new Date());54 this.date = schedule.getNextOccurence(new Date()); 54 55 } 55 56 56 public static Date getNextOccurence(Schedule sche, Date current){ 57 try { 58 Map<String, CronField> map = sche.getFields(); 59 Calendar base = Calendar.getInstance(); 60 base.setTime(current); 61 base.set(Calendar.SECOND, 0); 62 // minute 63 int minute = map.get("Minute").next(base.get(Calendar.MINUTE)); 64 if (minute == -1) { // wraps around 65 base.add(Calendar.HOUR_OF_DAY, 1); 66 minute = map.get("Minute").first(); 67 } 68 // hour 69 int hour = map.get("Hour").next(base.get(Calendar.HOUR_OF_DAY)); 70 if (hour == -1) { 71 base.add(Calendar.DAY_OF_MONTH, 1); 72 minute = map.get("Minute").first(); 73 hour = map.get("Hour").first(); 74 } else if (hour > base.get(Calendar.HOUR_OF_DAY)) { 75 minute = map.get("Minute").first(); 76 } 77 // day 78 int day = map.get("DayOfMonth").next(base, map.get("DayOfWeek")); // merge-or 79 // day_of_month 80 // and 81 // day_of_week 82 if (day == -1) { 83 base.add(Calendar.MONTH, 1); 84 minute = map.get("Minute").first(); 85 hour = map.get("Hour").first(); 86 day = map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 87 }else if(day > base.get(Calendar.DAY_OF_MONTH)){ 88 minute = map.get("Minute").first(); 89 hour = map.get("Hour").first(); 90 } 91 //month 92 int month = map.get("Month").next(base.get(Calendar.MONTH)); 93 if(month == -1){ 94 base.add(Calendar.YEAR, 1); 95 minute = map.get("Minute").first(); 96 hour = map.get("Hour").first(); 97 month = map.get("Month").first(); 98 base.set(Calendar.MONTH, month); 99 day = map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 100 }else if(month > base.get(Calendar.MONTH)){ 101 base.set(Calendar.MONTH, month); 102 minute = map.get("Minute").first(); 103 hour = map.get("Hour").first(); 104 day = map.get("DayOfMonth").first(base, map.get("DayOfWeek")); 105 } 106 int year = base.get(Calendar.YEAR); 107 108 Calendar result = Calendar.getInstance(); 109 result.set(Calendar.SECOND, 0); 110 result.set(Calendar.MINUTE, minute); 111 result.set(Calendar.HOUR_OF_DAY, hour); 112 result.set(Calendar.DAY_OF_MONTH, day + 1); //offset 113 result.set(Calendar.MONTH, month); 114 result.set(Calendar.YEAR, year); 115 return result.getTime(); 116 117 } catch (Exception e) { 118 e.printStackTrace(); 119 } 120 return null; 121 } 57 122 58 123 59 public String toString(){ … … 141 77 task.run(); 142 78 } 143 144 145 79 } -
kraken-cron/src/main/java/org/krakenapps/cron/impl/Scheduler.java
r322 r323 1 1 package org.krakenapps.cron.impl; 2 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.Collections; 6 import java.util.Date; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.PriorityQueue; 3 import java.util.*; 10 4 import java.util.Map.Entry; 11 12 import javax.swing.plaf.SliderUI;13 5 14 6 import org.krakenapps.cron.Schedule; … … 17 9 import org.slf4j.LoggerFactory; 18 10 11 /** 12 * Cron scheduler that checks and runs registered cron jobs. 13 * 14 * @author periphery 15 * @since 1.0.0 16 */ 19 17 public final class Scheduler { 20 18 21 final static Logger logger = LoggerFactory.getLogger(CronConfig.class.getName()); 19 final static Logger logger = LoggerFactory.getLogger(CronConfig.class 20 .getName()); 22 21 private final PriorityQueue<Job> queue; 23 22 private final Thread loop; 24 23 private boolean running; 25 24 26 25 public Scheduler(Map<Integer, Schedule> map) { 27 26 this.queue = reset(map); 28 27 this.loop = new Thread(new Loop()); 29 this.running = true;30 28 this.start(); 31 29 } 32 30 33 public void stop() {31 public void stop() { 34 32 this.running = false; 35 33 } 36 37 p ublic void start(){34 35 private void start() { 38 36 this.running = true; 39 37 this.loop.start(); … … 49 47 } 50 48 51 public synchronizedvoid insertToQueue(int id, Schedule sche) {49 public void insertToQueue(int id, Schedule sche) { 52 50 // have to implement 53 Job job = new Job(id,sche); 54 this.queue.add(job); 51 Job job = new Job(id, sche); 52 synchronized (this.queue) { 53 this.queue.add(job); 54 } 55 55 } 56 56 57 public synchronizedvoid deleteFromQueue(int id) {57 public void deleteFromQueue(int id) { 58 58 // have to implement 59 for(Object job : this.queue.toArray()){ 60 if(((Job)job).getScheduleId() == id) 61 this.queue.remove(job); 59 synchronized (this.queue) { 60 for (Object job : this.queue.toArray()) { 61 if (((Job) job).getScheduleId() == id) 62 this.queue.remove(job); 63 } 62 64 } 63 65 } … … 73 75 } 74 76 75 76 77 private class Loop implements Runnable{ 77 private class Loop implements Runnable { 78 78 79 79 @Override … … 82 82 loop(); 83 83 } 84 85 private void loop() {// have to implement sleeping. (may be for 10 secs?) 84 85 private void loop() {// have to implement sleeping. (may be for 10 86 // secs?) 86 87 while (running) { 87 88 checkAndRun(); 88 89 try { 89 Thread.sleep(10 * 1000); // sleep for 10 sec90 Thread.sleep(10 * 1000); // sleep for 10 sec 90 91 } catch (InterruptedException e) { 91 92 // TODO Auto-generated catch block … … 96 97 } 97 98 98 private synchronized void checkAndRun() { 99 try { 100 //logger.info("Cron: scheduler wake up"); 99 private void checkAndRun() { 100 synchronized (queue) { 101 101 if (itIsTime()) { 102 102 Job first = queue.poll(); 103 103 Thread runner = new Thread(new Runner(first.clone())); 104 104 runner.start(); 105 106 //set base time as 1 min after current time 107 first 108 .setNextOccurence(new Date( 109 new Date().getTime() + 60 * 1000)); 105 106 // set base time as 1 min after current time 107 first.setNextOccurence(new Date( 108 new Date().getTime() + 60 * 1000)); 110 109 queue.add(first); 111 // recursive call to check next Job110 // recursive call to check next Job 112 111 checkAndRun(); 113 112 } 114 } catch (Exception e) { // time passed. refresh without execution115 Job first = queue.poll();116 first.setNextOccurence();117 queue.add(first);118 113 } 119 114 } 120 121 public boolean itIsTime() throws Exception { 115 116 public boolean itIsTime() { 117 try{ 122 118 return queue.peek().timeToDo(); 119 } catch (Exception e) { //empty queue 120 return false; 121 } 123 122 } 124 123 } … … 134 133 public void run() { 135 134 try { 136 logger.info("Cron: run registered task " + job);137 135 job.run(); 138 } catch (NullPointerException e){ 139 logger.info("Cron: unable to run " + job + ". runnable " + job.schedule.getTaskAlias() + " is not active."); 136 logger.debug("Cron: run registered task " + job); 137 } catch (NullPointerException e) { 138 logger.debug("Cron: unable to run " + job + ". runnable \'" 139 + job.schedule.getTaskAlias() + "\' is not active."); 140 140 } catch (InvalidSyntaxException e) { 141 141 // TODO Auto-generated catch block … … 143 143 } 144 144 } 145 146 145 } 147 148 146 } -
kraken-cron/src/main/java/org/krakenapps/cron/test/NextOccurTest.java
r321 r323 1 1 package org.krakenapps.cron.test; 2 2 3 import java.util.Calendar; 3 4 import java.util.Date; 4 5 6 7 import org.junit.Test; 5 8 import org.krakenapps.cron.Schedule; 6 import org.krakenapps.cron.impl.Job;7 9 import org.osgi.framework.InvalidSyntaxException; 10 import static org.junit.Assert.*; 8 11 9 import junit.framework.TestCase; 12 public class NextOccurTest { 10 13 11 public class NextOccurTest extends TestCase { 12 13 public void setUp() { 14 15 } 16 17 // public void testDayNext() throws Exception{ 18 // CronField dom = new CronField(CronField.Type.Day_of_Month, "*/2"); 19 // CronField dow = new CronField(CronField.Type.Day_of_Week, "0"); 20 // BitSet a = new BitSet(); 21 // a.set(2); 22 // a.set(15); 23 // BitSet b = new BitSet(); 24 // b.set(0); 25 // System.out.println(CronField.mergeBits(6, a, b)); 26 // 27 // System.out.println(dom.next(Calendar.getInstance(), dow)); 28 // } 29 14 @Test 30 15 public void testNext1() throws InvalidSyntaxException, Exception { 31 16 … … 35 20 // Schedule.Builder("test2").build("20-40 * * 4-7 *"); 36 21 Date date = new Date(Date.parse("Feb 27, 2009 8:14 PM")); 37 System.out.println("============" + date + "============="); 22 System.out.println("============ test1 : " + date + "============="); 23 Schedule sche1 = new Schedule.Builder("awef").build(); 24 Schedule sche2 = new Schedule.Builder("awef").buildHourly(); 25 Schedule sche3 = new Schedule.Builder("awef").buildDaily(); 26 Schedule sche4 = new Schedule.Builder("awef").buildWeekly(); 27 Schedule sche5 = new Schedule.Builder("awef").buildYearly(); 28 Schedule sche6 = new Schedule.Builder("awef").build("*/30 * * * *"); 29 Schedule sche7 = new Schedule.Builder("awef").build("3,17,20 * * * *"); 30 Schedule sche8 = new Schedule.Builder("awef") 31 .build("3,17,20 */3 * * *"); 32 Schedule sche9 = new Schedule.Builder("awef").build("0 0 5,28 * *"); 33 Schedule sche10 = new Schedule.Builder("awef").build("0 0 5,28 3 *"); 34 Schedule sche11 = new Schedule.Builder("awef").build("0 0 5,28 3 1"); 35 Schedule sche12 = new Schedule.Builder("awef").build("* * 31 * 0"); 36 37 System.out.println(sche1.getNextOccurence(date)); 38 System.out.println(sche2.getNextOccurence(date)); 39 System.out.println(sche3.getNextOccurence(date)); 40 System.out.println(sche4.getNextOccurence( date)); 41 System.out.println(sche5.getNextOccurence(date)); 42 System.out.println(sche6.getNextOccurence(date)); 43 System.out.println(sche7.getNextOccurence(date)); 44 System.out.println(sche8.getNextOccurence(date)); 45 System.out.println(sche9.getNextOccurence(date)); 46 System.out.println(sche10.getNextOccurence(date)); 47 System.out.println(sche11.getNextOccurence( date)); 48 System.out.println(sche12.getNextOccurence( date)); 49 50 51 assertEquals(sche1.getNextOccurence(date).toString(),"Fri Feb 27 20:14:00 KST 2009"); 52 assertEquals(sche2.getNextOccurence(date).toString(),"Fri Feb 27 21:00:00 KST 2009"); 53 assertEquals(sche3.getNextOccurence(date).toString(),"Sat Feb 28 00:00:00 KST 2009"); 54 assertEquals(sche4.getNextOccurence(date).toString(),"Sun Mar 01 00:00:00 KST 2009"); 55 assertEquals(sche5.getNextOccurence(date).toString(),"Fri Jan 01 00:00:00 KST 2010"); 56 assertEquals(sche6.getNextOccurence(date).toString(),"Fri Feb 27 20:30:00 KST 2009"); 57 assertEquals(sche7.getNextOccurence(date).toString(),"Fri Feb 27 20:17:00 KST 2009"); 58 assertEquals(sche8.getNextOccurence(date).toString(),"Fri Feb 27 21:03:00 KST 2009"); 59 assertEquals(sche9.getNextOccurence(date).toString(),"Sat Feb 28 00:00:00 KST 2009"); 60 assertEquals(sche10.getNextOccurence(date).toString(),"Thu Mar 05 00:00:00 KST 2009"); 61 assertEquals(sche11.getNextOccurence( date).toString(),"Mon Mar 02 00:00:00 KST 2009"); 62 assertEquals(sche12.getNextOccurence( date).toString(),"Sun Mar 01 00:00:00 KST 2009"); 63 } 64 65 @Test 66 public void testNext2() throws InvalidSyntaxException, Exception { 67 68 // Schedule sche1 = new 69 // Schedule.Builder("test1").build("*/30 * 3,18 * 0"); 70 // Schedule sche2 = new 71 // Schedule.Builder("test2").build("20-40 * * 4-7 *"); 72 Date date = new Date(Date.parse("Aug 6, 2009 1:11 AM")); 73 System.out.println("============ test2 : " + date + "============="); 38 74 Schedule sche1 = new Schedule.Builder("awef").build(); 39 75 Schedule sche2 = new Schedule.Builder("awef").buildHourly(); … … 49 85 Schedule sche11 = new Schedule.Builder("awef").build("0 0 5,28 3 1"); 50 86 51 System.out.println( Job.getNextOccurence(sche1,date));52 System.out.println( Job.getNextOccurence(sche2,date));53 System.out.println( Job.getNextOccurence(sche3,date));54 System.out.println( Job.getNextOccurence(sche4,date));55 System.out.println( Job.getNextOccurence(sche5,date));56 System.out.println( Job.getNextOccurence(sche6,date));57 System.out.println( Job.getNextOccurence(sche7,date));58 System.out.println( Job.getNextOccurence(sche8,date));59 System.out.println( Job.getNextOccurence(sche9,date));60 System.out.println( Job.getNextOccurence(sche10,date));61 System.out.println( Job.getNextOccurence(sche11,date));87 System.out.println(sche1.getNextOccurence(date)); 88 System.out.println(sche2.getNextOccurence(date)); 89 System.out.println(sche3.getNextOccurence(date)); 90 System.out.println(sche4.getNextOccurence(date)); 91 System.out.println(sche5.getNextOccurence(date)); 92 System.out.println(sche6.getNextOccurence(date)); 93 System.out.println(sche7.getNextOccurence(date)); 94 System.out.println(sche8.getNextOccurence(date)); 95 System.out.println(sche9.getNextOccurence(date)); 96 System.out.println(sche10.getNextOccurence( date)); 97 System.out.println(sche11.getNextOccurence( date)); 62 98 99 assertEquals(sche1.getNextOccurence(date).toString(),"Thu Aug 06 01:11:00 KST 2009"); 100 assertEquals(sche2.getNextOccurence(date).toString(),"Thu Aug 06 02:00:00 KST 2009"); 101 assertEquals(sche3.getNextOccurence(date).toString(),"Fri Aug 07 00:00:00 KST 2009"); 102 assertEquals(sche4.getNextOccurence(date).toString(),"Sun Aug 09 00:00:00 KST 2009"); 103 assertEquals(sche5.getNextOccurence(date).toString(),"Fri Jan 01 00:00:00 KST 2010"); 104 assertEquals(sche6.getNextOccurence(date).toString(),"Thu Aug 06 01:30:00 KST 2009"); 105 assertEquals(sche7.getNextOccurence(date).toString(),"Thu Aug 06 01:17:00 KST 2009"); 106 assertEquals(sche8.getNextOccurence(date).toString(),"Thu Aug 06 03:03:00 KST 2009"); 107 assertEquals(sche9.getNextOccurence(date).toString(),"Fri Aug 28 00:00:00 KST 2009"); 108 assertEquals(sche10.getNextOccurence( date).toString(),"Fri Mar 05 00:00:00 KST 2010"); 109 assertEquals(sche11.getNextOccurence( date).toString(),"Mon Mar 01 00:00:00 KST 2010"); 110 } 63 111 64 assertEquals(Job.getNextOccurence(sche1, date).toString(),"Fri Feb 27 20:14:00 KST 2009");65 assertEquals(Job.getNextOccurence(sche2, date).toString(),"Fri Feb 27 21:00:00 KST 2009");66 assertEquals(Job.getNextOccurence(sche3, date).toString(),"Sat Feb 28 00:00:00 KST 2009");67 assertEquals(Job.getNextOccurence(sche4, date).toString(),"Sun Mar 01 00:00:00 KST 2009");68 assertEquals(Job.getNextOccurence(sche5, date).toString(),"Fri Jan 01 00:00:00 KST 2010");69 assertEquals(Job.getNextOccurence(sche6, date).toString(),"Fri Feb 27 20:30:00 KST 2009");70 assertEquals(Job.getNextOccurence(sche7, date).toString(),"Fri Feb 27 20:17:00 KST 2009");71 assertEquals(Job.getNextOccurence(sche8, date).toString(),"Fri Feb 27 21:03:00 KST 2009");72 assertEquals(Job.getNextOccurence(sche9, date).toString(),"Sat Feb 28 00:00:00 KST 2009");73 assertEquals(Job.getNextOccurence(sche10, date).toString(),"Thu Mar 05 00:00:00 KST 2009");74 assertEquals(Job.getNextOccurence(sche11, date).toString(),"Mon Mar 02 00:00:00 KST 2009");112 @Test 113 public void testDaily() throws InvalidSyntaxException, Exception{ 114 Date date1 = new Date(); 115 Schedule sche = new Schedule.Builder("aef").buildDaily(); 116 117 System.out.println("============ daily : " + date1 + "============="); 118 for(int i = 0 ; i < 20; i++){ 119 date1 = sche.getNextOccurence( date1); 120 System.out.println(date1); 121 date1 = new Date(date1.getTime() + 60 * 1000); 122 } 75 123 } 76 77 public void testNext2() throws InvalidSyntaxException, Exception { 78 79 80 // Schedule sche1 = new 81 // Schedule.Builder("test1").build("*/30 * 3,18 * 0"); 82 // Schedule sche2 = new 83 // Schedule.Builder("test2").build("20-40 * * 4-7 *"); 84 Date date = new Date(Date.parse("Aug 6, 2009 1:11 AM")); 85 System.out.println("============" + date + "============="); 86 Schedule sche1 = new Schedule.Builder("awef").build(); 87 Schedule sche2 = new Schedule.Builder("awef").buildHourly(); 88 Schedule sche3 = new Schedule.Builder("awef").buildDaily(); 89 Schedule sche4 = new Schedule.Builder("awef").buildWeekly(); 90 Schedule sche5 = new Schedule.Builder("awef").buildYearly(); 91 Schedule sche6 = new Schedule.Builder("awef").build("*/30 * * * *"); 92 Schedule sche7 = new Schedule.Builder("awef").build("3,17,20 * * * *"); 93 Schedule sche8 = new Schedule.Builder("awef") 94 .build("3,17,20 */3 * * *"); 95 Schedule sche9 = new Schedule.Builder("awef").build("0 0 5,28 * *"); 96 Schedule sche10 = new Schedule.Builder("awef").build("0 0 5,28 3 *"); 97 Schedule sche11 = new Schedule.Builder("awef").build("0 0 5,28 3 1"); 98 99 System.out.println(Job.getNextOccurence(sche1, date)); 100 System.out.println(Job.getNextOccurence(sche2, date)); 101 System.out.println(Job.getNextOccurence(sche3, date)); 102 System.out.println(Job.getNextOccurence(sche4, date)); 103 System.out.println(Job.getNextOccurence(sche5, date)); 104 System.out.println(Job.getNextOccurence(sche6, date)); 105 System.out.println(Job.getNextOccurence(sche7, date)); 106 System.out.println(Job.getNextOccurence(sche8, date)); 107 System.out.println(Job.getNextOccurence(sche9, date)); 108 System.out.println(Job.getNextOccurence(sche10, date)); 109 System.out.println(Job.getNextOccurence(sche11, date)); 110 111 assertEquals(Job.getNextOccurence(sche1, date).toString(),"Thu Aug 06 01:11:00 KST 2009"); 112 assertEquals(Job.getNextOccurence(sche2, date).toString(),"Thu Aug 06 02:00:00 KST 2009"); 113 assertEquals(Job.getNextOccurence(sche3, date).toString(),"Fri Aug 07 00:00:00 KST 2009"); 114 assertEquals(Job.getNextOccurence(sche4, date).toString(),"Sun Aug 09 00:00:00 KST 2009"); 115 assertEquals(Job.getNextOccurence(sche5, date).toString(),"Fri Jan 01 00:00:00 KST 2010"); 116 assertEquals(Job.getNextOccurence(sche6, date).toString(),"Thu Aug 06 01:30:00 KST 2009"); 117 assertEquals(Job.getNextOccurence(sche7, date).toString(),"Thu Aug 06 01:17:00 KST 2009"); 118 assertEquals(Job.getNextOccurence(sche8, date).toString(),"Thu Aug 06 03:03:00 KST 2009"); 119 assertEquals(Job.getNextOccurence(sche9, date).toString(),"Fri Aug 28 00:00:00 KST 2009"); 120 assertEquals(Job.getNextOccurence(sche10, date).toString(),"Fri Mar 05 00:00:00 KST 2010"); 121 assertEquals(Job.getNextOccurence(sche11, date).toString(),"Mon Mar 01 00:00:00 KST 2010"); 124 125 @Test 126 public void testWeekly() throws InvalidSyntaxException, Exception{ 127 Date date1 = new Date(); 128 Schedule sche = new Schedule.Builder("aef").buildWeekly(); 129 130 System.out.println("============ weekly : " + date1 + "============="); 131 for(int i = 0 ; i < 20; i++){ 132 date1 = sche.getNextOccurence( date1); 133 System.out.println(date1); 134 date1 = new Date(date1.getTime() + 60 * 1000); 135 } 122 136 } 123 137 138 @Test 139 public void testMondayAnd10th() throws InvalidSyntaxException, Exception{ 140 Date date1 = new Date(); 141 Schedule sche = new Schedule.Builder("aef").build("0 0 */10 * 1"); 142 143 System.out.println("============ mondayAnd10th : " + date1 + "============="); 144 for(int i = 0 ; i < 20; i++){ 145 date1 = sche.getNextOccurence( date1); 146 System.out.println(date1); 147 date1 = new Date(date1.getTime() + 60 * 1000); 148 } 149 } 150 151 @Test 152 public void test31th() throws InvalidSyntaxException, Exception{ 153 Date date1 = new Date(Date.parse("Feb 27, 2009 8:14 PM")); 154 Schedule sche = new Schedule.Builder("aef").build("0 0 29 * *"); 155 156 System.out.println("============ 31th : " + date1 + "============="); 157 for(int i = 0 ; i < 20; i++){ 158 date1 = sche.getNextOccurence( date1); 159 System.out.println(date1); 160 Calendar cal = Calendar.getInstance(); 161 cal.setTime(date1); 162 assertTrue(cal.get(Calendar.DAY_OF_MONTH) == 29); 163 date1 = new Date(date1.getTime() + 60 * 1000); 164 } 165 } 124 166 } -
kraken-cron/src/main/java/org/krakenapps/cron/test/ScheduleBuildTest.java
r321 r323 3 3 import java.text.ParseException; 4 4 5 import junit.framework.TestCase;5 import org.junit.Test; 6 6 import org.krakenapps.cron.Schedule; 7 7 import org.krakenapps.cron.impl.CronField; 8 8 import org.osgi.framework.InvalidSyntaxException; 9 9 10 public class ScheduleBuildTest extends TestCase { 10 import static org.junit.Assert.*; 11 11 12 public class ScheduleBuildTest { 13 @Test 12 14 public void testBuildManual() throws Exception { 13 try {14 Schedule test = new Schedule.Builder("daily").set(15 CronField.Type.Day_of_Week, "0").set(16 CronField.Type.Day_of_Month, "*/10").set(CronField.Type.Hour,17 "4-10").set(CronField.Type.Minute, "0,3,59").set(18 CronField.Type.Month, "12").build();19 Schedule test2 = new Schedule.Builder("daily").set(20 CronField.Type.Day_of_Week, "3").set(21 CronField.Type.Day_of_Month, "1").set(CronField.Type.Hour,22 "0-23").set(CronField.Type.Minute, "2,3,3").build();15 try { 16 Schedule test = new Schedule.Builder("daily").set( 17 CronField.Type.Day_of_Week, "0").set( 18 CronField.Type.Day_of_Month, "*/10").set( 19 CronField.Type.Hour, "4-10").set(CronField.Type.Minute, 20 "0,3,59").set(CronField.Type.Month, "12").build(); 21 Schedule test2 = new Schedule.Builder("daily").set( 22 CronField.Type.Day_of_Week, "3").set( 23 CronField.Type.Day_of_Month, "1").set(CronField.Type.Hour, 24 "0-23").set(CronField.Type.Minute, "2,3,3").build(); 23 25 24 System.out.println("test1" + test);25 System.out.println("test2" + test2);26 System.out.println("test1" + test); 27 System.out.println("test2" + test2); 26 28 27 assertEquals(test.fieldMembers(CronField.Type.Minute), "{0, 3, 59}");28 assertEquals(test.fieldMembers(CronField.Type.Day_of_Month),29 "{0, 10, 20, 30}"); // interval30 assertEquals(test.fieldMembers(CronField.Type.Hour),31 "{4, 5, 6, 7, 8, 9, 10}"); // range32 assertEquals(test.fieldMembers(CronField.Type.Month), "{11}");29 assertEquals(test.fieldMembers(CronField.Type.Minute), "{0, 3, 59}"); 30 assertEquals(test.fieldMembers(CronField.Type.Day_of_Month), 31 "{0, 10, 20, 30}"); // interval 32 assertEquals(test.fieldMembers(CronField.Type.Hour), 33 "{4, 5, 6, 7, 8, 9, 10}"); // range 34 assertEquals(test.fieldMembers(CronField.Type.Month), "{11}"); 33 35 34 assertEquals(test2.fieldMembers(CronField.Type.Minute), "{2, 3}"); // specify35 assertEquals(test2.fieldMembers(CronField.Type.Month),36 "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"); // default36 assertEquals(test2.fieldMembers(CronField.Type.Minute), "{2, 3}"); // specify 37 assertEquals(test2.fieldMembers(CronField.Type.Month), 38 "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"); // default 37 39 38 assertEquals(test, new Schedule.Builder("daily")39 .build("0,3,59 4-10 */10 12 0"));40 } catch (Exception e) {40 assertEquals(test, new Schedule.Builder("daily") 41 .build("0,3,59 4-10 */10 12 0")); 42 } catch (Exception e) { 41 43 fail(); 42 44 // TODO: handle exception 43 45 } 46 } 47 48 @Test 49 public void testBuildString() throws InvalidSyntaxException, Exception { 50 try { 51 Schedule test3 = new Schedule.Builder("daily").build("0 0 1 1 3-6"); 52 System.out.println("test3" + test3); 44 53 45 } 46 47 public void testBuildString() throws InvalidSyntaxException, Exception { 48 try{ 49 Schedule test3 = new Schedule.Builder("daily").build("0 0 1 1 3-6"); 50 System.out.println("test3" + test3); 51 52 assertEquals(test3.fieldMembers(CronField.Type.Day_of_Week), 53 "{3, 4, 5, 6}"); 54 assertEquals(test3.fieldMembers(CronField.Type.Day_of_Month), "{0}"); 55 assertEquals(test3.fieldMembers(CronField.Type.Month), "{0}"); 56 }catch (Exception e) { 57 fail(); 58 // TODO: handle exception 59 } 60 61 } 62 63 public void testBuildShort() throws InvalidSyntaxException, Exception { 64 try{ 65 Schedule test8 = new Schedule.Builder("daily").build(); 66 Schedule test4 = new Schedule.Builder("daily").buildYearly(); 67 Schedule test5 = new Schedule.Builder("daily").buildMonthly(); 68 Schedule test6 = new Schedule.Builder("daily").buildDaily(); 69 Schedule test7 = new Schedule.Builder("daily").buildWeekly(); 70 System.out.println("test8" + test8); 71 System.out.println("test4" + test4); 72 System.out.println("test5" + test5); 73 System.out.println("test6" + test6); 74 System.out.println("test7" + test7); 75 76 assertEquals(test8, new Schedule.Builder("daily").build("* * * * * ")); 77 assertEquals(test4, new Schedule.Builder("daily").build("0 0 1 1 *")); 78 assertEquals(test5, new Schedule.Builder("daily").build("0 0 1 * *")); 79 assertEquals(test6, new Schedule.Builder("daily").build("0 0 * * *")); 80 assertEquals(test7, new Schedule.Builder("daily").build("0 0 * * 0")); 54 assertEquals(test3.fieldMembers(CronField.Type.Day_of_Week), 55 "{3, 4, 5, 6}"); 56 assertEquals(test3.fieldMembers(CronField.Type.Day_of_Month), "{0}"); 57 assertEquals(test3.fieldMembers(CronField.Type.Month), "{0}"); 81 58 } catch (Exception e) { 82 59 fail(); … … 86 63 } 87 64 65 @Test 66 public void testBuildShort() throws InvalidSyntaxException, Exception { 67 try { 68 Schedule test8 = new Schedule.Builder("daily").build(); 69 Schedule test4 = new Schedule.Builder("daily").buildYearly(); 70 Schedule test5 = new Schedule.Builder("daily").buildMonthly(); 71 Schedule test6 = new Schedule.Builder("daily").buildDaily(); 72 Schedule test7 = new Schedule.Builder("daily").buildWeekly(); 73 System.out.println("test8" + test8); 74 System.out.println("test4" + test4); 75 System.out.println("test5" + test5); 76 System.out.println("test6" + test6); 77 System.out.println("test7" + test7); 78 79 assertEquals(test8, new Schedule.Builder("daily") 80 .build("* * * * * ")); 81 assertEquals(test4, new Schedule.Builder("daily") 82 .build("0 0 1 1 *")); 83 assertEquals(test5, new Schedule.Builder("daily") 84 .build("0 0 1 * *")); 85 assertEquals(test6, new Schedule.Builder("daily") 86 .build("0 0 * * *")); 87 assertEquals(test7, new Schedule.Builder("daily") 88 .build("0 0 * * 0")); 89 } catch (Exception e) { 90 fail(); 91 // TODO: handle exception 92 } 93 94 } 95 96 @Test 88 97 public void testSafety() throws Exception { 89 try {90 Schedule.Builder safe_builder = new Schedule.Builder("daily");91 Schedule safe_test = safe_builder.build();92 System.out.println("safe_test1" + safe_test);93 safe_builder.set(CronField.Type.Hour, "3");94 System.out.println("safe_test2" + safe_test);95 assertEquals(safe_test, new Schedule.Builder("daily").build());96 } catch (Exception e) {98 try { 99 Schedule.Builder safe_builder = new Schedule.Builder("daily"); 100 Schedule safe_test = safe_builder.build(); 101 System.out.println("safe_test1" + safe_test); 102 safe_builder.set(CronField.Type.Hour, "3"); 103 System.out.println("safe_test2" + safe_test); 104 assertEquals(safe_test, new Schedule.Builder("daily").build()); 105 } catch (Exception e) { 97 106 fail(); 98 107 // TODO: handle exception … … 100 109 } 101 110 111 @Test 102 112 public void testCollision() { 103 113 try { … … 120 130 } 121 131 132 @Test 122 133 public void testException() { 123 134 try { … … 126 137 } catch (ParseException e) { 127 138 // TODO Auto-generated catch block 128 } catch (InvalidSyntaxException e) { 129 // TODO Auto-generated catch block 130 } 139 } 131 140 try { 132 141 new Schedule.Builder("daily").set(CronField.Type.Day_of_Month, "0"); … … 134 143 } catch (ParseException e) { 135 144 // TODO Auto-generated catch block 136 } catch (InvalidSyntaxException e) { 137 // TODO Auto-generated catch block 138 } 145 } 139 146 try { 140 147 new Schedule.Builder("daily").set(CronField.Type.Hour, "24"); … … 142 149 } catch (ParseException e) { 143 150 // TODO Auto-generated catch block 144 } catch (InvalidSyntaxException e) { 145 // TODO Auto-generated catch block 146 } 151 } 147 152 try { 148 153 new Schedule.Builder("daily").set(CronField.Type.Hour, "-4"); … … 150 155 } catch (ParseException e) { 151 156 // TODO Auto-generated catch block 152 } catch (InvalidSyntaxException e) { 153 // TODO Auto-generated catch block 154 } 157 } 155 158 try { 156 159 new Schedule.Builder("daily").build("0 0 0 0"); … … 158 161 } catch (ParseException e) { 159 162 // TODO Auto-generated catch block 160 } catch (InvalidSyntaxException e) { 161 // TODO Auto-generated catch block 162 } catch (Exception e) { 163 }catch (Exception e) { 163 164 // TODO Auto-generated catch block 164 165 }
