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 多个触发器的更多相关文章

  1. Quartz Trigger Priority 触发器优先级

    Quartz Trigger Priority 触发器优先级 当多个触发器在一个相同的时间内触发,并且调度引擎中的资源有限的情况下,那么具有较高优先级的触发器先触发. 需要将配置文件中的org.qua ...

  2. 企业级任务调度框架Quartz(9) Quartz之作业触发器Trigger

    前序:      我们已经大概对Quartz的基本有了一个大概的认识:现在我们将要逐渐对Quartz的各个重要组件进行学习:前面已经对job进行了详细讲解,现在我们来认识下它的一个重要兄弟,没有它,作 ...

  3. Quartz.net Trigger触发器下 Cron表达式的格式

    有位博主写的不错,样式标准好理解,借鉴下. foamflower 1.   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] ...

  4. Quartz(自动任务)中的触发器Trigger

    1.Quartz中的触发器TriggerJob 包含了要执行任务的逻辑,但是 Job 对何时该执行却一无所知.这个事情留给了 Trigger.Quartz Trigger 继承了抽象的 org.qua ...

  5. [Quartz笔记]玩转定时调度

    简介 Quartz是什么? Quartz是一个特性丰富的.开源的作业调度框架.它可以集成到任何Java应用. 使用它,你可以非常轻松的实现定时任务的调度执行. Quartz的应用场景 场景1:提醒和告 ...

  6. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  7. quartz配置文件详解

    quartz配置文件详解(转载)     quartz学习总结: 一.关于job:    用Quartz的行话讲,作业是一个执行任务的简单Java类.任务可以是任何Java代码.只需你实现org.qu ...

  8. Spring-----定时任务Quartz配置

    第一种,作业类继承自特定的基类:org.springframework.scheduling.quartz.QuartzJobBean. 第一步:定义作业类 import org.quartz.Job ...

  9. Spring任务调度之Quartz

    一.Quartz作业类的继承方式来讲,可以分为两类: 作业类需要继承自特定的作业类基类,如Quartz中需要继承自org.springframework.scheduling.quartz.Quart ...

随机推荐

  1. sass教程汇总

    Sass @at-root http://www.w3cplus.com/preprocessor/Sass-3-3-new-feature-at-root-bem.html Sass中连体符(&am ...

  2. How to Run Node.js with Express on Mobile Devices

    We released a JXcore plugin for Apache Cordova recently and in this article I will show how to run a ...

  3. XSS之学习误区分析

    有段时间没写东西了, 最近看到zone里出现了很多“XSS怎么绕过某某符号的帖子”,觉得很多新手在寻找XSS时走进了一些误区,比如:专门想着怎么去“绕过”.这里做个总结,希望对大家有所帮助. 1. 误 ...

  4. Mysql的列索引和多列索引(联合索引)

    转自:http://blog.chinaunix.net/uid-29305839-id-4257512.html 创建一个多列索引:CREATE TABLE test (      id       ...

  5. UINavigationController使用的注意事项

    1.常用属性viewControllers //所有在栈中的控制器topViewController //栈顶控制器navigationBar //导航栏 竖屏下默认44,横屏默认32 2.对navi ...

  6. MPlayer

    名称   mplayer − 电影播放器 mencoder − 电影编解码器 概要   mplayer [选项] [文件|URL|播放列表|−] mplayer [选项] 文件1 [指定选项] [文件 ...

  7. 安装 Homebrew

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ...

  8. 【转】 如何查看linux版本 如何查看LINUX是多少位

    原文网址:http://blog.csdn.net/hongweigg/article/details/7192471 一.如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! ...

  9. [Buffalo] 一些SQL函数

    取得当前时间的函数:GETDATE() 计算时间的函数:DATEADD(datepart,number,date) 计算两个时间差额:DATEDIFF(datepart,startdate,endda ...

  10. @DataProvider ITestContext 参数

    package roger.testng; import java.util.Random; import org.testng.ITestContext; import org.testng.ann ...