1.【第一种使用方式】需要加入两段XML声明
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
创建类
package org.wittysource.test.task;
public class TestTask {
public void testShow(){
System.out.println("Task run......");
}
}
定义Bean
<bean id="testTask" class="org.wittysource.task.TestTask"></bean>
<task:scheduled-tasks>
<task:scheduled ref="testTask" method="testShow" cron="0 46 14 * * ?"/>
</task:scheduled-tasks>
2.【第二种方式 extends QuartzJobBean】
<bean name="job.vgic" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="org.wittysource.task.TestTask" /> <property name="jobDataAsMap"> <map> <entry key="triggerMessage" value="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" /> </map> </property> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="job.vgic" /> <property name="startDelay" value="10000"/> <!-- repeat every 10 seconds --> <property name="repeatInterval" value="1000"/> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job.vgic" /> <!-- s m h d M --> <property name="cronExpression" value="0 32 16 * * ?" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean>
3.第三种方式
<bean id="obj" class="org.wittysource.test.TestBatch" /> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="obj" /> <property name="targetMethod" value="doShow" /> <property name="concurrent" value="true" /> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobDetail" /> <!-- run every morning at 6 AM --> <property name="cronExpression" value="0 38 17 * * ?" /> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <!-- see the example of method invoking job above --> <property name="jobDetail" ref="jobDetail" /> <!-- 10 seconds --> <property name="startDelay" value="10000" /> <!-- repeat every 50 seconds --> <property name="repeatInterval" value="1000" /> </bean> <!-- 指定触发器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> <ref bean="simpleTrigger" /> </list> </property> </bean>