在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度。
有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间,非常easy。比如:每天凌晨几点定时运行一个程序,这只要在工程中的spring配置文件中配置好spring整合quartz的几个属性就好。
Spring配置文件
引用
<bean
id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property
name="targetObject" ref="simpleService" />
<property
name="targetMethod" value="test" />
</bean>
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property
name="jobDetail" ref="jobDetail" />
<property
name="cronExpression" value="0 0/50 * ? * * *"
/>
</bean>
<bean
id="schedulerTrigger"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property
name="triggers">
<list>
<ref
bean="cronTrigger"/>
</list>
</property>
</bean>
这种配置就是对quartz的一种简单的使用了,调度任务会在spring启动的时候加载到内存中,按照cronTrigger中定义的
cronExpression定义的时间按时触发调度任务。但是这是quartz使用“内存”方式的一种配置,也比较常见,当然对于不使用spring的项目,也可以单独整合quartz。方法也比较简单,可以从quartz的doc中找到配置方式,或者看一下《Quartz
Job Scheduling Framework
》。
但是对于想持久化调度任务的状态,并且灵活调整调度时间的方式来说,上面的内存方式就不能满足要求了,正如本文开始我遇到的情况,需要采用数据库方式集成
Quartz,这部分集成其实在《Quartz Job Scheduling Framework
》中也有较为详细的介绍,当然doc文档中也有,但是缺乏和spring集成的实例。
一、需要构建Quartz数据库表,建表脚本在Quartz发行包的docs\dbTables目录,里面有各种数据库建表脚本,我采用的Quartz
1.6.5版本,总共12张表,不同版本,表个数可能不同。我用mysql数据库,执行了Quartz发行包的docs\dbTables\tables_mysql_innodb.sql建表。
二、建立java
project,完成后目录如下
三、配置数据库连接池
配置jdbc.properties文件
引用
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
jdbc.username=root
jdbc.password=kfs
cpool.checkoutTimeout=5000
cpool.minPoolSize=10
cpool.maxPoolSize=25
cpool.maxIdleTime=7200
cpool.acquireIncrement=5
cpool.autoCommitOnClose=true
配置applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
>
<context:component-scan
base-package="com.sundoctor"/>
<!-- 属性文件读入
-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property
name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!--
数据源定义,使用c3p0 连接池 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass"
value="${jdbc.driverClassName}" />
<property name="jdbcUrl"
value="${jdbc.url}" />
<property name="user" value="${jdbc.username}"
/>
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}"
/>
<property name="maxIdleTime"
value="${cpool.maxIdleTime}"/>
</bean>
</beans>
这里只是配置了数据连接池,我使用c3p0 连接池,还没有涉及到Quartx有关配置,下面且听我慢慢道来。
相关推荐
Spring Quartz如何动态配置时间
本人花费多日时间,亲测有效。所需文件代码完整,只需导入常用开发IDE和mysql数据库即可正常使用。
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间、频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quartz与spring的组合...
Spring Quartz动态配置时间.rar
NULL 博文链接:https://sundoctor.iteye.com/blog/399980
Quartz + spring简单配置多个任务调度 spring配置文件 简单配置
spring框架配置的时间调度插件,提供jar包 及 配置实例
Quartz 是一个强大的企业级 Schedule 工具,也是目前最好的开源 Schedule 工具。实例中quartz在spring中的应用及quartz详细时间配置。
Spring自带了多线程设置:quartz,在quartz的设置中,时间字段CronExpression的设置比较纠结,一个Cron表达式是一个由六至七个字段组成由空格分隔的字符串,其中6个字段是必须的而一个是可选的,文档详细讲解了这个...
Spring+Quartz 从数据库中获取定时任务和定时时间,动态实现对定时任务的增删改查,部署到tomcat即可看到定时任务执行效果。本人亲测,可用!
虽然可以通过属性文件(在属性文件中可以指定 JDBC 事务的数据源、全局作业和/或触发器侦听器、插件、线程池,以及更多)配置 Quartz,但它根本没有与应用程序服务器的上下文或引用集成在一起。结果就是作业不能访问...
下载导入就可以用,执行test类的main方法加载spring applicationContext.xml配置文件,启动Quartz定时器.修改spring配置文件的配置时间.详细看注释
传统Spring+Quartz实现的定时器一般都是通过配置文件中配置的,这就意味着如果我们修改了定时任务的时间,就得重新启动一下Tomcat,而且每次新建一个任务就得在spring中添加相关的配置信息,非常麻烦。基于上面的...
Quartz学习文档 Spring + Quartz配置详细实例 jar包 Quartz时间格式设置
NULL 博文链接:https://daoshud1.iteye.com/blog/1955099
NULL 博文链接:https://hzywy.iteye.com/blog/1647076
spring不能正常加载,由于第一次使用Quartz,我就是当时忽略了这一点,浪费了很多时间来调配置,其实配置一点没有错误。 3. 你会问为什么用spring后就不用 quartz.properties quartz-jobsxml这两个文件了么? ...
Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz框架包含了调度器监听、作业和触发器监听。你可以配置作业和触发器监听为全局监听或者是特定于作业和触发器的监听...
quartz1.6.5 整合 Hibernate、Spring,本例支持同时运行多个不同的任务,每个任务动态调用的方法和时间都不一样; 附件中有支持各种数据库的脚本;本例支持Oracle10g数据库脚本;开发测试使用的IDE工具MyEclipse6.5;...
quartz1.6.5 整合 Hibernate、Spring,本例支持同时运行多个不同的任务,每个任务动态调用的方法和时间都不一样; 附件中有支持各种数据库的脚本;本例支持Oracle10g数据库脚本;开发测试使用的IDE工具MyEclipse6.5;...