Quartz.NET,每 x 周重复一次

发布于 2024-10-15 11:33:37 字数 197 浏览 5 评论 0原文

我需要使用 Quartz.NET 实现以下场景:

每 n 周重复一次:
星期日和/或星期一、星期二、星期三、星期四、星期五、星期六...

所以例如我可以选择:星期一和星期四,并且每两周重复一次,这可能吗?

我发现可行的方法可能是使用 Cron 表达式,但到目前为止我还没有运气好“每 X 周重复一次”

谢谢!

i need to implement the following scenario using Quartz.NET:

Recur every n week(s) on:
Sunday and/or Monday, Tuesday, Wednesday, Thursday, Friday, Saturday...

So for example i might select: monday and thursday, and recur every 2 weeks, is this possible?

I figure it out that the way to go might be using Cron Expressions, but i haven't had luck so far with the "Recur Every X Weeks"

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初与友歌 2024-10-22 11:33:37

正在运行 Quartz.Net 2.0 和选项吗?它尚未正式发布,但其中有一种新的触发器类型可以解决您的问题。这称为日历间隔触发器。基本上你定义它就像你在问题中描述的那样。您将间隔设置为 2,间隔单位设置为周,并且每 2 周触发一次。我在此处写了一篇描述它的文章 。您可以在此处。

Is running Quartz.Net 2.0 and option for you? It has not been officially released yet, but in it there is a new trigger type that solves your problem. It's called the calendar interval trigger. Basically you define it just as you described in your question. You set the interval to 2 and the interval unit to weeks, and it fires every 2 weeks. I've written a post describing it here. You can access the source code documentation here.

单调的奢华 2024-10-22 11:33:37

这是我使用的解决方案...

当没有复发时,我使用 cron 触发器并选择日期并使其每周运行一次


0 0 * * 1,2,3

当每个选定的日子都有重复时,我添加一个 SimpleTrigger,基本上开始日期是一周中的某一天,然后我通过将重复乘以 7 来计算重复,

所以我最终会每天只需一个简单的触发器。

我希望这对其他人有帮助!

This is the solution that i used...

When there is no recurence i use a cron trigger and select the days and make it run every week

E.G.
0 0 * * 1,2,3

when there is recurence for each selected day i add a SimpleTrigger, bassically the start date is the day of the week, and then i calculate the recurrence by multiplying the recurence for 7

So i will end up wit one simpletrigger for each day.

I hope this helps someone else!

沫雨熙 2024-10-22 11:33:37

这是一个复杂的触发器,您可以通过3个触发器进行管理;

  1. 触发 2 周样本 cron: "0 0 0 1 *"
  2. 触发 2 周样本 cron: "0 0 0 15 *"
  3. 触发触发所选日期样本 cron: "0 0 0 ? * SUN-SAT"

第一个触发器将创建 3。
第二个触发器将删除 3。

祝你好运。

It is a complext trigger, you can manage by 3 triggers;

  1. trigger 2 weeks sample cron: "0 0 0 1 *"
  2. trigger 2 weeks sample cron: "0 0 0 15 *"
  3. trigger trig selected days sample cron: "0 0 0 ? * SUN-SAT"

first trigger will create 3.
second trigger will remove 3.

Good luck.

演出会有结束 2024-10-22 11:33:37

不幸的是,Quartz.Net 2 尚未发布、未记录,并且引入了重大更改。

就像奥雷里亚诺和邦戈所说,触发器的组合可能会有所帮助,但我不太明白他们各自的解决方案。

我的解决方案是包装 CronTrigger 并跳过不需要的事件:

var ct = new CronTrigger();
ct.CronExpression = new CronExpression(
    string.Format("0 {0} {1} ? * {2} *", 
    minuteOfHour, hourOfDay, daysList));
ct = new WeeklyTriggerWrapper(ct, 2);

public class WeeklyTriggerWrapper : CronTrigger
{
    public CronTrigger Trigger
    {
        get;
        private set;
    }

    public int WeekInterval
    {
        get;
        private set;
    }

    public DateTime? LastFireDateTime
    {
        get;
        private set;
    }

    public WeeklyTriggerWrapper(CronTrigger trigger, int weekInterval)
    {
        Trigger = trigger;
        WeekInterval = weekInterval;
    }

    public override DateTime? ComputeFirstFireTimeUtc(ICalendar cal)
    {
        return Trigger.ComputeFirstFireTimeUtc(cal);
    }

    public override DateTime? GetFireTimeAfter(DateTime? afterTimeUtc)
    {
        var result = Trigger.GetFireTimeAfter(afterTimeUtc);

        if (result.HasValue)
        {
            DateTime reference = StartTimeUtc;

            if (LastFireDateTime.HasValue && LastFireDateTime.Value > reference)
                reference = LastFireDateTime.Value;

            reference = reference.AddDays(7 * WeekInterval);

            while (result.HasValue && result.Value < reference)
                result = Trigger.GetFireTimeAfter(result.Value);
        }

        LastFireDateTime = result;
        return result;
    }

    // TODO: handle events...
}

Unfortunately Quartz.Net 2 is unreleased, undocumented, and introduces breaking changes.

Like Aureliano and Bongo say, a combination of triggers might help but I do not quite understand their respective solutions.

My solution is to wrap a CronTrigger and skip the unwanted events :

var ct = new CronTrigger();
ct.CronExpression = new CronExpression(
    string.Format("0 {0} {1} ? * {2} *", 
    minuteOfHour, hourOfDay, daysList));
ct = new WeeklyTriggerWrapper(ct, 2);

public class WeeklyTriggerWrapper : CronTrigger
{
    public CronTrigger Trigger
    {
        get;
        private set;
    }

    public int WeekInterval
    {
        get;
        private set;
    }

    public DateTime? LastFireDateTime
    {
        get;
        private set;
    }

    public WeeklyTriggerWrapper(CronTrigger trigger, int weekInterval)
    {
        Trigger = trigger;
        WeekInterval = weekInterval;
    }

    public override DateTime? ComputeFirstFireTimeUtc(ICalendar cal)
    {
        return Trigger.ComputeFirstFireTimeUtc(cal);
    }

    public override DateTime? GetFireTimeAfter(DateTime? afterTimeUtc)
    {
        var result = Trigger.GetFireTimeAfter(afterTimeUtc);

        if (result.HasValue)
        {
            DateTime reference = StartTimeUtc;

            if (LastFireDateTime.HasValue && LastFireDateTime.Value > reference)
                reference = LastFireDateTime.Value;

            reference = reference.AddDays(7 * WeekInterval);

            while (result.HasValue && result.Value < reference)
                result = Trigger.GetFireTimeAfter(result.Value);
        }

        LastFireDateTime = result;
        return result;
    }

    // TODO: handle events...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文