此定时器可重置定时时间。
1. spring的定时器配置文件application.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
4 xmlns:context="http://www.springframework.org/schema/context" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
6 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
9 http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
10
11 <!-- 定时器bean类定义 -->
12 <bean id="×××Daemon" class="com.×××.×××.×××Daemon">
13 <!-- 指定定时器调度工厂 -->
14 <property name="scheduler" ref="timingPrintSchedulerFactory" />
15 <!-- 向定时器注入bean -->
16 <property name="×××" ref="×××" />
17 </bean>
18
19 <!-- 任务1定义 -->
20 <bean id="×××DaemonJob"
21 class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
22 <!-- 任务1所在的类 -->
23 <property name="targetObject" ref="×××Daemon" />
24 <!-- 任务1所对应的方法名 -->
25 <property name="targetMethod" value="executeInternal" />
26 <property name="concurrent" value="false" />
27 </bean>
28
29 <!-- 任务1触发器 -->
30 <bean id="×××JobTrigger"
31 class="org.springframework.scheduling.quartz.CronTriggerBean">
32 <property name="jobDetail" ref="×××DaemonJob"/>
33 <property name="cronExpression" value="0 0 0 ? * *"/> <!-- 默认每天凌晨0点整打印 -->
34 <property name="startDelay" value="10000"/> <!-- 延迟10秒(10000毫秒)启动 -->
35 </bean>
36
37 <!-- 重置定时时间任务 -->
38 <bean id="reset×××DaemonJob"
39 class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
40 <property name="targetObject" ref="×××Daemon" />
41 <property name="targetMethod" value="resetJob" />
42 <property name="concurrent" value="false" />
43 </bean>
44
45 <!-- 重置定时时间任务触发器 -->
46 <bean id="reset×××JobTrigger"
47 class="org.springframework.scheduling.quartz.SimpleTriggerBean">
48 <property name="jobDetail" ref="reset×××DaemonJob"/>
49 <property name="repeatInterval" value="10000"/> <!-- 每隔10秒扫描一次 -->
50 <property name="startDelay" value="10000"/> <!-- 延迟10秒(10000毫秒)启动 -->
51 </bean>
52
53 <!-- 调度工厂,触发器集合 -->
54 <bean id="timingPrintSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
55 <property name="triggers">
56 <list>
57 <ref bean="×××JobTrigger"/>
58 <ref bean="reset×××JobTrigger"/>
59 </list>
60 </property>
61 </bean>
62
63 </beans>
View Code
2. 定时器对应的类
1 import org.quartz.Scheduler;
2 import org.springframework.scheduling.quartz.CronTriggerBean;
3
4
5 public class TimingPrintDaemon {
6
7 /**
8 * 调度工厂,可取得所属的触发器
9 */
10 private Scheduler scheduler;
11
12 /**
13 * 任务1执行的方法
14 */
15 protected void executeInternal(){
16 // To do something
17 }
18
19 /**
20 * 重置定时时间任务的方法
21 */
22 public void resetJob()throws Exception{
23 String cronExpression = transformTime(×××);// 时间格式如:03:30 下午
24 CronTriggerBean trigger = (CronTriggerBean)scheduler.getTrigger("×××JobTrigger",Scheduler.DEFAULT_GROUP);
25 String originConExpression = trigger.getCronExpression();
26 if(!originConExpression.equalsIgnoreCase(cronExpression)){
27 trigger.setCronExpression(cronExpression);
28 scheduler.rescheduleJob("×××JobTrigger", Scheduler.DEFAULT_GROUP, trigger);
29 }
30 }
31
32 /**
33 * 将时间转换为cron表达式
34 * @param time 时间字符串,格式如:03:30 下午
35 * @return
36 */
37 private String transformTime(String time){
38 StringBuffer result = new StringBuffer();
39 String[] arr = time.split(" ");
40 String[] timeArr = arr[0].split(":");
41 result.append("0 "+timeArr[1]+" ");
42 if("上午".equalsIgnoreCase(arr[1])){
43 result.append(Integer.valueOf(timeArr[0])%12);
44 }else{
45 result.append(12+Integer.valueOf(timeArr[0])%12);
46 }
47 result.append(" ? * *");
48 return result.toString();
49 }
50
51 public Scheduler getScheduler() {
52 return scheduler;
53 }
54 public void setScheduler(Scheduler scheduler) {
55 this.scheduler = scheduler;
56 }
57
58
59 }
View Code