实现场景一:多个频率H1(5分钟)任务、H1(10分钟)任务 定时执行,且每个任务执行所需时间在频率时间段内
配置文件:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " default-lazy-init="false"> <bean id="configholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> </bean> <context:annotation-config /> <tx:annotation-driven proxy-target-class="true" /> <bean id="h1JsonFileService" class="com.hp.bon.omc.nms.core.service.JsonFileServiceH1"> <property name="freq" value="H1" /> </bean> <bean id="H1Job" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="h1JsonFileService" /> <property name="targetMethod" value="doIt" /> <property name="concurrent" value="false" /> </bean> <!-- H1频率,每5分钟一次: 440200017 任务 0 0,5,10,15,20,25,30,35,40,45,50,55 * * * ? --> <bean id="H1Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="H1Job" /> <property name="cronExpression" value="0 0/5 * * * ?" /> </bean> <bean id="h2JsonFileService" class="com.hp.bon.omc.nms.core.service.JsonFileServiceH2"> <property name="freq" value="H2" /> </bean> <bean id="H2Job" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="h2JsonFileService" /> <property name="targetMethod" value="doIt" /> <property name="concurrent" value="false" /><!-- 关闭任务并行执行,采用 串行执行--> </bean> <!-- H2频率,每10分钟一次 --> <bean id="H2Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="H2Job" /> <property name="cronExpression" value="0 0/10 * * * ?" /> </bean> <bean name="H1SchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="H1Trigger" /> <ref bean="H2Trigger" /> </list> </property> </bean> </beans>
com.hp.bon.omc.nms.core.service.JsonFileServiceH1 为自定义类:(JsonFileServiceH2代码相同)
public class JsonFileServiceH1 { private static final Logger LOG = LoggerFactory.getLogger(JsonFileServiceH1.class); public void doIt(){ try{ doItReal(); }catch(Exception e){ LOG.error("doItReal() error", e); } } public void doItReal(){ LOG.info("****doing*****"); } }
输出结果:
5分钟时:
[H1SchedulerFactoryBean_Worker-1] ****doing***** 10分钟时: [H1SchedulerFactoryBean_Worker-1] ****doing***** [H2SchedulerFactoryBean_Worker-1] ****doing*****
实现场景二:多个频率H1(5分钟)任务、H1(10分钟)任务 定时执行,但某个频率(如H1)内任务执行所需时间超过频率时间段
配置文件:同上,不变
com.hp.bon.omc.nms.core.service.JsonFileServiceH1 为自定义类:(JsonFileServiceH2代码不变)
public class JsonFileServiceH1 { private static final Logger LOG = LoggerFactory.getLogger(JsonFileServiceH1.class); public void doIt(){ try{ doItReal(); }catch(Exception e){ LOG.error("doItReal() error", e); } } public void doItReal() throws InterruptedException{ for( int x = 1 ; x <= 10;x++){ Thread.sleep(60*1000);//Sleep 1分钟 LOG.info("****doing"+x+"*****"); } } }
输出结果:
5-9分钟时: [H1SchedulerFactoryBean_Worker-1] ****doing1***** [H1SchedulerFactoryBean_Worker-1] ****doing2***** [H1SchedulerFactoryBean_Worker-1] ****doing3***** [H1SchedulerFactoryBean_Worker-1] ****doing4***** 10分钟时: [H1SchedulerFactoryBean_Worker-2] ****doing1***** [H2SchedulerFactoryBean_Worker-1] ****doing***** 11-14分钟 [H1SchedulerFactoryBean_Worker-2] ****doing2***** [H1SchedulerFactoryBean_Worker-2] ****doing3***** [H1SchedulerFactoryBean_Worker-2] ****doing4***** ......
结论:1)每次 定时任务执行时,如果前次任务没有执行完成,当前任务会中断前任务执行过程。
2)一个 SchedulerFactoryBean类,默认只会启动 10个_Worker,即org.quartz.threadPool.threadCount = 10(默认值),一旦启动的任务过多,只能等到空闲_Worker只才会执行。
springframework.scheduling.quartz 为 org.springframework.context.support-3.1.2.RELEASE包,源代码研待续。。。。