Quartz.net - 每 m 个月在第 n 天重复一次? [英] Quartz.net - Repeat on day n, of every m months?

查看:28
本文介绍了Quartz.net - 每 m 个月在第 n 天重复一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Quartz.NET,我正在尝试创建一个触发器:

Using Quartz.NET, I'm trying to create a trigger that:

  1. 开始于9 月 30 日
  2. 最后一天
  3. 重复
  4. 每 5 个月.
  5. 能够使用 ITrigger.GetFireTimeAfter() 来计算/预测下一次触发时间(UI 反馈)
  1. starts September 30th,
  2. repeats on the last day
  3. of every 5 months.
  4. be able to use ITrigger.GetFireTimeAfter() to compute/project the next fire times (UI feedback)

预期:

  • 2017-9-30
  • 2018-2-28
  • 2018-7-31
  • 2018-12-31

我想我可以使用 CronTrigger(即 0 0 0 L 9/5 ? *)

I thought I could use a CronTrigger (ie 0 0 0 L 9/5 ? *)

但预计的天数是:

  • 2017-9-30
  • 2018-9-30
  • 2019-9-30

也不能使用 CalendarIntervalTrigger:

例如,如果您选择发生在 1 月 31 日的开始时间,并且具有单位为 Month 和间隔 1 的触发器,则下一次触发时间将是 2 月 28 日,之后的下一次将是 3 月 28 日 -并且基本上每个后续的发射都会发生在当月的 28 日,即使存在第 31 天.如果你想要一个总是在一个月的最后一天触发的触发器——不管这个月有多少天,你应该使用 ICronTrigger

For example, if you choose a start time that occurs on January 31st, and have a trigger with unit Month and interval 1, then the next fire time will be February 28th, and the next time after that will be March 28th - and essentially each subsequent firing will occur on the 28th of the month, even if a 31st day exists. If you want a trigger that always fires on the last day of the month - regardless of the number of days in the month, you should use ICronTrigger

Quartz.NET 文档

我如何安排这样的触发器?我是否必须从头开始实现我自己的触发器和 IScheduler?或者有没有办法让我注入自定义计算?

How can I schedule a trigger like this? Do I have to implement my own trigger and IScheduler from scratch? Or is there a way for me to inject in a custom calculation?

谢谢

推荐答案

这不是 cron 表达式的工作方式.通过将其定义为9/5",您只是表达了从 9 月开始"和5 个月前增加",但没有第 14 个月.每个超过月数的月份都会被削减,例如 1/5 将评估为一月",然后是六月",最后是十一月".另一次尝试将溢出月数,cron 将从头开始.所以它将从另一个一月"开始,然后是六月",最后是十一月".看看这个表达式,如果它像你想要的那样工作,它就不会适合明年的表达式,因为例如 1/5 将在April"中计算

It's not the way how the cron expression works. By defining it "9/5" you just expressed "start at september" and "icrement by 5 month" but there is no fourteenth month. Every month that exceed months count will be cutted so for example 1/5 will evaluate to "January" then "June" and lastly to "November". Another try will overflow months count and cron will start from the begining. So it will start from another "January", then "June" and lastly "November". Look at the expression, if it would work like you want, it wouldn't fit the expression in the next year becouse for example 1/5 would be evaluated in "April"

1/5 => January (2017) +5
1/5 => June (2017) +5
1/5 => November (2017) +5
1/5 => April (2018) +5

这是错误的!因为四月不适合1/5

这是正确的行为:

1/5 => January (2017) +5
1/5 => June (2017) +5
1/5 => November (2017) +5
1/5 => January (2018) +5
1/5 => June (2018)
... 

您真正需要的是以不同方式工作的工具.我不认为有什么方法可以强制 Quartz.NET Cron 按您的意愿工作.唯一的方法是用不同的东西替换" Quartz.NET cron 评估器.如果你环顾四周,你会发现我是作者的库,它实现了小型领域特定语言,它应该更适合你想做的事情.我在这里描述过:评估复杂的时间模式

What you really need is tool that works in a different way. I don't think there is some way to force Quartz.NET Cron to work as you want. The only way would be to "replace" Quartz.NET cron evaluator with something different. If you look around a bit, You could find library which I'm an author that implement small domain specific language that should be much more fitted for what you would like to do. I described it here: Evaluate complex time patterns

这篇关于Quartz.net - 每 m 个月在第 n 天重复一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