引
Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度。
Jar包依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
需要注意一点,与Spring3.1以下版本整合必须使用Quartz 1.x.x。
在Spring中使用Quartz方式:
- 任务类继承QuartzJobBean
- 在配置文件里定义任务类和要执行的方法,类和方法仍然是普通类
- 使用注解的方式
任务类Job
package com.chen.quatrz;
import org.springframework.stereotype.Service;
@Service(value="springD")
public class SpringD {
public void one() {
System.out.println("spring task D 定时任务");
}
}
package com.chen.quatrz;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service(value="springC")
public class SpringC {
/*
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
- 区间
* 通配符
? 你不想设置那个字段
*/
@Scheduled(cron="1/2 * * * * ?")
public void one() {
System.out.println("spring task C 定时任务");
}
}
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"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-lazy-init="false">
<context:annotation-config />
<context:component-scan base-package="com.chen.quartz" />
<!-- 定时任务注解扫描-->
<task:annotation-driven />
<bean id="springDJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="springD" />
<property name="targetMethod" value="one" />
<!-- 是否并发调度 -->
<!-- <property name="concurrent" value="true" /> -->
</bean>
<!-- 触发器 -->
<bean id="springDJobCT" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="springDJob" />
<property name="cronExpression" value="2/5 * * * * ?" />
</bean>
<!-- 线程池 -->
<bean id="executor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="5" />
<!-- 最大线程数 -->
<property name="maxPoolSize" value="10" />
<!-- 队列最大长度 >=mainExecutor.maxSize -->
<property name="queueCapacity" value="90" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="3000" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃. -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>
<!-- 调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name="triggers">
<list>
<ref bean="springDJobCT" />
</list>
</property>
<property name="taskExecutor" ref="executor" />
</bean>
<!-- Spring内部有一个task,是Spring自带的一个设定时间自动任务调度 -->
<!-- ref是工作类
method是工作类中要执行的方法
initial-delay是任务第一次被调用前的延时,单位毫秒
fixed-delay是上一个调用完成后再次调用的延时
fixed-rate是上一个调用开始后再次调用的延时(不用等待上一次调用完成)
cron是表达式,表示在什么时候进行任务调度
-->
<!-- <task:scheduled-tasks>
<task:scheduled ref="springD" method="one" cron="2/5 * * * * ?"/>
</task:scheduled-tasks> -->
</beans>
测试
package com.chen.junit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/quartz-t