Quartz 16-quartz spring 整合使用
2017年12月19日大约 5 分钟
序言
有时候我们希望将 quartz 与 spring 进行整合使用。
准备工作
整体结构
│ pom.xml
│
├─src
│ └─main
│ ├─java
│ │ └─com
│ │ └─github
│ │ └─houbb
│ │ └─quartz
│ │ └─spring
│ │ │ CronTest.java
│ │ │ SimpleTest.java
│ │ │
│ │ ├─job
│ │ │ MyJob.java
│ │
│ └─resources
│ applicationContext-cron.xml
│ applicationContext-simple.xml
maven 依赖
主要新增了 spring 的相关依赖:
2.3.0
1.6.4
0.9.28
1.6.4
4.0.5.RELEASE
org.quartz-scheduler
quartz
${quartz.version}
org.quartz-scheduler
quartz-jobs
${quartz.version}
org.slf4j
slf4j-api
${slf4j-api.version}
ch.qos.logback
logback-classic
${logback-classic.version}
commons-logging
commons-logging
runtime
org.slf4j
jcl-over-slf4j
${jcl-over-slf4j.version}
runtime
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-tx
${spring.version}
定义任务实现
- Job.java
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("========================================= my job");
}
}
简单的触发器定义
xml 配置
- applicationContext-simple.xml
测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class SimpleTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-simple.xml");
}
}
日志
主要日志如下:
18:06:54.767 [scheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
18:06:54.767 [scheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job hw_group.hw_job
========================================= my job
Cron 触发器实现
xml 配置
- applicationContext-cron.xml
测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CronTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-cron.xml");
}
}
日志
主要日志如下,每 S 都会执行一次。
18:09:36.001 [scheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
18:09:37.000 [scheduler_Worker-6] DEBUG org.quartz.core.JobRunShell - Calling execute on job hw_group.hw_job
========================================= my job
spring 整合的另一种方式
Job 的实现方式
上面我们展示了继承 Job 接口的实现方式,实际上也可以不继承接口。
这样我们的任务实现更加灵活。
public class AnotherJob {
public void run() {
System.out.println("========================================= my job");
}
}
xml 配置
这里我们需要在配置中指定对应的执行方法:
- applicationContext-simple-invoke.xml
测试代码
public class SimpleInvokeTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext-simple-invoke.xml");
}
}
日志
主要日志如下:
20:18:46.637 [schedulerFactoryBean_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
20:18:48.636 [schedulerFactoryBean_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
20:18:48.636 [schedulerFactoryBean_Worker-4] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.testJob
========================================= my job
20:18:48.637 [schedulerFactoryBean_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
20:18:50.636 [schedulerFactoryBean_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
20:18:50.636 [schedulerFactoryBean_Worker-5] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.testJob
========================================= my job
小结
这里基于 RAM 的执行方式就和 spring 整合完成了,但是我们希望更加灵活的话,可以参考前面的与 jdbc 的整合。
下一节将实现 spring+jdbc+quartz 的实现例子。
导航目录
参考资料
贡献者
binbin.hou