目的:实现动态的定时任务脚本框架(增删改定时脚本无需重启服务)
基础:J2SE、Groovy、cron表达式
首先贴出解析cron表达式的类,本人对自己写的这个类并不满意,如果大神有更好的实现还请告诉我一下。
package com.hdb.schedule.common /** * @author MwXx * @version 1.0 * @date 2014-04-21 */ class CronExpression { private Set<Integer> minuteSet private Set<Integer> hourSet private Set<Integer> dateSet private Set<Integer> monthSet private Set<Integer> weekSet private Set<Integer> yearSet String error private crontab public CronExpression(crontab){ this.crontab = crontab this.buildExpression(this.crontab) } private void buildExpression(crontab) throws Exception{ minuteSet = new HashSet<Integer>() hourSet = new HashSet<Integer>() dateSet = new HashSet<Integer>() monthSet = new HashSet<Integer>() weekSet = new HashSet<Integer>() yearSet = new HashSet<Integer>() try{ int size = crontab.size() 6.times {index-> def val if(size > index){ val = crontab[index].toString().trim() }else{ val = '*' } val.split(',').each { addToSet(index, it) } } }catch(Exception e){ } } private void addValue(int index,int val){ switch(index){ case 0: minuteSet.add(val) break case 1: hourSet.add(val) break case 2: dateSet.add(val) break case 3: monthSet.add(val) break case 4: