




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Quartz 线程处理Quartz定时任务默认都是并发执行的,不会等待上一次任务执行完毕,只要间隔时间到就会执行, 如果定时任执行太长,会长时间占用资源,导致其它任务堵塞。1.在Spring中这时需要设置concurrent的值为false, 禁止并发执行。12.当不使用spring的时候就需要在Job的实现类上加DisallowConcurrentExecution的注释DisallowConcurrentExecution 禁止并发执行多个相同定义的JobDetail, 这个注解是加在Job类上的, 但意思并不是不能同时执行多个Job, 而是不能并发执行同一个Job Definition(由JobDetail定义), 但是可以同时执行多个不同的JobDetail, 举例说明,我们有一个Job类,叫做SayHelloJob, 并在这个Job上加了这个注解, 然后在这个Job上定义了很多个JobDetail, 如sayHelloToJoeJobDetail, sayHelloToMikeJobDetail, 那么当scheduler启动时, 不会并发执行多个sayHelloToJoeJobDetail或者sayHelloToMikeJobDetail, 但可以同时执行sayHelloToJoeJobDetail跟sayHelloToMikeJobDetailPersistJobDataAfterExecution 同样, 也是加在Job上,表示当正常执行完Job后, JobDataMap中的数据应该被改动, 以被下一次调用时用。当使用PersistJobDataAfterExecution 注解时, 为了避免并发时, 存储数据造成混乱, 强烈建议把DisallowConcurrentExecution注解也加上。DisallowConcurrentExecution此标记用在实现Job的类上面,意思是不允许并发执行,按照我之前的理解是 不允许调度框架在同一时刻调用Job类,后来经过测试发现并不是这样,而是Job(任务)的执行时间比如需要10秒大于任务的时间间隔 Interval(5秒),那么默认情况下,调度框架为了能让 任务按照我们预定的时间间隔执行,会马上启用新的线程执行任务。否则的话会等待任务执行完毕以后 再重新执行!(这样会导致任务的执行不是按照我们预先定义的时间间隔执行)测试代码,这是官方提供的例子。设定的时间间隔为3秒,但job执行时间是5秒,设置DisallowConcurrentExecution以后程序会等任务执行完毕以后再去执行,否则会在3秒时再启用新的线程执行1org.quartz.threadPool.threadCount =5 这里配置框架的线程池中线程的数量,要多配置几个,否则DisallowConcurrentExecution不起作用?123org.quartz.scheduler.instanceName = MySchedulerorg.quartz.threadPool.threadCount =5org.quartz.jobStore.class =org.quartz.simpl.RAMJobStore?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596/* Copyright 2005 - 2009 Terracotta, Inc.* Licensed under the Apache License, Version 2.0 (the License); you may not* use this file except in compliance with the License. You may obtain a copy* of the License at* /licenses/LICENSE-2.0* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS, WITHOUT* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the* License for the specific language governing permissions and limitations* under the License.*/package org.quartz.examples.example5;import java.util.Date;import org.quartz.DisallowConcurrentExecution;import org.quartz.Job;import org.quartz.JobDataMap;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.PersistJobDataAfterExecution;/* A dumb implementation of Job, for unit testing purposes. * author James House*/PersistJobDataAfterExecutionDisallowConcurrentExecutionpublic class StatefulDumbJob implements Job /* * Constants.* */public static final String NUM_EXECUTIONS = NumExecutions;public static final String EXECUTION_DELAY = ExecutionDelay;/* * Constructors.* */public StatefulDumbJob() /* * Interface.* */* Called by the link org.quartz.Scheduler when a link org.quartz.Trigger fires that is associated with the Job. * throws JobExecutionException if there is an exception while executing the job.*/public void execute(JobExecutionContext context)throws JobExecutionException System.err.println(- + context.getJobDetail().getKey() + executing. +new Date() +);JobDataMap map = context.getJobDetail().getJobDataMap();int executeCount =0;if (map.containsKey(NUM_EXECUTIONS) executeCount = map.getInt(NUM_EXECUTIONS);executeCount+;map.put(NUM_EXECUTIONS, executeCount);long delay = 5000l;if (map.containsKey(EXECUTION_DELAY) delay = map.getLong(EXECUTION_DELAY);try Thread.sleep(delay);catch (Exception ignore) System.err.println( - + context.getJobDetail().getKey() + complete ( + executeCount +).);123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100/* Copyright 2005 - 2009 Terracotta, Inc.* Licensed under the Apache License, Version 2.0 (the License); you may not* use this file except in compliance with the License. You may obtain a copy* of the License at* /licenses/LICENSE-2.0* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS, WITHOUT* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the* License for the specific language governing permissions and limitations* under the License.*/package org.quartz.examples.example5;import static org.quartz.JobBuilder.newJob;import static org.quartz.SimpleScheduleBuilder.simpleSchedule;import static org.quartz.TriggerBuilder.newTrigger;import static org.quartz.DateBuilder.*;import java.util.Date;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerFactory;import org.quartz.SchedulerMetaData;import org.quartz.SimpleTrigger;import org.quartz.impl.StdSchedulerFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/* Demonstrates the behavior of StatefulJobs, as well as how misfire instructions affect the firings of triggers of StatefulJob s - when the jobs take longer to execute that the frequency of the triggers repitition.* While the example is running, you should note that there are two triggers with identical schedules, firing identical jobs. The triggers want to fire every 3 seconds, but the jobs take 10 seconds to execute. Therefore, by the time the jobs complete their execution, the triggers have already misfired (unless the schedulers misfire threshold has been set to more than 7 seconds). You should see that one of the jobs has its misfire instruction set to SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT, which causes it to fire immediately, when the misfire is detected. The other trigger uses the default smart policy misfire instruction, which causes the trigger to advance to its next fire time (skipping those that it has missed) - so that it does not refire immediately, but rather at the next scheduled time. * author Chris Bonham*/public class MisfireExample public void run()throws Exception Logger log = LoggerFactory.getLogger(MisfireExample.class);(- Initializing -);/ First we must get a reference to a schedulerSchedulerFactory sf =new StdSchedulerFactory();Scheduler sched = sf.getScheduler();(- Initialization Complete -);(- Scheduling Jobs -);/ jobs can be scheduled before start() has been called/ get a nice round time a few seconds in the future.Date startTime = nextGivenSecondDate(null,15);/ statefulJob1 will run every three seconds/ (but it will delay for ten seconds)JobDetail job = newJob(StatefulDumbJob.class).withIdentity(statefulJob1,group1).usingJobData(StatefulDumbJob.EXECUTION_DELAY, 10000L).build();SimpleTrigger trigger = newTrigger().withIdentity(trigger1,group1).startAt(startTime).withSchedule(simpleSchedule().withIntervalInSeconds(3).repeatForever().build();Date ft = sched.scheduleJob(job, trigger);(job.getKey() + will run at: + ft + and repeat: + trigger.getRepeatCount() + times, every + trigger.getRepeatInterval() /1000 + seconds);(- Starting Scheduler -);/ jobs dont start firing until start() has been called.sched.start();(- Started Scheduler -);try / sleep for ten minutes for triggers to file.Thread.sleep(600L * 1000L);catch (Exception e) (- Shutting Down -);sched.shutdown(true);(- Shutdown Complete -);SchedulerMetaData metaData = sched.getMetaData();(Executed + metaData.getNumberOfJobsExecuted() + jobs.);public static void main(String args)throws Exception MisfireExample example =new MisfireExample();example.run();PersistJobDataAfterExecution此标记说明在执行完Job的execution方法后保存JobDataMap当中固定数据,在默认情况下 也就是没有设置PersistJobDataAfterExecution的时候 每个job都拥有独立JobDataMap否则改任务在重复执行的时候具有相同的JobDataMap123456789101112131415161718192021222324252627282930313233343536373839404142434445464748/* Copyright 2005 - 2009 Terracotta, Inc.* Licensed under the Apache License, Version 2.0 (the License); you may not* use this file except in compliance with the License. You may obtain a copy* of the License at* /licenses/LICENSE-2.0* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS, WITHOUT* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the* License for the specific language governing permissions and limitations* under the License.*/package com.quartz.demo.example6;import java.util.Date;import org.quartz.DisallowConcurrentExecution;import org.quartz.Job;import org.quartz.JobDataMap;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.JobKey;import org.quartz.PersistJobDataAfterExecution;PersistJobDataAfterExecutionDisallowConcurrentExecutionpublic class BadJob1implements Job public BadJob1() public void execute(JobExecutionContext context)throws JobExecutionException JobKey jobKey = context.getJobDetail().getKey();JobDataMap dataMap = context.getJobDetail().getJobDataMap();int denominator = dataMap.getInt(denominator);System.out.println(- + jobKey + executing at +new Date() + with denominator + denominator);denominator+;dataMap.put(denominator, denominator);1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980/* Copyright 2005 - 2009 Terracotta, Inc.* Licensed under the Apache License, Version 2.0 (the License); you may not* use this file except in compliance with the License. You may obtain a copy* of the License at* /licenses/LICENSE-2.0* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS, WITHOUT* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the* License for the specific language governing permissions and limitations* under the License.*/package com.quartz.demo.example6;import static org.quartz.JobBuilder.newJob;import static org.quartz.SimpleScheduleBuilder.simpleSchedule;import static org.quartz.TriggerBuilder.newTrigger;import static org.quartz.DateBuilder.*;import java.util.Date;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerFactory;import org.quartz.SimpleTrigger;import org.quartz.impl.StdSchedulerFactory;public class JobExceptionExample public void run()throws Exception / First we must get a reference to a schedulerSchedulerFactory sf =new StdSchedulerFactory();Scheduler sched = sf.g
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025湖南省人民医院(湖南师范大学附属第一医院)高层次人才公开招聘78人考前自测高频考点模拟试题及答案详解(典优)
- Hydroxynorketamine-d6-hydrochloride-生命科学试剂-MCE
- Hexanoyl-coenzyme-A-R-Hexanoyl-CoA-生命科学试剂-MCE
- HDAC6-ligand-Linker-Conjugate-1-生命科学试剂-MCE
- 2025安徽阜阳市颍州区教育局面向本区教育系统选调专职教研员6人模拟试卷及一套参考答案详解
- GM1-Sphingosine-d18-1-生命科学试剂-MCE
- 2025广东深圳市宝安区鹏晖中英文学校急聘生物教师1人考前自测高频考点模拟试题及答案详解(考点梳理)
- 2025年动力转向泵项目发展计划
- 安全培训效果确认表课件
- 2025年数字化X射线机合作协议书
- 医院财务管理年度工作报告
- 灌溉水量平衡分析报告
- 高标准基本农田建设项目初步验收报告
- (2025版)国内旅游“一日游”合同(示范文本)
- 连云港市辅警考试题库2025
- 乡村执业助理试题及答案
- 2025年成人高考专升本医学综合真题及答案
- 2025-2026学年一年级上册统编版道德与法治教学计划
- 国开2025年秋季《形势与政策》专题测验1-5答案
- 急性STEMI PCI术冠状动脉内溶栓共识解读
- 陪诊师备考指南试题及答案
评论
0/150
提交评论