略
Quartz的使用
开启一个简单的Quartz
1 | //任务调度器工厂 |
加上任务
1 | SchedulerFactory sf = new StdSchedulerFactory(); |
任务的具体内容Job接口的具体实现
1 | public class HelloJob implements Job { |
结合Spring来使用
导包
1
2
3
4
5
6
7
8
9
10<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40<!-- 扫描Service注解 -->
<context:component-scan base-package="com.yuda" />
<!-- 具体任务内容 -->
<bean id="helloJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.yuda.job.HelloJob"/>
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="5"/>
</map>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean" id="promotionJob">
<property name="jobClass" value="com.yuda.quartz.PromotionJob"/>
</bean>
<!-- ======================== 调度触发器 ======================== -->
<!-- 什么时间做什么事 -->
<bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="helloJob"></property>
<property name="cronExpression" value="0 54 * * * ?"></property>
</bean>
<!-- 60秒一次 -->
<bean class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean" id="simpleTrigger">
<property name="jobDetail" ref="promotionJob"/>
<property name="startDelay" value="0"/>
<property name="repeatInterval" value="60000"/>
</bean>
<!-- ======================== 调度工厂 ======================== -->
<!-- 任务调度主入口 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory" ref="jobFactory" />
<property name="triggers">
<list>
<ref bean="cronTriggerBean" />
<ref bean="simpleTrigger" />
</list>
</property>
</bean>JobFactory
为了把QuartzJobBean转换为一个bean,为了让service可以注入到JobBean中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Service;
public class JobFactory extends AdaptableJobFactory {
private AutowireCapableBeanFactory capableBeanFactory;
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
// 调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
// 进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}注意:该类的写法固定
具体任务内容(继承QuartzJobBean类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.yuda.service.HelloService;
public class HelloJob extends QuartzJobBean {
private HelloService helloService;
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
helloService.sayHello();
}
}jobFactory的具体作用就是把上述类声明为一个Spring管理的bean,只有这样,
private HelloService helloService
才可以被自动装配到HelloJob中.