本文来自:http://www.blogjava.net/crazycy/archive/2013/06/06/400287.html

每次使用Quartz Cron的时候都要去查manual document;
(URI:http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger)

对于第四个day of month 和 第六个 day of week常常需要花时间,这里做个简单总结

*    *   *     *    *    *   (year optional)
┬   ┬    ┬    ┬    ┬    ┬
│   │    │    │    │    │
│   │    │    │    │    │
│   │    │    │    │    └───── day of week (0 - 7) (0 or 7 is Sun, or use names)
│   │    │    │    └────────── month (1 - 12)
│   │    │    └─────────────── day of month (1 - 31)
│   │    └──────────────────── hour (0 - 23)
│   └───────────────────────── min (0 - 59)
└────────────────────────      seconds
Wild-cards (the * character) can be used to say "every" possible value of this field. 
Therefore the * character in the "Month" field simply means "every month". 
A '*' in the Day-Of-Week field would therefore obviously mean "every day of the week".

The '?' character is allowed for the day-of-month and day-of-week fields. 
It is used to specify "no specific value". This is useful when you need to specify something in one of the two fields, but not the other.

为了解释清楚“?"字符的使用,再来一段

Field Name    Mandatory    Allowed Values    Allowed Special Characters
Seconds         YES            0-59                        , - * /
Minutes          YES            0-59                        , - * /
Hours             YES            0-23                        , - * /
Day of month  YES            1-31                        , - * ? / L W
Month             YES            1-12 or JAN-DEC      , - * /
Day of week    YES            1-7 or SUN-SAT        , - * ? / L #
Year                NO            empty, 1970-2099     , - * /

可以看到只有第四、六两个位置允许使用"?"
这就说明这2个位置是相互依赖的
? ("no specific value") - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field. See the examples below for clarification.

所以一旦用了"?",就说明这个字段不起作用了,对应的另一个字段起作用;

所以:
1. 配置一个任务在每天凌晨2点运行做出截止到当日的报表,但是周末因为没人值班所以不需要生成报表,这个表达式就是
    0 0 2 ? * MON-FRI
2. 配置一个任务在每个月的最后一天夜里11点运行
    0 0 23 L * ?

如果想用数据库驱动这个时间怎么办呢?请问下面大虾的做法:

Dec 22nd, 2008, 01:53 AM #2 Siva Krishna  
Hello,

I got almost similar requirement, making the schedulers as DB driven, and handled it in the following way.

I created a new UI that takes start time & interval time and saves them in DB. Then a method is called to refresh the given jobs/schedulers.

Here is the snippet.

Code:
try {
    scheduler = (StdScheduler) context.getBean(schedulerVO.getSchedulerName());
    triggerNames = new String[] {};

if (scheduler != null) {
        try {
            // throws SchedulerException
            triggerNames = scheduler.getTriggerNames("DEFAULT");
            triggerName = triggerNames.length > 0 ? triggerNames[0] : "";
            trigger = (CronTrigger) scheduler.getTrigger(triggerName, "DEFAULT");
            if (trigger != null) {
                // throws ParseException                                    
                trigger.setCronExpression(getCronExpression(schedulerVO.getStartTime(), schedulerVO.getInterval()));

// throws SchedulerException
                scheduler.rescheduleJob(triggerName, "DEFAULT",trigger);
            } 
        } catch (SchedulerException e) {                    
            logger.error(e);
        } catch (ParseException e) {
            logger.error(e);
        }
    }
} catch (NoSuchBeanDefinitionException e) {
    logger.error(e);
}

Computing the cronExpression with this method.

Code:
private String getCronExpression(String startTime, String interval) {        
    String cronExpression = "";
    if ("0".equals(startTime) || "0".equals(interval)) {
        // default trigger runs at 10AM & 10PM            
            cronExpression = "0 0 10/12 * * ?";
    } else {
        cronExpression = "0 0 " + startTime + "/" + interval + " * * ?";
        }
    return cronExpression;
}
As I need to run the job every day and not concerned about minutes the above approach worked for me.

Hope this gives an idea to you.

Regards
Siva Krishna.

POSTED ON 2013-06-06 11:35 CRAZYCY 阅读(2095) 评论(1)  编辑  收藏 所属分类: JAVAEE技术

