什么是Quartz
Quartz是一个由java编写的开源作业调度框架,简单来说,Quartz就是启动定时任务的框架,像linux系统中的corntab,可以定时启动任务.
Quartz的单独使用
Scheduler 定时器对象
JobDetail 任务对象
Trigger 触发器对象
SimpleTrigger 简单触发器
CornTrigger任务调度触发器
使用
导入Maven坐标
<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>
工作对象
//要实现Job接口
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello Quartz~~~");
}
}
测试用例
//定时器对象
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//定义一个工作对象 设置工作名称与组名
JobDetail job =JobBuilder.newJob(HelloJob.class).withIdentity("job41","group1").build();
//定义一个触发器 简单Trigger 设置工作名称与组名 5秒触发一次
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group1").startNow().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)).build();
//定义一个任务调度的Trigger 设置工作名称与组名 每天的24:00触发一次
//Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group1").withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build();
//设置工作 与触发器
scheduler.scheduleJob(job, trigger);
// and start it off
//开始定时任务
scheduler.start();
CornTrigger表达式的写法
* * * * * *
分别代表秒、分、时、日、月、周
例子:
0 0 12 ? * 3 每周二中午 12 点执行任务
0 0 0 0 * ? 每月 1 日 0 点执行任务
测试结果
Quartz与spring的整合
导入Quartz和Spring的Maven依赖
<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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
配置ApplicationContext文件
<!-- 扫描包 -->
<context:component-scan base-package="包名"></context:component-scan>
<!-- job -->
<bean id="helloJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.lzh.quartz.job.HelloJob"></property>
</bean>
<!-- trigger -->
<bean id="simpleTriger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="helloJob"></property>
<!-- 三秒后执行 -->
<property name="startDelay" value="3000"></property>
<!-- 五秒重复执行一次 -->
<property name="repeatInterval" value="5000"></property>
</bean>
<!-- scheduler -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- <property name="jobFactory" ref="jobFactory" ></property>
--> <property name="triggers">
<list>
<ref bean="simpleTriger"/>
</list>
</property>
</bean>
Job对象
public class HelloJob implements Job {
/*@Autowired
private HelloService helloService;*/
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
/* helloService.sayHello();
*/ System.out.println("hello,job Spring");
}
}
Quartz与spring整合Bean无法注入的问题
当Quartz要注入Spring Bean对象时,会报错,因为Job对象不是Spring进行管理的,当job对象中注入Spring Bean会产生空指针异常 即无法注入
解决办法: 写一个JobFactory ,在ApplicationContext 中配置jobFactory,由JobFactory生成job时候,自动注入Spring Bean对象
JobFactory
@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
Object jobInstance = super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
在Secheduler中配置jobFactory
测试结果
运行maven项目的时候,自动开启Scheduler
测试成功