/*
* 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 + ")."); } }

Quartz1.8.5例子(五)的更多相关文章

  1. Quartz1.8.5例子(二)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  2. scrapy-splash抓取动态数据例子五

    一.介绍 本例子用scrapy-splash抓取智能电视网网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站 ...

  3. 计算机网络再次整理————tcp例子[五]

    前言 本文介绍一些tcp的例子,然后不断完善一下. 正文 服务端: // See https://aka.ms/new-console-template for more information us ...

  4. 从零开始学习Node.js例子五 服务器监听

    httpsnifferInvoke.js var http = require('http'); var sniffer = require('./httpsniffer'); var server ...

  5. Quartz1.8.5例子(十四)

    org.quartz.scheduler.instanceName: PriorityExampleScheduler # Set thread count to 1 to force Trigger ...

  6. Quartz1.8.5例子(十一)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  7. Quartz1.8.5例子(十)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  8. Quartz1.8.5例子(九)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  9. Quartz1.8.5例子(八)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

随机推荐

  1. mybatis10 实现类代理对象开发

    mapper实现类代理对象开发 要想让mybatis自动创建dao接口实现类的代理对象,必须遵循一些规则: SqlSession sqlSession = sqlSessionFactory.open ...

  2. mybatis04 根据用户名称模糊查询用户信息

    根据用户名称模糊查询用户信息可能返回多条记录. 1.1.1User.xml 编码 如果用%进行模糊查询,#{}表示一个占位符会被翻译为一个?号(SELECT * FROM USER WHERE id= ...

  3. CentOS6.4下使用默认的文档查看器打开PDF文档乱码的解决方案

     最近在CentOS6.4下使用其默认的文档查看器打开PDF文档时出现乱码的方块,有两种方法可以解决.    方法一:修改/etc/fonts/conf.d/49-sansserif.conf文件,如 ...

  4. GUI编程笔记(java)03:GUI的组件继承图

    1.组件继承图: 2.分析上面的组件继承图 (1)Component:public abstract class Component extends Object implements ImageOb ...

  5. linux find grep使用

    在当前目录下所有文件中查找内容包含 string 的文件: find ./ -name "*" -exec grep "string" {} \; 注意:在最后 ...

  6. <div>相关

    定义 <div>是一个块级元素[会自动换行] 用法 <div>可用于划分独立的一个块状区域,其内部内容显示在<div>的content部分内 结构 [盗用张图] 从 ...

  7. js判断手机端操作系统(Andorid/IOS)

    非常实用的js判断手机端操作系统(Andorid/IOS),并自动跳转相应下载界面 androidURL = "http://xxx/xxx.apk"; var browser = ...

  8. Hello World深入理解

    每个编程人员都知道第一个都是Hello World, 可是只是单知道 用,不知道为何会这样,就一直学的只是皮毛. 学东西,不能知其然而不知其所以然.这样永远达不到境界. 我们用编辑器eclipse 创 ...

  9. C# - linq查询现有的DataTable

    可以通过linq对现有的DataTable进行查询,并将结果拷贝至新的DataTable中例如: // Query the SalesOrderHeader table for orders plac ...

  10. SQL Server 表字段值转列名 示例

    前几天,同事问我怎样把字段值转换成字段列,就写了一个最简单的Demo分享一下. 代码如下: -- 创建测试表以及添加测试数据create table #temp(a money,b varchar(1 ...