Spring配置Quartz任务调度、及 ThreadPool 线程池
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
......
本文到此结束啦~