先给个场景:每五分钟扫描并关闭7天未付款的订单
1.quartz pom.xml依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.6.RELEASE</version> </dependency> </dependencies>
2.spring.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 定义任务 --> <bean id="payment" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- 指定具体的job类 --> <property name="jobClass" value="com.kawa.order.job.payOrderJob"></property> <!-- 指定job名称 --> <property name="name" value="payOrder"></property> <!-- 指定job的分组 --> <property name="group" value="Order"></property> <!-- 必须设置为true,如果为false,当没活动的触发器与之关联时会在调度器中删除该任务--> <property name="durability" value="true"></property> <!-- 指定spring容器的key,如果不设定在jobmap中是获取不到spring容器的 --> <property name="applicationContextJobDataKey" value="applicationContext"></property> </bean> <!-- 定义触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="payment"></property> <!-- 每一分钟执行一次,清理无效订单 --> <property name="cronExpression" value="0 0/1 * * * ?"></property> </bean> <!-- 定义调度器 --> <bean id="" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger"/> </list> </property> </bean> </beans>
3.java 代码package com.kawa.order.job;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; import com.taotao.order.service.OrderService; public class payOrderJob extends QuartzJobBean{ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { //获取spring容器 ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap().get("applicationContext"); //获取OrderService实例,clearOrder为执行方法名 //new DateTime().minusDays(7).toDate()为当前时间前7天 //clearOrder方法的逻辑就不写了...
applicationContext.getBean(OrderService.class).clearOrder(new DateTime().minusDays(7).toDate()); } }