Spring集成Scheduler调度器(XML版本)
- 第一步:定义一个执行任务的类
- ExampleJob 继承 QuartzJobBean , 并重写executeInternal这个方法,实现具体的任务
package example;
public class ExampleJob extends QuartzJobBean {
private int timeout;
/**
* Setter called after the ExampleJob is instantiated
* with the value from the JobDetailFactoryBean (5)
*/
public void setTimeout(int timeout) {
this.timeout = timeout;
}
/**
* 实现具体的任务
*/
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
// do the actual work
}
}
- 第二步: 定义一个 JobDetail , 用来 管理 Job
<bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="example.ExampleJob"/>
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="5"/>
</map>
</property>
</bean>
- 第三步: 定义一个 Trigger触发器, 用来设置 任务执行的时间 ,频率等信息
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="exampleJob"/>
<!-- run every morning at 6 AM -->
<property name="cronExpression" value="0 0 6 * * ?"/>
</bean>
- 第四步: 定义一个 Scheduler , 管理 Trigger
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
Spring官方参考地址
https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#scheduling