quartz
-
引入quartz
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency>
-
quartz.properties
- 默认加载路径为类的根路径,即开发时,放在mvn项目的resources根目录下
-
scheduler
任务调度器DirectSchedulerFactory
或者StdSchedulerFactory
-
trigger
触发器,用于定义任务调度时间规则SimpleTrigger
,CronTrigger
,etc
-
job
任务,即被调度的任务- 实现
org.quartz.Job
接口 @PersistJobDataAfterExecution
,@DisallowConcurrentExecution
- 实现
Quartz 任务调度的核心元素是 scheduler, trigger 和 job,其中 trigger 和 job 是任务调度的元数据, scheduler 是实际执行调度的控制器。
-
一个job可以被多个Trigger 绑定,但是一个Trigger只能绑定一个job!
-
每次执行时的JobDataMap是clone获得的,所以基础数据类型,String值不会变,对象是引用,对象里的属性值会变
-
不能重复添加同一个任务
spring quartz
-
引入
<!-- 不能少 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.9.RELEASE</version> </dependency> <!-- spring-context-support是核心 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.9.RELEASE</version> </dependency> <!-- quartz自然也不能漏 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency>
-
配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <!--<context:annotation-config/>--> <!--<context:component-scan base-package="quartz"/>--> <!-- 任意bean,不用特意实现quartz的job接口 --> <bean id="helloSpring" class="quartz.HelloSpring"/> <!-- 创建一个JobDetail --> <bean id="helloSpringJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="helloSpring"/> <property name="targetMethod" value="execute"/> <property name="concurrent" value="false"/> </bean> <!-- 给jobDetail创建一个触发器 --> <bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="helloSpringJob"/> <property name="startDelay" value="3000"/> <property name="repeatInterval" value="2000"/> </bean> <!-- 触发器添加到调度器里 --> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="helloTrigger"/> </list> </property> </bean> </beans>
spring-task
-
引入
在spring的配置文件中添加task命名空间即可
xmlns:task="http://www.springframework.org/schema/task"
&http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
-
配置
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <bean id="helloSpring" class="quartz.HelloSpring"/> <!-- 调度器 --> <task:scheduler id="taskScheduler" pool-size="10"/> <!-- job列表 --> <task:scheduled-tasks scheduler="taskScheduler"> <task:scheduled ref="helloSpring" method="execute" fixed-delay="1000"/> </task:scheduled-tasks> </beans>
#更多
-
https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html
-