在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度。
有关调度的实现我就第一就想到了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.CronTriggerFactoryBean"> <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 2.2.1版本,使用H2内存数据库,执行了Quartz发行包的docs\dbTables\tables_h2.sql建表。
二、建立Maven project,完成后目录如下
三、配置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:jdbc="http://www.springframework.org/schema/jdbc" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="com.sundoctor" /> <!-- 使用H2内存数据库并创建quartz数据库表 --> <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:db/tables_h2.sql"/> </jdbc:embedded-database> </beans>
这里只是配置了数据库连接dataSource,为了简单方便,我使用H2内存数据库,执行tables_h2.sql创建quartz数据库表,还没有涉及到quartz有关配置,下面且听我慢慢道来。
四、实现动态定时任务
什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定)。
这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户修改配置文件,但总需要重新启动web服务啊,研究了下Quartz在Spring中的动态定时,发现
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetail" /> <property name="cronExpression" value="0 0/50 * ? * * *" /> </bean>
中cronExpression是关键,如果可以动态设置cronExpression的值,就可以顺利解决问题了。这样我们就不能直接使用org.springframework.scheduling.quartz.CronTriggerFactoryBean,需要自己实现一个动态调度服务类,在其中构建CronTrigger或SimpleTrigger,动态配置时间。
动态调度服务接口:
package com.sundoctor.quartz.service; import java.util.Date; import org.quartz.CronExpression; public interface SchedulerService { /** * 根据 Quartz Cron Expression 调试任务 * * @param cronExpression * Quartz Cron 表达式,如 "0/10 * * ? * * *"等 */ void schedule(String cronExpression); /** * 根据 Quartz Cron Expression 调试任务 * * @param name * Quartz CronTrigger名称 * @param cronExpression * Quartz Cron 表达式,如 "0/10 * * ? * * *"等 */ void schedule(String name, String cronExpression); /** * 根据 Quartz Cron Expression 调试任务 * * @param name * Quartz CronTrigger名称 * @param group * Quartz CronTrigger组 * @param cronExpression * Quartz Cron 表达式,如 "0/10 * * ? * * *"等 */ void schedule(String name, String group, String cronExpression); /** * 根据 Quartz Cron Expression 调试任务 * * @param cronExpression * Quartz CronExpression */ void schedule(CronExpression cronExpression); /** * 根据 Quartz Cron Expression 调试任务 * * @param name * Quartz CronTrigger名称 * @param cronExpression * Quartz CronExpression */ void schedule(String name, CronExpression cronExpression); /** * 根据 Quartz Cron Expression 调试任务 * * @param name * Quartz CronTrigger名称 * @param group * Quartz CronTrigger组 * @param cronExpression * Quartz CronExpression */ void schedule(String name, String group, CronExpression cronExpression); /** * 在startTime时执行调试一次 * * @param startTime * 调度开始时间 */ void schedule(Date startTime); void schedule(Date startTime, String group); /** * 在startTime时执行调试一次 * * @param name * Quartz SimpleTrigger 名称 * @param startTime * 调度开始时间 */ void schedule(String name, Date startTime); void schedule(String name, Date startTime, String group); /** * 在startTime时执行调试,endTime结束执行调度 * * @param startTime * 调度开始时间 * @param endTime * 调度结束时间 */ void schedule(Date startTime, Date endTime); void schedule(Date startTime, Date endTime, String group); /** * 在startTime时执行调试,endTime结束执行调度 * * @param name * Quartz SimpleTrigger 名称 * @param startTime * 调度开始时间 * @param endTime * 调度结束时间 */ void schedule(String name, Date startTime, Date endTime); void schedule(String name, Date startTime, Date endTime, String group); /** * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次 * * @param startTime * 调度开始时间 * @param repeatCount * 重复执行次数 */ void schedule(Date startTime, int repeatCount); /** * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次 * * @param startTime * 调度开始时间 * @param endTime * 调度结束时间 * @param repeatCount * 重复执行次数 */ void schedule(Date startTime, Date endTime, int repeatCount); void schedule(Date startTime, Date endTime, int repeatCount, String group); /** * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次 * * @param name * Quartz SimpleTrigger 名称 * @param startTime * 调度开始时间 * @param endTime * 调度结束时间 * @param repeatCount * 重复执行次数 */ void schedule(String name, Date startTime, Date endTime, int repeatCount); void schedule(String name, Date startTime, Date endTime, int repeatCount, String group); /** * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次 * * @param startTime * 调度开始时间 * * @param repeatCount * 重复执行次数 * @param repeatInterval * 执行时间隔间 */ void schedule(Date startTime, int repeatCount, long repeatInterval); /** * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次 * * @param startTime * 调度开始时间 * @param endTime * 调度结束时间 * @param repeatCount * 重复执行次数 * @param repeatInterval * 执行时间隔间 */ void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval); void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group); /** * 在startTime时执行调试,endTime结束执行调度,重复执行repeatCount次,每隔repeatInterval秒执行一次 * * @param name * Quartz SimpleTrigger 名称 * @param startTime * 调度开始时间 * @param endTime * 调度结束时间 * @param repeatCount * 重复执行次数 * @param repeatInterval * 执行时间隔间 */ void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval); void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group); /** * 暂停触发器 * * @param triggerName * 触发器名称 */ void pauseTrigger(String triggerName); /** * 暂停触发器 * * @param triggerName * 触发器名称 * @param group * 触发器组 */ void pauseTrigger(String triggerName, String group); /** * 恢复触发器 * * @param triggerName * 触发器名称 */ void resumeTrigger(String triggerName); /** * 恢复触发器 * * @param triggerName * 触发器名称 * @param group * 触发器组 */ void resumeTrigger(String triggerName, String group); /** * 删除触发器 * * @param triggerName * 触发器名称 * @return */ boolean removeTrigdger(String triggerName); /** * 删除触发器 * * @param triggerName * 触发器名称 * @param group * 触发器组 * @return */ boolean removeTrigdger(String triggerName, String group); }
动态调度服务实现类:
package com.sundoctor.quartz.service; import java.text.ParseException; import java.util.Date; import java.util.UUID; import org.quartz.CronExpression; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerKey; import org.quartz.impl.triggers.CronTriggerImpl; import org.quartz.impl.triggers.SimpleTriggerImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("schedulerService") public class SchedulerServiceImpl implements SchedulerService { private static final String NULLSTRING = null; private static final Date NULLDATE = null; @Autowired private Scheduler scheduler; @Autowired private JobDetail jobDetail; @Override public void schedule(String cronExpression) { schedule(NULLSTRING, cronExpression); } @Override public void schedule(String name, String cronExpression) { schedule(name, NULLSTRING, cronExpression); } @Override public void schedule(String name, String group, String cronExpression) { try { schedule(name, group, new CronExpression(cronExpression)); } catch (ParseException e) { throw new IllegalArgumentException(e); } } @Override public void schedule(CronExpression cronExpression) { schedule(NULLSTRING, cronExpression); } @Override public void schedule(String name, CronExpression cronExpression) { schedule(name, NULLSTRING, cronExpression); } @Override public void schedule(String name, String group, CronExpression cronExpression) { if (isValidExpression(cronExpression)) { if (name == null || name.trim().equals("")) { name = UUID.randomUUID().toString(); } CronTriggerImpl trigger = new CronTriggerImpl(); trigger.setCronExpression(cronExpression); TriggerKey triggerKey = new TriggerKey(name, group); trigger.setJobName(jobDetail.getKey().getName()); trigger.setKey(triggerKey); try { scheduler.addJob(jobDetail, true); if (scheduler.checkExists(triggerKey)) { scheduler.rescheduleJob(triggerKey, trigger); } else { scheduler.scheduleJob(trigger); } } catch (SchedulerException e) { throw new IllegalArgumentException(e); } } } @Override public void schedule(Date startTime) { schedule(startTime, NULLDATE); } @Override public void schedule(Date startTime, String group) { schedule(startTime, NULLDATE, group); } @Override public void schedule(String name, Date startTime) { schedule(name, startTime, NULLDATE); } @Override public void schedule(String name, Date startTime, String group) { schedule(name, startTime, NULLDATE, group); } @Override public void schedule(Date startTime, Date endTime) { schedule(startTime, endTime, 0); } @Override public void schedule(Date startTime, Date endTime, String group) { schedule(startTime, endTime, 0, group); } @Override public void schedule(String name, Date startTime, Date endTime) { schedule(name, startTime, endTime, 0); } @Override public void schedule(String name, Date startTime, Date endTime, String group) { schedule(name, startTime, endTime, 0, group); } @Override public void schedule(Date startTime, int repeatCount) { schedule(null, startTime, NULLDATE, 0); } @Override public void schedule(Date startTime, Date endTime, int repeatCount) { schedule(null, startTime, endTime, 0); } @Override public void schedule(Date startTime, Date endTime, int repeatCount, String group) { schedule(null, startTime, endTime, 0, group); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount) { schedule(name, startTime, endTime, 0, 0L); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount, String group) { schedule(name, startTime, endTime, 0, 0L, group); } @Override public void schedule(Date startTime, int repeatCount, long repeatInterval) { schedule(null, startTime, NULLDATE, repeatCount, repeatInterval); } @Override public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval) { schedule(null, startTime, endTime, repeatCount, repeatInterval); } @Override public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) { schedule(null, startTime, endTime, repeatCount, repeatInterval, group); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval) { schedule(name, startTime, endTime, repeatCount, repeatInterval, NULLSTRING); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) { if (this.isValidExpression(startTime)) { if (name == null || name.trim().equals("")) { name = UUID.randomUUID().toString(); } TriggerKey triggerKey = new TriggerKey(name, group); SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setKey(triggerKey); trigger.setJobName(jobDetail.getKey().getName()); trigger.setStartTime(startTime); trigger.setEndTime(endTime); trigger.setRepeatCount(repeatCount); trigger.setRepeatInterval(repeatInterval); try { scheduler.addJob(jobDetail, true); if (scheduler.checkExists(triggerKey)) { scheduler.rescheduleJob(triggerKey, trigger); } else { scheduler.scheduleJob(trigger); } } catch (SchedulerException e) { throw new IllegalArgumentException(e); } } } @Override public void pauseTrigger(String triggerName) { pauseTrigger(triggerName, NULLSTRING); } @Override public void pauseTrigger(String triggerName, String group) { try { scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器 } catch (SchedulerException e) { throw new RuntimeException(e); } } @Override public void resumeTrigger(String triggerName) { resumeTrigger(triggerName, NULLSTRING); } @Override public void resumeTrigger(String triggerName, String group) { try { scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器 } catch (SchedulerException e) { throw new RuntimeException(e); } } @Override public boolean removeTrigdger(String triggerName) { return removeTrigdger(triggerName, NULLSTRING); } @Override public boolean removeTrigdger(String triggerName, String group) { TriggerKey triggerKey = new TriggerKey(triggerName, group); try { scheduler.pauseTrigger(triggerKey);// 停止触发器 return scheduler.unscheduleJob(triggerKey);// 移除触发器 } catch (SchedulerException e) { throw new RuntimeException(e); } } private boolean isValidExpression(final CronExpression cronExpression) { CronTriggerImpl trigger = new CronTriggerImpl(); trigger.setCronExpression(cronExpression); Date date = trigger.computeFirstFireTime(null); return date != null && date.after(new Date()); } private boolean isValidExpression(final Date startTime) { SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setStartTime(startTime); Date date = trigger.computeFirstFireTime(null); return date != null && date.after(new Date()); } }
SchedulerService 只有一个多态方法schedule,SchedulerServiceImpl实现SchedulerService接口,注入org.quartz.Schedulert和org.quartz.JobDetail,schedule方法可以动态配置org.quartz.CronExpression或org.quartz.SimpleTrigger调度时间。
五、实现自己的org.quartz.JobDetail
package com.sundoctor.example.service; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; public class MyQuartzJobBean extends QuartzJobBean { private static final Logger logger = LoggerFactory.getLogger(MyQuartzJobBean.class); @Override protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException { Trigger trigger = jobexecutioncontext.getTrigger(); String triggerName = trigger.getKey().getName(); SimpleService simpleService = getApplicationContext(jobexecutioncontext).getBean("simpleService", SimpleService.class); simpleService.testMethod(triggerName); } private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) { try { return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContextKey"); } catch (SchedulerException e) { logger.error("jobexecutioncontext.getScheduler().getContext() error!", e); throw new RuntimeException(e); } } }
MyQuartzJobBean继承org.springframework.scheduling.quartz.QuartzJobBean,SimpleService如下:
package com.sundoctor.example.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @Service("simpleService") public class SimpleService { private static final Logger logger = LoggerFactory.getLogger(SimpleService.class); public void testMethod(String triggerName) { // 这里执行定时调度业务 logger.info(triggerName); } }
SimpleService主要执行定时调度业务,在这里我只是简单打印一下log日志。
配置applicationContext-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="configLocation" value="classpath:quartz.properties" /> </bean> <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass"> <value>com.sundoctor.example.service.MyQuartzJobBean</value> </property> <property name="durability" value="true" /> </bean> </beans>
dataSource:项目中用到的数据源,里面包含了quartz用到的数据库表;
applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下文以key/value的方式存放在了quartz的SchedulerContext,可以用applicationContextSchedulerContextKey所定义的key得到spring的ApplicationContext,然后就可使用ApplicationContext取得spring beans,使用方法参见MyQuartzJobBean;
configLocation:用于指明quartz的配置文件的位置,如果不用spring配置quartz的话,本身quartz是通过一个配置文件进行配置的,默认名称是quartz.properties,里面配置的参数在quartz的doc文档中都有介绍,可以调整quartz,我在项目中也用这个文件部分的配置了一些属性,代码如下:
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 org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true org.quartz.jobStore.misfireThreshold = 60000 #org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate #org.quartz.jobStore.useProperties = true org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.isClustered = false org.quartz.jobStore.maxMisfiresToHandleAtATime=1
这里面没有数据源相关的配置部分,采用spring注入datasource的方式已经进行了配置。
六、测试
运行如下测试类
package com.sundoctor.example.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.SchedulerException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sundoctor.quartz.service.SchedulerService; public class MainTest { /** * @param args * @throws SchedulerException */ public static void main(String[] args) throws SchedulerException { ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[] { "classpath:applicationContext.xml", "classpath:applicationContext-quartz.xml" }); SchedulerService schedulerService = springContext.getBean("schedulerService", SchedulerService.class); // 执行业务逻辑... // 设置高度任务 // 每10秒中执行调试一次 schedulerService.schedule("0/10 * * ? * * *"); Date startTime = parse("2014-08-19 16:33:00"); Date endTime = parse("2014-08-22 21:10:00"); // 2014-08-19 16:33:00开始执行调度 schedulerService.schedule(startTime); // 2014-08-19 16:33:00开始执行调度,2014-08-22 21:10:00结束执行调试 schedulerService.schedule(startTime, endTime); // 2014-08-19 16:33:00开始执行调度,执行5次结束 schedulerService.schedule(startTime, 5); // 2014-08-19 16:33:00开始执行调度,每隔20秒执行一次,执行5次结束 schedulerService.schedule(startTime, 5, 20); // 等等,查看com.sundoctor.quartz.service.SchedulerService } private static Date parse(String dateStr) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return format.parse(dateStr); } catch (ParseException e) { throw new RuntimeException(e); } } }
输出
[2014-08-19 22:31:50]INFO com.sundoctor.example.service.SimpleService(line:14) -7e132a81-6d24-44e1-8d6c-15cdaeefb2ce [2014-08-19 22:32:00]INFO com.sundoctor.example.service.SimpleService(line:14) -7e132a81-6d24-44e1-8d6c-15cdaeefb2ce [2014-08-19 22:32:10]INFO com.sundoctor.example.service.SimpleService(line:14) -7e132a81-6d24-44e1-8d6c-15cdaeefb2ce [2014-08-19 22:32:20]INFO com.sundoctor.example.service.SimpleService(line:14) -7e132a81-6d24-44e1-8d6c-15cdaeefb2ce [2014-08-19 22:32:30]INFO com.sundoctor.example.service.SimpleService(line:14) -7e132a81-6d24-44e1-8d6c-15cdaeefb2ce [2014-08-19 22:32:40]INFO com.sundoctor.example.service.SimpleService(line:14) -7e132a81-6d24-44e1-8d6c-15cdaeefb2ce
这样只是简单的将quartz trigger名称打印出来。
这样通过SchedulerService就可以动态配置调度时间。其实SchedulerService 还可扩展,比如可以注入多个JobDetail,调度不同的JobDetail。
相关推荐
Spring Quartz如何动态配置时间
本人花费多日时间,亲测有效。所需文件代码完整,只需导入常用开发IDE和mysql数据库即可正常使用。
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间、频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quartz与spring的组合...
Spring Quartz动态配置时间.rar
Quartz + spring简单配置多个任务调度 spring配置文件 简单配置
Quartz 是一个强大的企业级 Schedule 工具,也是目前最好的开源 Schedule 工具。实例中quartz在spring中的应用及quartz详细时间配置。
spring框架配置的时间调度插件,提供jar包 及 配置实例
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;...