spring quartz定时器配置-JobStoreTX方式持久化在数据库中

配置quartz持久化在数据库中,同样需要以下三步配置,如下:

  • 首先需要引入quartz所依赖的jar包
  • 创建quartz_jobs.xml文件用于配置调度器
  • 引入quartz_jobs.xml

以上三个步骤,见上篇博客:https://blog.csdn.net/llmys/article/details/81069445

只是在为quartz定时器配置数据库时,在quartz_jobs.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-4.1.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
<!--此处为设置你的连接数据库所需要的驱动,不同的数据库需要不同的驱动程序;
如Oracle更改为:oracle.jdbc.driver.OracleDriver;
MySQL更改为:com.mysql.jdbc.Driver -->
	  	<property name="driverClass" value="org.postgresql.Driver" />
		<property name="jdbcUrl" value="jdbc:postgresql://192.168.2.155:5432/quartz_test" />
		<property name="user" value="你的数据库用户名" />
		<property name="password" value="你的数据库登录密码" />
	</bean>
	<bean id="quartzScheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
				<prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>

				<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
				<prop key="org.quartz.threadPool.threadCount">30</prop>
				<prop key="org.quartz.threadPool.threadPriority">5</prop>

				<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
<!--此处为设置连接数据库所需要的驱动,不同的数据库需要不同的驱动程序;
自己数据库对应的驱动程序可在此处连接查看:
http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ConfigJobStoreTX.html-->
				<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.PostgreSQLDelegate</prop>
				<prop key="org.quartz.jobStore.useProperties">false</prop>
 <!--对应数据库表的名称前缀,持久化数据到数据库之前我们需要创建quartz对应的数据库表
在数据库中,postgresql数据库对应的表见链接:
https://gist.github.com/ajbrown/7923127,其他数据库对应的表可自行查找。-->
				<prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
				<prop key="org.quartz.jobStore.isClustered">false</prop>
				<prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
				<prop key="org.quartz.scheduler.misfirePolicy">doNothing</prop>
				
				<prop key="org.quartz.plugin.shutdownhook.class">org.quartz.plugins.management.ShutdownHookPlugin</prop>
				<prop key="org.quartz.plugin.shutdownhook.cleanShutdown">true</prop>
				<prop key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">true</prop>
			</props>
		</property>
	</bean>
</beans> 

接下来就可以使用代码对其动态添加、停止、修改、删除定时任务了。

提示:如果启动时出现以下错误,可能是由于你使用的JDBC驱动的版本和你数据库的版本不一致,这种情况可以更改驱动程序和你数据库版本对应的JDBC驱动一致即可。

org.quartz.JobPersistenceException: Couldn't obtain triggers for job: An SQLException was provoked by the following failure: java.lang.ArrayIndexOutOfBoundsException: 2 [See nested exception: java.sql.SQLException: An SQLException was provoked by the following failure: java.lang.ArrayIndexOutOfBoundsException: 2]
	at org.quartz.impl.jdbcjobstore.JobStoreSupport.getTriggersForJob(JobStoreSupport.java:2144)
	at org.quartz.impl.jdbcjobstore.JobStoreSupport$28.execute(JobStoreSupport.java:2130)
	at org.quartz.impl.jdbcjobstore.JobStoreCMT.executeInLock(JobStoreCMT.java:245)
	at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeWithoutLock(JobStoreSupport.java:3723)
	at org.quartz.impl.jdbcjobstore.JobStoreSupport.getTriggersForJob(JobStoreSupport.java:2127)
	at org.quartz.core.QuartzScheduler.getTriggersOfJob(QuartzScheduler.java:1476)
	at org.quartz.impl.StdScheduler.getTriggersOfJob(StdScheduler.java:461)
	at com.leagsoft.unisims.base.uniquartz.service.UniQuartzService.lambda$0(UniQuartzService.java:570)
	at java.lang.Iterable.forEach(Unknown Source)
	at com.leagsoft.unisims.base.uniquartz.service.UniQuartzService.getAllJob(UniQuartzService.java:568)
	at com.leagsoft.unisims.base.uniquartz.controller.UniQuartzController.addQuartz(UniQuartzController.java:31)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
......
......

猜你喜欢

转载自blog.csdn.net/llmys/article/details/81069638
今日推荐