Quartz 多个触发器
http://www.oschina.net/code/snippet_114990_4440
最近项目中要做个定时生成静态html文件东东,7点到19点每5分钟生成一次,其他时间1小时生成一次,刚开始就走错了 居然想用一条cron表达式搞定,搞了半天,问了好多人,得到一个好的办法,就是给以个job创建多个触发器,不扯了,看代码。。
创建job并给job添加多个触发器
package com.f139.frame.job; import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.CronScheduleBuilder.cronSchedule; import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.nutz.ioc.Ioc;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory; import com.f139.frame.pojo.factory.Template; public class CreateJob { private static SchedulerFactory sf = new StdSchedulerFactory(); public static void createTemplateJob(Map<Integer, Template> map, Ioc ioc) {
Scheduler sched;
try {
sched = sf.getScheduler(); // ioc参数,将ioc传递到job中
Map<String, Object> params = new HashMap<String, Object>();
params.put("ioc", ioc);
// 获取所有模板
Collection<Template> templates = map.values();
for (Template template : templates) {
if (template.getIntervalTime() > 0) {
// 将当前模板ID传入job中
params.put("templateID", template.getTemplateID());
// 创建作业
JobDetail jobDetail = newJob(TemplateJob.class).withIdentity(new JobKey("templateJob_" + template.getTemplateID(), "template")).usingJobData(
new JobDataMap(params)).build();
// 创建触发器,并将触发器加入到作业中
sched.scheduleJob(jobDetail, newTrigger().withIdentity(new TriggerKey("between7and19_" + template.getTemplateID(), "template")).withSchedule(
cronSchedule("0 0/1 7-19 * * ?")).forJob(jobDetail).build());
sched.scheduleJob(newTrigger().withIdentity(new TriggerKey("between0and7_" + template.getTemplateID(), "template")).withSchedule(
cronSchedule("0 0/5 0-7 * * ?")).forJob(jobDetail).build());
sched.scheduleJob(newTrigger().withIdentity(new TriggerKey("between19and23_" + template.getTemplateID(), "template")).withSchedule(
cronSchedule("0 0/5 19-23 * * ?")).forJob(jobDetail).build());
}
}
sched.start();
} catch (SchedulerException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} }
}
job处理类
package com.f139.frame.job; import java.util.Map; import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import com.f139.frame.freemarker.FreemarkerUtile;
import com.f139.frame.pojo.factory.Log;
import com.f139.frame.pojo.factory.Template;
import com.f139.frame.system.LocalCache;
import com.f139.frame.util.DateUtil; public class TemplateJob implements Job { private Dao dao = null;
private Ioc ioc = null; @Override
@SuppressWarnings("unchecked")
public void execute(JobExecutionContext context) throws JobExecutionException {
Map<String, Object> params = null;
Template template = null;
FreemarkerUtile freemarkerUtile = null;
try {
// 获取参数
params = context.getJobDetail().getJobDataMap();
// 获取ioc
ioc = (Ioc) params.get("ioc"); // 获取Dao
dao = ioc.get(NutDao.class,"dao"); // 获取当前模板
template = LocalCache.selectTemplateByID.get(Integer.parseInt(params.get("templateID").toString()));
// 获取FreemarkerUtile
freemarkerUtile = ioc.get(FreemarkerUtile.class, "freemarkerUtile");
// 创建文件
freemarkerUtile.createHtml(template.getTemplateContent(), template.getFileUrl(), null); } catch (Exception e) {
FailLog("模板" + template.getTemplateName() + "在" + DateUtil.getNowString() + "生成静态文件时发生异常!");
} } public void FailLog(String message) {
Log log = new Log();
log.setUserName("admin");
log.setLogClass("html");
log.setLogLevel("9");
log.setLogMessage(message);
log.setUpdateTime(DateUtil.getNowString());
dao.insert(log);
} }
Quartz 多个触发器的更多相关文章
- Quartz Trigger Priority 触发器优先级
Quartz Trigger Priority 触发器优先级 当多个触发器在一个相同的时间内触发,并且调度引擎中的资源有限的情况下,那么具有较高优先级的触发器先触发. 需要将配置文件中的org.qua ...
- 企业级任务调度框架Quartz(9) Quartz之作业触发器Trigger
前序: 我们已经大概对Quartz的基本有了一个大概的认识:现在我们将要逐渐对Quartz的各个重要组件进行学习:前面已经对job进行了详细讲解,现在我们来认识下它的一个重要兄弟,没有它,作 ...
- Quartz.net Trigger触发器下 Cron表达式的格式
有位博主写的不错,样式标准好理解,借鉴下. foamflower 1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] ...
- Quartz(自动任务)中的触发器Trigger
1.Quartz中的触发器TriggerJob 包含了要执行任务的逻辑,但是 Job 对何时该执行却一无所知.这个事情留给了 Trigger.Quartz Trigger 继承了抽象的 org.qua ...
- [Quartz笔记]玩转定时调度
简介 Quartz是什么? Quartz是一个特性丰富的.开源的作业调度框架.它可以集成到任何Java应用. 使用它,你可以非常轻松的实现定时任务的调度执行. Quartz的应用场景 场景1:提醒和告 ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- quartz配置文件详解
quartz配置文件详解(转载) quartz学习总结: 一.关于job: 用Quartz的行话讲,作业是一个执行任务的简单Java类.任务可以是任何Java代码.只需你实现org.qu ...
- Spring-----定时任务Quartz配置
第一种,作业类继承自特定的基类:org.springframework.scheduling.quartz.QuartzJobBean. 第一步:定义作业类 import org.quartz.Job ...
- Spring任务调度之Quartz
一.Quartz作业类的继承方式来讲,可以分为两类: 作业类需要继承自特定的作业类基类,如Quartz中需要继承自org.springframework.scheduling.quartz.Quart ...
随机推荐
- 如何在Ubuntu Unity上修改应用程序图标
转自如何在Ubuntu Unity上修改应用程序图标 这篇文章将教大家在Ubuntu Unity上修改应用程序图标,这个教程适合于Ubuntu 14.04, Ubuntu 13.10, Ubuntu ...
- jsp接收相同复合参数 request.getParameterValues()用法
在网站中往往需要用户选择复选框,此时需要创建多个复选框让用户进行选择: <head> <meta http-equiv="Content-Type" conten ...
- explain 用法详解
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...
- 使用NSURLSession获取网络数据和下载文件
使用NSURLSession获取网络数据 使用NSURLSession下载文件
- c++ 学习笔记 c++ 引用C库注意点:#ifdef __cplusplus 倒底是什么意思?
时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #en ...
- ANDROID_MARS学习笔记_S01原始版_011_XML
一.代码 1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- linux c/c++ GDB教程详解
学习使用了GDB一段时间后,发现它真的好强大!好用! GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在U ...
- mplayer windows configure修改
相信大家在编译mplayer的时候,都会遇到一个问题,就是那个折腾人的mplayer会检测当面目录下有没有ffmpeg的文件夹. 没有的话,mplayer会启动git进行漫长的下载ffmpeg源码.其 ...
- windows 下 使用codeblocks 实现C语言对python的扩展
本人比较懒就粘一下别人的配置方案了 从这开始到代码 摘自http://blog.csdn.net/yueguanghaidao/article/details/11538433 一直对Python扩展 ...
- 【CSS3】
Web前端实验室http://demo.doyoe.com/ ::before ::afterCSS3已经将伪元素的前缀更改为双冒号,而伪类则保持为单冒号 backface-visibility ht ...