What is corresponding Cron expression to fire in every X seconds, where X > 60? --转载

本文探讨了在使用Quartz进行定时任务调度时遇到的问题,即如何实现每X秒执行一次任务的需求。由于标准的Cron表达式不支持秒级的精确控制,尤其是在X值超过59秒的情况下。文章给出了正确的解决方案,建议使用SimpleTrigger而非CronTrigger来实现这一需求。

原文地址:http://stackoverflow.com/questions/2996280/what-is-corresponding-cron-expression-to-fire-in-every-x-seconds-where-x-60

Question:

I want my jobs to execute in every X seconds, there's one to one matching between job and X. Also during runtime there can be registered new jobs with their own intervals.

I've tried to write cron expression for such scenarios, but in documentation there's written that value of seconds can't be more than 59. So cron expression like this: "0/63 * * * * ?" isn't valid.

At first sight solution of that problem seemed to be expression like this: "0/3 0/1 * * * ?", but it means completely different thing: trigger job in every three second of every minute.

Can you suggest what is the right solution (cron expression) for that? I know I could use just simple timers, but I've to use cron jobs using Quartz.

 

answer:

The Quartz cron syntax is designed to express timing based on the standard 24 hour, 60 minute clock. It's not suitable for "every n seconds", where n can be any value. You're not going to able to do this with cron expressions.

If you need to do this with Quartz, you should use SimpleTrigger, not CronTrigger. You don't really have an option, unless you want to use a hugely complex collection of overlapping, artificially-deduced cron expressions.

 

碰到这个问题的时候,我陷入了误区,在stackoverflow上一看,明白自己掉坑里了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值