cuiyi's blog(崔毅 crazycy)

记录点滴 鉴往事之得失 以资于发展
数据加载中……

Quartz Cron表达式

每次使用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 阅读(2696) 评论(1)  编辑  收藏 所属分类: JavaEE技术

评论

# re: Quartz Cron表达式  回复  更多评论   

刚好项目用到这个 ,太感谢
2013-06-21 20:46 | hypnosis01

只有注册用户登录后才能发表评论。


网站导航: