1.加载包
2.添加quartz.propertes
3.编写自己的任务类
4.添加自动任务配置
5.通过 quartzProperties 配置连接池
1.加载包
<dependency> <groupId>quartz</groupId> <artifactId>quartz</artifactId> <version>1.5.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency>
2.添加quartz.propertes
从quartz.jar中copy一份quartz.properties 放在项目资源目录下
修改线程池数量
org.quartz.threadPool.threadCount = 1
# Default Properties file for use by StdSchedulerFactory # to create a Quartz Scheduler Instance, if a different # properties file is not explicitly specified. # org.quartz.scheduler.instanceName = DefaultQuartzScheduler org.quartz.scheduler.rmi.export = false org.quartz.scheduler.rmi.proxy = false org.quartz.scheduler.wrapJobExecutionInUserTransaction = false org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool #thread pool count org.quartz.threadPool.threadCount = 1 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true org.quartz.jobStore.misfireThreshold = 60000 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
3.编写自己的任务类
com.qi.task.RiskManagermentJob
4.添加自动任务配置
需要添加
<property name="configLocation" value="classpath:quartz.properties"></property>
不添加,配置的线程池数将不生效。
<?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" 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"> <bean id="riskManagermentJob" class="com.qi.task.RiskManagermentJob" /> <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="riskManagermentJob" /> </property> <property name="targetMethod"> <value>execute</value> </property> </bean> <!-- ======================== 调度触发器 ======================== --> <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="SpringQtzJobMethod"></property> <property name="cronExpression" value="2/60 * 15 * * ?"></property> </bean> <!-- ======================== 调度工厂 ======================== --> <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="CronTriggerBean" /> </list> </property> <property name="configLocation" value="classpath:quartz.properties"></property> </bean> </beans>
5.通过 quartzProperties 配置连接池
<bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="CronTriggerBean" /> </list> </property> <property name="quartzProperties"> <props> <prop key="org.quartz.threadPool.threadCount">1</prop> </props> </property> <!-- 方式二 --> <!-- <property name="configLocation" value="classpath:quartz.properties"></property> --> </bean>