QUARTZ CRON的更多相关文章

  1. Quartz Cron 触发器 Cron Expression 的格式

    转自:http://blog.csdn.net/yefengmeander/article/details/5985064 上一文中提到 Cron触发器可以接受一个表达式来指定执行JOB,下面看看这个 ...

  2. Quartz Cron表达式的二三事

    最近在解决产品上的一个需求,就是定期生成报告(Report),我们叫做Scheduled Report. 原理:UI获取用户输入的时间信息,后台使用Spring框架设置定时任务,这里定时任务用的就是  ...

  3. Spring 调度工具Quartz cron 表达式的格式

    http://chinaxxren.iteye.com/blog/839630 Quartz cron 表达式的格式十分类似于 UNIX cron 格式,但还是有少许明显的区别.区别之一就是 Quar ...

  4. Quartz cron 表达式(linux 定时器,java 定时任务,spring task定时任务)

    原文地址:https://blog.csdn.net/feng27156/article/details/39293403 Quartz cron 表达式的格式十分类似于 UNIX cron 格式,但 ...

  5. Quartz 之Quartz Cron表达式

    说到这个Quartz了,必不可少的就要说到我们的Triggger触发器,相信大家也都知道,我们在之前也说过了,Trigger又有两个子类,也就是两种方式,分别是:SimpleTrigger和CronT ...

  6. Quartz Cron表达式生成器

    格式: [秒] [分] [小时] [日] [月] [周] [年]  序号 说明   是否必填  允许填写的值 允许的通配符   1  秒  是  0-59    , - * /  2  分  是  0 ...

  7. Quartz Cron表达式 每周、每月执行一次

    原文:https://blog.csdn.net/qq_33432559/article/details/75633767 系统中通常有些需要自动执行的任务,这些任务可能每隔一段时间就要执行一次,也可 ...

  8. [quartz] - Cron表达式举例

    Quartz是一个任务调度框架.比如你遇到这样的问题 想每月25号,信用卡自动还款 想每年2月14日自己给当年暗恋女神发一封匿名贺卡 想每隔1小时,备份一下自己的SpringCloud学习笔记到云盘 ...

  9. quartz cron表达式在线生成

    近期使用了quartz定时器,有感于起cron表达式有点复杂.且无法实时推断定时时间是否正确,因此写了个在线表达式及依据表达式获得前10次运行时间. 訪问地址例如以下:http://cron.g2ro ...

随机推荐

  1. 页面动态数据的滚动效果——jquery滚动组件(vticker.js)

    <script language="javascript" src="lirms/Test/jquery-1.4.2.js"></script ...

  2. yii使用寻呼功能

    CDbCriteria这是类包使用,包是yii自带专门用来处理类似分类这种功能的. 而我们使用yii框架然后调用这种方法会起到事半功倍的效果,会发现使用这个可以节省非常多的时间.让你高速的使用PHP中 ...

  3. MP3文件结构及解码概述

    MP3文件结构概述 Layer-3音频文件.MPEG(MovingPicture Experts Group)在汉语中译为活动图像专家组,特指活动影音压缩标准,MPEG音频文件是MPEG1标准中的声音 ...

  4. Hacker(15)----嗅探原理

    嗅探指窃听网络中流经的数据包,这里的网络一般指用集线器或路由器组建的局域网.通过嗅探并解析数据包,便可知道数据包中的信息,一旦含有账户密码等隐私信息就可能造成个人资金损失. 嗅探数据包无法通过输入命令 ...

  5. overflow:hidden

    原来以为overflow:hidden能隐藏所有溢出的子元素,但今天发现我错了. 对于overflow:hidden的最大误解时:当一个具有高度和宽度中至少一项的容器应用了overflow:hidde ...

  6. Could not load file or assembly 试图加载格式不正确的程序

    问题: 今天发布项目的时候遇到这个破问题,纳闷了好久,最后想起来自己改过程序生成的目标平台(原生成目标平台是Any CPU,被我改成了X86的). 解决方法: 改回原来的Any CPU 从新发布即可.

  7. Java---文件夹及文件操作

    /** * 获取文件夹大小 * @param file File实例 * @return long */ public static long getFolderSize(java.io.File f ...

  8. winsock开发重复定义问题

    参考: VS2013使用winsock.h和winsock2.h发生冲突后的终极解决方法:http://www.cnblogs.com/Shirlies/p/5137548.html WINSOCK. ...

  9. (原)在ubuntu 中安装 swi prolog 和 简单的使用

    参考网址:http://www0.cs.ucl.ac.uk/staff/mahmed/teaching/intro.html 参考网址:http://www.swi-prolog.org/build/ ...

  10. MySql存储引擎介绍

    MySQL5.5以后默认使用InnoDB存储引擎,其中InnoDB和BDB提供事务安全表,其它存储引擎都是非事务安全表.若要修改默认引擎,可以修改配置文件中的default-storage-engin ...