/*
* 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
*
* http://www.apache.org/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.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.Logger; /**
* Demonstrates the behavior of <code>StatefulJob</code>s, as well as how
* misfire instructions affect the firings of triggers of <code>StatefulJob</code>
* s - when the jobs take longer to execute that the frequency of the trigger's
* repitition.
*
* <p>
* 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 scheduler's "misfire threshold" has been set to more than 7
* seconds). You should see that one of the jobs has its misfire instruction
* set to <code>SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT</code>,
* 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.
* </p>
*
* @author <a href="mailto:bonhamcm@thirdeyeconsulting.com">Chris Bonham</a>
*/
public class MisfireExample { public void run() throws Exception {
Logger log = LoggerFactory.getLogger(MisfireExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete -----------"); log.info("------- Scheduling Jobs -----------"); // jobs can be scheduled before start() has been called // get a "nice round" time a few seconds in the future...
long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime(); // statefulJob1 will run every three seconds
// (but it will delay for ten seconds)
JobDetail job = new JobDetail("statefulJob1", "group1",
StatefulDumbJob.class);
job.getJobDataMap().put(MisfireJob.EXECUTION_DELAY, 10000L);
SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1",
new Date(ts), null,
SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
Date ft = sched.scheduleJob(job, trigger);
log.info(job.getFullName() +
" will run at: " + ft +
" and repeat: " + trigger.getRepeatCount() +
" times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // statefulJob2 will run every three seconds
// (but it will delay for ten seconds)
job = new JobDetail("statefulJob2", "group1", StatefulDumbJob.class);
job.getJobDataMap().put(MisfireJob.EXECUTION_DELAY, 10000L);
trigger = new SimpleTrigger("trigger2", "group1",
new Date(ts), null,
SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
trigger
.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT);
ft = sched.scheduleJob(job, trigger);
log.info(job.getFullName() +
" will run at: " + ft +
" and repeat: " + trigger.getRepeatCount() +
" times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // jobs don't start firing until start() has been called...
sched.start(); log.info("------- Started Scheduler -----------------"); try {
// sleep for ten minutes for triggers to file....
Thread.sleep(600L * 1000L);
} catch (Exception e) {
} log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData();
log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
} public static void main(String[] args) throws Exception { MisfireExample example = new MisfireExample();
example.run();
} }

  

/*
* 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
*
* http://www.apache.org/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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.StatefulJob;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; /**
* <p>
* A dumb implementation of Job, for unittesting purposes.
* </p>
*
* @author James House
*/
public class MisfireJob implements StatefulJob { // Logging
private static Logger _log = LoggerFactory.getLogger(MisfireJob.class); // Constants
public static final String NUM_EXECUTIONS = "NumExecutions";
public static final String EXECUTION_DELAY = "ExecutionDelay"; /**
* Empty public constructor for job initilization
*/
public MisfireJob() {
} /**
* <p>
* Called by the <code>{@link org.quartz.Scheduler}</code> when a <code>{@link org.quartz.Trigger}</code>
* fires that is associated with the <code>Job</code>.
* </p>
*
* @throws JobExecutionException
* if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
String jobName = context.getJobDetail().getFullName();
_log.info("---" + jobName + " executing at " + new Date()); // default delay to five seconds
long delay = 5000L; // use the delay passed in as a job parameter (if it exists)
JobDataMap map = context.getJobDetail().getJobDataMap();
if (map.containsKey(EXECUTION_DELAY)) {
delay = map.getLong(EXECUTION_DELAY);
} try {
Thread.sleep(delay);
} catch (Exception ignore) {
} _log.info("---" + jobName + " completed at " + new Date());
} }

  

/*
* 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
*
* http://www.apache.org/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.StatefulJob;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; /**
* <p>
* A dumb implementation of Job, for unittesting purposes.
* </p>
*
* @author James House
*/
public class StatefulDumbJob implements StatefulJob { /*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constants.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/ public static final String NUM_EXECUTIONS = "NumExecutions"; public static final String EXECUTION_DELAY = "ExecutionDelay"; /*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/ public StatefulDumbJob() {
} /*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Interface.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/ /**
* <p>
* Called by the <code>{@link org.quartz.Scheduler}</code> when a <code>{@link org.quartz.Trigger}</code>
* fires that is associated with the <code>Job</code>.
* </p>
*
* @throws JobExecutionException
* if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.err.println("---" + context.getJobDetail().getFullName()
+ " 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().getFullName()
+ " complete (" + executeCount + ")."); } }

最新文章

  1. 使用WCF 创建 Rest service
  2. c# DllImport 找不到指定模块
  3. Java线上应用故障排查之二:高内存占用
  4. Android使用SharedPreference存储数据
  5. [小技巧]初次接触 SSIS Package 的一点总结
  6. jsp - 引用 jar包.
  7. 拥有最小高度能自适应高度,IE、FF全兼容的div设置
  8. 关于ToolStrip设置Location无效的问题
  9. 获取手机wifi下的网络地址
  10. jQuery获取Select选择的Text和 Value(转,待测试确认)
  11. 201521123036 《Java程序设计》第9周学习总结
  12. gevent程序员指南
  13. layer兼容性问题
  14. Mac NPM 配置
  15. [日常工作]非Windows Server 系统远程经常断以及提高性能的方法
  16. NOIP2018普及组模拟赛
  17. Linux命令之ll
  18. 服务器由于redis未授权访问漏洞被攻击
  19. 51Nod 1135:元根(数论)
  20. Python的generator生成器

热门文章

  1. js模拟下载
  2. [Webpack 2] Grouping vendor files with the Webpack CommonsChunkPlugin
  3. php通过文件头检测文件类型通用类(zip,rar…)(转)
  4. JAVA对象之生
  5. 传输层:TCP UDP SCTP
  6. Xcode 5 安装coco2d-iphone
  7. SonarQube代码质量管理平台安装与使用--转载
  8. hdu2044java递推
  9. C#中的static静态变量的用法
  10. C#发送邮件-C#教程