1、静态设置:示例,每五分钟执行一次myTask xml: 1 <bean id="mySchedule" class="test.MySchedule"></bean>
2
3 <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
4 <property name="triggers">
5 <list>
6 <ref bean="myTrigger" />
7 </list>
8 </property>
9 <property name="configLocation" value="classpath:quartz.properties"/>
10 </bean>
11 <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
12 <property name="jobDetail" ref="myTaskMethod"/>
13 <property name="cronExpression" value="0 0/5 * * * ?"/>
14 </bean>
15 <bean id="myTaskMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
16 <property name="targetObject" ref="mySchedule"/>
17 <property name="targetMethod" value="myTask"/>
18 </bean>
java:1 public class MySchedule {
2 public void myTask(){
3 System.out.println("do my task at time: " + new Date());
4 }
5 }
2、动态设置:示例,程序运行过程中,后台管理人员指定5个小时后执行一次发短信的任务 xml: 1 <task:scheduler id="taskScheduler" pool-size="10"/>
java: 1 public class MySchedulerTest {
2 private TaskScheduler taskScheduler;
3
4 /**
5 *在指定的时间内执行一次发短信的操作
6 */
7 public void sendMessageDelayedAt(Date delayedTime){
8 taskScheduler.schedule(new Runnable() {
9 @Override
10 public void run() {
11 System.out.println("send message at time: " + new Date());
12 }
13 }, delayedTime);
14 }
15 }
更多配置参考spring文档:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html#scheduling-quartz