博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QUARTZ CRON表达式
阅读量:7106 次
发布时间:2019-06-28

本文共 3531 字,大约阅读时间需要 11 分钟。

每次使用Quartz Cron的时候都要去查;

(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.

转载于:https://www.cnblogs.com/fx2008/p/4243453.html

你可能感兴趣的文章
NLPIR-Parser智能系统文本挖掘的“挖掘机”
查看>>
GitHub上传项目到远程库
查看>>
格式化字符串
查看>>
实验五 函数程序设计
查看>>
ubuntu下lnmp的安装
查看>>
高斯消元
查看>>
关于正则表达式的一些实际应用
查看>>
Windows 建立链接
查看>>
JavaScript进行DOM操作时的一点点小经验
查看>>
Android获取手机应用
查看>>
Weblogic12c安装与配置详解
查看>>
结对-英文词频检测-结对项目总结
查看>>
php随机生成汉字
查看>>
android:scaleType属性
查看>>
在Ubuntu上手动安装nginx软件
查看>>
Linux任务前后台的切换
查看>>
SpringBoot之第一个应用HelloWorld
查看>>
Thinkphp 数据的修改及删除操作
查看>>
hbase命令
查看>>
Java序列化和反序列化
查看>>