1 编制我们的任务执行类
public class ExampleBusinessObject {
public void doIt() {
System.out.println(System.currentTimeMillis());
}
}2 配置Spring的配置文件
<bean id="exampleBusinessObject" class="ExampleBusinessObject" />
<bean id="methodInvokingJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="exampleBusinessObject" />
<property name="targetMethod" value="doIt" />
</bean>
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- see the example of method invoking job above -->
<property name="jobDetail" ref="methodInvokingJobDetail" />
<!-- 10 seconds -->
<property name="startDelay" value="1000" />
<!-- repeat every 50 seconds -->
<property name="repeatInterval" value="5000" />
</bean>
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="methodInvokingJobDetail" />
<!-- run every morning at 6 AM -->
<property name="cronExpression" value="0 * * * * ?" />
</bean>
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
<ref bean="simpleTrigger" />
</list>
</property>
</bean><property name="cronExpression" value="0 * * * * ?" /> 是关键,
0 * * * * ? 代表每分钟的第0秒
0 0 * * * ? 代表每小时的第0分,第0秒,当然
0 0 0 * * ? 每天的0:0:0
具体的请参考Quartz的说明
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /