ONE、除了引入 Spring 相關的 jar 包,還要引入 Quartz 的 jar 包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.2</version> </dependency>
這里用的是 MAVEN,普通工程自行下載 jar 包即可。
TWO、調度任務類 QuartzJob.java
package test; public class QuartzJob { public void work1() { System.out.println("Quartz定時器!!!work1 doing…"); } public void work2() { System.out.println("Quartz定時器!!!work2 doing…"); } }
里面定義兩個方法,quartz配置文件里也會用兩種方式配置 類 和 方法。
THREE、Spring 配置 spring-quartz.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName" default-lazy-init="true"> <!-- 要調用的工作類 --> <bean id="testJob" class="test.QuartzJob"></bean> <!-- 配置任務並發執行線程池 --> <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心線程數 線程池維護線程的最少數量 --> <property name="corePoolSize" value="100" /> <!-- 線程池維護線程所允許的空閑時間 --> <property name="keepAliveSeconds" value="200" /> <!-- 線程池維護線程的最大數量 --> <property name="maxPoolSize" value="100" /> <!-- 線程池所使用的緩沖隊列 --> <property name="queueCapacity" value="2000" /> <!-- 線程池對拒絕任務(無線程可用)的處理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,調用者的線程會執行該任務,如果執行器已關閉,則丟棄. --> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> <!-- 定義調用對象和調用對象的方法 --> <bean id="testTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 調用的類 --> <property name="targetObject"> <ref bean="testJob" /> </property> <!-- 調用類中的方法 --> <property name="targetMethod"> <value>work1</value> </property> </bean> <bean id="testTask2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 調用的類 --> <property name="targetObject" ref="testJob" /> <!-- 調用類中的方法 --> <property name="targetMethod" value="work2" /> </bean> <!-- 調度觸發器 --> <bean id="jibDoTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="testTask" /> </property> <!-- cron表達式 1秒執行一次 --> <property name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> <bean id="jibDoTime2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="testTask2" /> <property name="cronExpression" value="0/2 * * * * ?" /> </bean> <!-- 調度工廠 如果將lazy-init='false'那么容器啟動就會執行調度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="jibDoTime" /> <ref bean="jibDoTime2" /> </list> </property> <!-- 設置 QuartzScheduler 延時啟動,應用啟動完后 QuartzScheduler 再啟動 --> <property name="startupDelay" value="5" /> <property name="taskExecutor" ref="executor" /> </bean> </beans>
FOUR、web.xml 里加載 quartz
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-quartz.xml </param-value> </context-param>
下面雖然不太重要,但很能說服人~
Quartz定時器!!!work1 doing…
2017-09-14 15:36:32,009 DEBUG [org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers
2017-09-14 15:36:32,009 DEBUG [org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.testTask
Quartz定時器!!!work1 doing…
2017-09-14 15:36:32,012 DEBUG [org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers
2017-09-14 15:36:32,012 DEBUG [org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.testTask2
Quartz定時器!!!work2 doing…
2017-09-14 15:36:33,003 DEBUG [org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers
2017-09-14 15:36:33,003 DEBUG [org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.testTask
Quartz定時器!!!work1 doing…
2017-09-14 15:36:34,007 DEBUG [org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers
2017-09-14 15:36:34,007 DEBUG [org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.testTask
Quartz定時器!!!work1 doing…
2017-09-14 15:36:34,009 DEBUG [org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers
2017-09-14 15:36:34,009 DEBUG [org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.testTask2
Quartz定時器!!!work2 doing…
2017-09-14 15:36:35,004 DEBUG [org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.testTask
......
本文到此結束啦~