组件介绍:
- JOB 全部任务的统称
- JobDetail 任务的具体实现
- Scheduler 调度器.管理全部的任务.在内部实时监控任务的执行时间
- Trigger触发器. 当有任务需要执行时,它会创建一个线程调用jobDetail完成任务.
调用的顺序:
- 将JOB的任务和具体的内容注入到调度器中.
- 调度器内部会监控任务.如果任务的执行时间到了.则交给Trigger处理.
- Trigger启动线程执行jobDetail.
依赖jar包
<!-- 定时任务 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <!--定时任务需要依赖c3p0jar包 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
spring配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 定义任务bean --> <bean name="paymentOrderJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- 指定具体的job类 --> <property name="jobClass" value="com.jt.order.job.PaymentOrderJob" /> <!-- 指定job的名称 --> <property name="name" value="paymentOrder" /> <!-- 指定job的分组 --> <property name="group" value="Order" /> <!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务 --> <property name="durability" value="true"/> <!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 --> <property name="applicationContextJobDataKey" value="applicationContext"/> </bean> <!-- 定义触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="paymentOrderJobDetail" /> <!-- 每一分钟执行一次 --> <property name="cronExpression" value="0 0/1 * * * ?" /> </bean> <!-- 定义调度器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
XML万用转义字符
<![CDATA[]]
java类
注意:quartz每次创建线程就会创建一次此类,并且执行重写的方法。
public class PaymentOrderJob extends QuartzJobBean{ /** * 1、从上下文中获取spring容器 * 2、从spring容器中获取 orderMapper对象 * 3、执行mapper操作将超时订单状态设置为6:已取消(未付款订单,超时取消) */ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { //获取spring容器 ApplicationContext ac = (ApplicationContext) context.getJobDetail().getJobDataMap().get("applicationContext"); //获取mapper对象 OrderMapper orderMapper = ac.getBean(OrderMapper.class); //设置超时订单状态 Date agoDate = new DateTime().minusDays(2).toDate(); System.out.println(agoDate); orderMapper.updateStatus(agoDate); }