Spring整合Quartz实现定时任务调度

来源:互联网 发布:淘宝怎么设置支持花呗 编辑:程序博客网 时间:2024/04/30 10:51

一. 核心类

1. Job: 表示一个工作, 具体的业务处理都在这里.

2. JobDetail: 表示一个具体的可执行的调度程序.

3. Trigger: 用于调度参数的配置(什么时候去调用Job).

4. Scheduler: 表示一个调度容器, 容器中有一个线程池, 用来并行调度执行每个作业, 一个调度容器中可以注册多个JobDetail和Trigger. 


二. 整合spring

1. 代码结构图:



2. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd"default-autowire="byName"><!-- 调度器 --><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><!-- 触发器列表 --><ref bean="cronTrigger" /></list></property><!-- 加载配置文件, 如果不配置, 将会使用quartz默认的配置 --><property name="configLocation" value="classpath:quartz.properties" /></bean><!-- 触发器 --><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><!-- 指向我们的任务 --><property name="jobDetail" ref="reportTask" /><!-- 每天晚上23点1分到59分,每分钟运行一次 --><property name="cronExpression" value="0 1-59 23 * * ?" /></bean><!-- 业务处理类 --><bean name="reportTask" class="org.springframework.scheduling.quartz.JobDetailBean"><property name="jobClass" value="com.zdp.quartz.BusinessReport" /></bean></beans>

3. quartz.properties

#============================================================================# Configure Main Scheduler Properties  #============================================================================org.quartz.scheduler.instanceName = DefaultQuartzSchedulerorg.quartz.scheduler.instanceId = AUTOorg.quartz.scheduler.rmi.export = falseorg.quartz.scheduler.rmi.proxy = falseorg.quartz.scheduler.wrapJobExecutionInUserTransaction = false#============================================================================# Configure ThreadPool  #============================================================================org.quartz.threadPool.threadCount = 10org.quartz.threadPool.threadPriority = 5#============================================================================# Configure JobStore  #============================================================================org.quartz.jobStore.misfireThreshold = 60000

4. BusinessReport.java

/** * 业务处理类 */public class BusinessReport implements Job {// 执行报表统计入口函数public void execute(JobExecutionContext context) throws JobExecutionException {System.out.println("开始执行报表的业务逻辑了, 现在的时间是: " + new Date());}}


5. Main.Java

/** *  测试类 */public class Main {public static void main(String[] args) {new ClassPathXmlApplicationContext("applicationContext.xml");}}

三. cronExpression表达式

0 0 12 * * ? --------------- 在每天中午12:00触发 
0 15 10 ? * * --------------- 每天上午10:15触发 
0 15 10 * * ? --------------- 每天上午10:15触发 
0 15 10 * * ? * --------------- 每天上午10:15触发 
0 15 10 * * ? 2005 --------------- 在2005年中的每天上午10:15触发 
0 * 14 * * ? --------------- 每天在下午2:00至2:59之间每分钟触发一次 
0 0/5 14 * * ? --------------- 每天在下午2:00至2:59之间每5分钟触发一次 
0 0/5 14,18 * * ? --------------- 每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次 
0 0-5 14 * * ? --------------- 每天在下午2:00至2:05之间每分钟触发一次 
0 10,44 14 ? 3 WED --------------- 每三月份的星期三在下午2:00和2:44时触发 
0 15 10 ? * MON-FRI --------------- 从星期一至星期五的每天上午10:15触发 
0 15 10 15 * ? --------------- 在每个月的每15天的上午10:15触发 
0 15 10 L * ? --------------- 在每个月的最后一天的上午10:15触发 
0 15 10 ? * 6L --------------- 在每个月的最后一个星期五的上午10:15触发 
0 15 10 ? * 6L 2002-2005 --------------- 在2002, 2003, 2004, 2005年的每个月的最后一个星期五的上午10:15触发 
0 15 10 ? * 6#3 --------------- 在每个月的第三个星期五的上午10:15触发 
0 0 12 1/5 * ? --------------- 从每月的第一天起每过5天的中午12:00时触发 
0 11 11 11 11 ? --------------- 在每个11月11日的上午11:11时触发




2 0
原创粉丝点击