sping的quartz设置定时任务

时间:2022-03-24 08:00:00

除了spring相关的jar包之外,还需要引入quartz-all-1.8.6.jar

下载地址:http://www.quartz-scheduler.org/downloads/

spring配置文件增加quartz-bean.xml和quartz-set.xml

quartz-bean.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: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"
>

<!-- 测试 -->
<bean id="testQuartzTask" class="com.tech.jin.quartz.TestQuartzTask"/>

</beans>

quartz-set.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: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"
>

<!-- 定时任务定义 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- <property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:spring-quartz.properties" /> 用于数据库中配置任务,集群使用
-->
<property name="overwriteExistingJobs" value="true" /><!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->
<property name="startupDelay" value="1" /><!-- 启动完1秒后执行任务 -->
<property name="autoStartup" value="true" /><!-- 自动启动 -->
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">50</prop>
</props>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />

<property name="triggers">
<list>
<!-- 配置多个任务,继续在list中添加 -->
<ref bean="testQuartzTaskTrigger" /><!-- 测试自动任务 -->

</list>
</property>
</bean>

<!-- 测试自动任务 -->
<bean id="testQuartzTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="testQuartzTaskJobDetail"/>
</property>
<property name="cronExpression">
<value>0/10 * * * * ? </value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="jobClass" value="testQuartzTask"/>
<entry key="jobName" value="测试自动任务"/>
</map>
</property>
</bean>

<!-- MethodInvokingJobDetailFactoryBean不支持序列化 -->
<bean id="testQuartzTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="testQuartzTask"/></property>
<property name="targetMethod"><value>doTask</value></property>
<property name="concurrent" value="false"/><!-- concurrent(并发) : false表示等上一个任务执行完后再开启新的任务 -->
</bean>

<!-- 这种支持job序列化,但是这个jobClass对应的类,必须实现job,有execute方法-->
<!-- <bean id="testQuartzTaskJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.tech.jin.quartz.TestQuartzTask"></property>
<property name="durability" value="true" />
<property name="requestsRecovery" value="true" />
</bean>
-->

</beans>

 

 

使用MethodInvokingJobDetailFactoryBean调用的类TestQuartzTask:

package com.tech.jin.quartz;

import org.apache.log4j.Logger;

public class TestQuartzTask {

private Logger logger = Logger.getLogger(TestQuartzTask.class);

public void doTask(){
logger.info(
"66666666666666666666666");
}
}

 

使用JobDetailFactoryBean调用的类TestQuartzTask:

package com.tech.jin.quartz;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class TestQuartzTask implements Job{

private Logger logger = Logger.getLogger(TestQuartzTask.class);
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
logger.info(
"5555555");

}


}

 

 

除此之外,还需要将quartz-bean.xml 和quartz-set.xml引入到ApplicationContext.xml文件中(推荐)

<import resource="spring-quartz.xml"/> 

或者在web.xml中添加上读取sping配置文件的配置:classpath:spring/quartz-*xml。

  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/ApplicationContext.xml;
classpath:spring/quartz-*xml
</param-value>
</context-param>

 

--------------------------------------------------------------------------------------------------------------------------------------------------------

 

如果需要集群使用,就打开quartz-set.xml中SchedulerFactoryBean注掉的datasource和配置文件。

每个server把将要及正在运行的job所有状态都即时同步到*数据库,然后再次触发调用时从数据库中分析是否已有别的server正在运行相同job

(同名同定时时间点的job属于相当job),如果相同job正在别的server运行,那么当前server忽略本次运行的job.

Quartz的 Task(11 张表)实例化采用数据库存储,基于数据库引擎及 High-Available 的策略(集群的一种策略)自动协调每个节点的 Quartz。

spring-quartz.properties:

#==============================================================  
#Configure Main Scheduler Properties
#
==============================================================
org.quartz.scheduler.instanceName
= defaultScheduler
org.quartz.scheduler.instanceId
= AUTO

#
==============================================================
#Configure JobStore
#
==============================================================
org.quartz.jobStore.
class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass
= org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix
= QRTZ_
org.quartz.jobStore.isClustered
= true
org.quartz.jobStore.clusterCheckinInterval
= 20000
org.quartz.jobStore.dataSource
= myDS
org.quartz.jobStore.maxMisfiresToHandleAtATime
= 1
org.quartz.jobStore.misfireThreshold
= 120000
org.quartz.jobStore.txIsolationLevelSerializable
= true

#
==============================================================
#Configure ThreadPool
#
==============================================================
org.quartz.threadPool.
class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount
= 10
org.quartz.threadPool.threadPriority
= 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread
= true

#
==============================================================
#Skip Check Update
#update:
true
#not update:
false
#
==============================================================
org.quartz.scheduler.skipUpdateCheck
= true

#
============================================================================
# Configure Plugins
#
============================================================================
org.quartz.plugin.triggHistory.
class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.shutdownhook.
class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown
= true

数据库的sql版本要和squartz完全一致,在下载解压后的quartz\quartz-1.8.6\docs\dbTables文件夹在都有

tables_mysql.sql、tables_oracle.sql等...