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

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

问题描述

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

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


  1. 9月30日开始

  2. 每5个月重复
  3. >
  4. 能够使用 ITrigger.GetFireTimeAfter()计算/预测下一次触发时间(用户界面反馈)

  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

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

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

但是预计的日期是:


  • 2017-9-30

  • 2018-9-30

  • 2019-9-30

不能使用 CalendarIntervalTrigger 之一:


例如,如果您选择一个开始时间为1月31日,并且触发器的单位为月和间隔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将在四月中求值。

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

这是错误的!,因为四月没有t适合 1/5

which is wrong! becouse April doesn't fit 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天全站免登陆