1. 目的:动态设置时间,触发相应的任务
2. 系统架构为 struts + spring + hibernate
3. 实现步骤:
-
在页面上设置时间;
-
将时间转换为Unix Cron Expression;
-
将转换后的时间规则表达式记录到数据库中(也可以写入xml文件中,这里是项目需要记录入数据库中);
-
从数据库中得到相应的时间规则表达式;
-
更新您的任务触发器的时间设置;
-
RESCHEDULE THE JOB。; - )
4. 具体实现细节:
1) 在页面上设置时间
根据具体的业务需求,设置时间规则,下面以某一项目为例,需要 按每月、每周、自定义分为三种规则。
每月则需要选择该月的第几个星期的星期几
每周则需要选择星期几
自定义则选择具体的日期,如 2007-1-10
三种规则都需要设定时间点
OK. 这样我们的页面设置就完成了。: - )
上回说到,我们的设置页面已经做好了,接下来就是将时间转换为Unix Cron Expression。
2) 将时间转换为Unix Cron Expression
需要ActionForm将页面表单数据映射到Action中,然后在Action中转换为cron expression:
1
< tr >
2
< th > 执行这个任务 </ th >
3
< td style ="font-weight:bold;" >
4
< html:radio property ="everyWhat" styleClass ="InputBorderNone" value ="monthly" onclick ="changeStatus(this.value)" > 每月 </ html:radio >
5
< html:radio property ="everyWhat" styleClass ="InputBorderNone" value ="weekly" onclick ="changeStatus(this.value)" > 每周 </ html:radio >
6
< html:radio property ="everyWhat" styleClass ="InputBorderNone" value ="userDefined" onclick ="changeStatus(this.value)" > 自定义 </ html:radio >
7
< html:hidden property ="jobName" value ="compare" />
8
</ td >
9
</ tr >
10

2

3

4

5

6

7

8

9

10

每月则需要选择该月的第几个星期的星期几
1
< tr style ="display:" id ="whichWeek" >
2
< th > 选择第几个星期 </ th >
3
< td style ="font-weight:bold;" >
4
< html:select property ="week" >
5
< html:option value ="1" > 一 </ html:option >
6
< html:option value ="2" > 二 </ html:option >
7
< html:option value ="3" > 三 </ html:option >
8
< html:option value ="4" > 四 </ html:option >
9
</ html:select >
10
< html:select property ="dayOfMonth" >
11
< html:option value ="1" > 星期日 </ html:option >
12
< html:option value ="2" > 星期一 </ html:option >
13
< html:option value ="3" > 星期二 </ html:option >
14
< html:option value ="4" > 星期三 </ html:option >
15
< html:option value ="5" > 星期四 </ html:option >
16
< html:option value ="6" > 星期五 </ html:option >
17
< html:option value ="7" > 星期六 </ html:option >
18
</ html:select >
19
</ td >
20
</ tr >
21

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

每周则需要选择星期几
1
< tr style ="display:none" id ="whichDay" >
2
< th > 选择星期 </ th >
3
< td style ="font-weight:bold;" >
4
< html:select property ="dayOfWeek" >
5
< html:option value ="1" > 星期日 </ html:option >
6
< html:option value ="2" > 星期一 </ html:option >
7
< html:option value ="3" > 星期二 </ html:option >
8
< html:option value ="4" > 星期三 </ html:option >
9
< html:option value ="5" > 星期四 </ html:option >
10
< html:option value ="6" > 星期五 </ html:option >
11
< html:option value ="7" > 星期六 </ html:option >
12
</ html:select >
13
</ td >
14 </ tr >
15

2

3

4

5

6

7

8

9

10

11

12

13

14 </ tr >
15

自定义则选择具体的日期,如 2007-1-10
三种规则都需要设定时间点
1
< tr >
2
< th > 起始时间 </ th >
3
< td style ="font-weight:bold;" >
4
< html:select property ="timeType" styleId ="type" onchange ="changeStatus2(this.value)" >
5
< html:option value ="AM" > 上午 </ html:option >
6
< html:option value ="PM" > 下午 </ html:option >
7
</ html:select >
8
< html:select property ="hour" styleId ="amHours" >
9
< html:option value ="1" > 1 </ html:option >
10
< html:option value ="2" > 2 </ html:option >
11
< html:option value ="3" > 3 </ html:option >
12
< html:option value ="4" > 4 </ html:option >
13
< html:option value ="5" > 5 </ html:option >
14
< html:option value ="6" > 6 </ html:option >
15
< html:option value ="7" > 7 </ html:option >
16
< html:option value ="8" > 8 </ html:option >
17
< html:option value ="9" > 9 </ html:option >
18
< html:option value ="10" > 10 </ html:option >
19
< html:option value ="11" > 11 </ html:option >
20
< html:option value ="12" > 12 </ html:option >
21
</ html:select >
22
< html:select property ="hour" styleId ="pmHours" style ="display:none" >
23
< html:option value ="13" > 13 </ html:option >
24
< html:option value ="14" > 14 </ html:option >
25
< html:option value ="15" > 15 </ html:option >
26
< html:option value ="16" > 16 </ html:option >
27
< html:option value ="17" > 17 </ html:option >
28
< html:option value ="18" > 18 </ html:option >
29
< html:option value ="19" > 19 </ html:option >
30
< html:option value ="20" > 20 </ html:option >
31
< html:option value ="21" > 21 </ html:option >
32
< html:option value ="22" > 22 </ html:option >
33
< html:option value ="23" > 23 </ html:option >
34
< html:option value ="0" > 0 </ html:option >
35
</ html:select > 点
36
< html:text property ="minute" name ="minute" style ="width:20px;" value ="0" onchange ="valTime(this.value)" /> 分
37
< html:text property ="second" name ="second" style ="width:20px;" value ="0" onchange ="valTime(this.value)" /> 秒(0-59之间的整数)
38
</ td >
39
</ tr >
40

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

OK. 这样我们的页面设置就完成了。: - )
上回说到,我们的设置页面已经做好了,接下来就是将时间转换为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 = 

2

3

4

