posts - 40,  comments - 187,  trackbacks - 0
书接上回,
上回说到,武松武二郎斗杀西门庆,为大哥报了仇……  !!!-_- 啊,说串了,嘿嘿 不好意思
(删掉上面一行)
上回说到,我们的设置页面已经做好了,接下来就是将时间转换为Unix Cron Expression。

2) 将时间转换为Unix Cron Expression

需要ActionForm将页面表单数据映射到Action中,然后在Action中转换为cron expression:

 1 SchedulerForm schedulerForm  =  (SchedulerForm) form;
 2         String jobName  =  schedulerForm.getJobName();
 3         String cronExpression  =   "" ;
 4         String[] commonNeeds  =   {schedulerForm.getSecond(), schedulerForm.getMinute(), schedulerForm.getHour()} ;
 5         String[] monthlyNeeds  =   {schedulerForm.getWeek(), schedulerForm.getDayOfMonth()} ;
 6         String weeklyNeeds  =  schedulerForm.getDayOfWeek();
 7         String userDefinedNeeds  =  schedulerForm.getDate();
 8         String everyWhat  =  schedulerForm.getEveryWhat();
 9          // 得到时间规则
10         cronExpression  =  CronExpConversion.getCronExpression(everyWhat, commonNeeds,
11                 monthlyNeeds, weeklyNeeds, userDefinedNeeds);
12

我定义了一个 规则类来处理转换规则(写得不是很好 能用就行 嘿嘿)
 1
 2 /**
 3  * 页面设置转为UNIX cron expressions 转换类
 4  * CronExpConversion
 5   */

 6 public   class  CronExpConversion  {
 7     
 8      /**
 9      * 页面设置转为UNIX cron expressions 转换算法
10      *  @param  everyWhat
11      *  @param  commonNeeds 包括 second minute hour
12      *  @param  monthlyNeeds 包括 第几个星期 星期几
13      *  @param  weeklyNeeds  包括 星期几
14      *  @param  userDefinedNeeds  包括具体时间点
15      *  @return  cron expression
16       */

17      public   static  String convertDateToCronExp(String everyWhat,
18             String[] commonNeeds, String[] monthlyNeeds, String weeklyNeeds,
19             String userDefinedNeeds)  {
20         String cronEx  =   "" ;
21         String commons  =  commonNeeds[ 0 +   "   "   +  commonNeeds[ 1 +   "   "
22                  +  commonNeeds[ 2 +   "   " ;
23         String dayOfWeek  =   "" ;
24          if  ( " monthly " .equals(everyWhat))  {
25              //  eg.: 6#3 (day 6 = Friday and "#3" = the 3rd one in the
26              //  month)
27             dayOfWeek  =  monthlyNeeds[ 1 ]
28                      +  CronExRelated.specialCharacters
29                             .get(CronExRelated._THENTH)  +  monthlyNeeds[ 0 ];
30             cronEx  =  (commons
31                      +  CronExRelated.specialCharacters.get(CronExRelated._ANY)
32                      +   "   "
33                      +  CronExRelated.specialCharacters.get(CronExRelated._EVERY)
34                      +   "   "   +  dayOfWeek  +   "   " ).trim();
35         }
  else   if  ( " weekly " .equals(everyWhat))  {
36             dayOfWeek  =  weeklyNeeds;  //  1
37             cronEx  =  (commons
38                      +  CronExRelated.specialCharacters.get(CronExRelated._ANY)
39                      +   "   "
40                      +  CronExRelated.specialCharacters.get(CronExRelated._EVERY)
41                      +   "   "   +  dayOfWeek  +   "   " ).trim();
42         }
  else   if  ( " userDefined " .equals(everyWhat))  {
43             String dayOfMonth  =  userDefinedNeeds.split( " - " )[ 2 ];
44              if  (dayOfMonth.startsWith( " 0 " ))  {
45                 dayOfMonth  =  dayOfMonth.replaceFirst( " 0 " "" );
46             }

47             String month  =  userDefinedNeeds.split( " - " )[ 1 ];
48              if  (month.startsWith( " 0 " ))  {
49                 month  =  month.replaceFirst( " 0 " "" );
50             }

51             String year  =  userDefinedNeeds.split( " - " )[ 0 ];
52              // FIXME 暂时不加年份 Quartz报错
53              /* cronEx = (commons + dayOfMonth + " " + month + " "
54                     + CronExRelated.specialCharacters.get(CronExRelated._ANY)
55                     + " " + year).trim(); */

56             cronEx  =  (commons  +  dayOfMonth  +   "   "   +  month  +   "   "
57                      +  CronExRelated.specialCharacters.get(CronExRelated._ANY)
58                      +   "   " ).trim();
59         }

60          return  cronEx;
61     }
    
62 }

63
这样就将页面的时间设置转为了Cron Expression。

                                                   To Be Continued...
posted on 2007-01-10 16:15 小立飞刀 阅读(5063) 评论(3)  编辑  收藏 所属分类: Spring

FeedBack:
# re: Spring Quartz如何动态配置时间(2)
2007-01-10 18:34 | zxy
好好好 不错不错  回复  更多评论
  
# re: Spring Quartz如何动态配置时间(2)[未登录]
2007-10-15 09:09 | CC
能否提供下CronExRelated类的代码?

貌似不是spring或者quartz包里的  回复  更多评论
  
# re: Spring Quartz如何动态配置时间(2)
2007-10-15 09:40 | 小立飞刀
@CC
这个类封装了一些Quartz时间规则的常量,便于自己使用,定义比较灵活,可以根据您的具体情况扩展。

/**
* Quartz时间规则常量类
* CronExRelated
* @author allen
*/
public class CronExRelated {

public static final String _EVERY = "every";
public static final String _ANY = "any";
public static final String _RANGES = "ranges";
public static final String _INCREMENTS = "increments";
public static final String _ADDITIONAL = "additional";
public static final String _LAST = "last";
public static final String _WEEKDAY = "weekday";
public static final String _THENTH = "theNth";
public static final String _CALENDAR = "calendar";

public static final String _TYPE = "type";

/**
* 0 0 6 ? * 1#1 ? monthly
* 0 0 6 ? * 1 ? weekly
* 0 0 6 30 7 ? 2006 useDefined
*/
static String[] headTitle = {"TYPE","SECONDS","MINUTES","HOURS","DAYOFMONTH","MONTH","DAYOFWEEK","YEAR"};

/**
* cron expression special characters
* Map
* specialCharacters
*/
public static Map specialCharacters;

static {
specialCharacters = new HashMap(10);
specialCharacters.put(_EVERY, "*");
specialCharacters.put(_ANY, "?");
specialCharacters.put(_RANGES, "-");
specialCharacters.put(_INCREMENTS, "/");
specialCharacters.put(_ADDITIONAL, ",");
specialCharacters.put(_LAST, "L");
specialCharacters.put(_WEEKDAY, "W");
specialCharacters.put(_THENTH, "#");
specialCharacters.put(_CALENDAR, "C");

specialCharacters.put(_TYPE, headTitle);
}

public static void set(String ex, int index) {
((String[])specialCharacters.get(_TYPE))[index] = ex;
}

}  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

生存或毁灭,这是个必答之问题:是否应默默的忍受坎苛命运之无情打击,还是应与深如大海之无涯苦难奋然为敌,并将其克服。此二抉择,究竟是哪个较崇高?

常用链接

留言簿(12)

随笔分类(43)

相册

收藏夹(7)

朋友的博客

电子资料

搜索

  •  

积分与排名

  • 积分 - 302059
  • 排名 - 192

最新评论

阅读排行榜

评论排行榜