简体   繁体   中英

Quartz.net cron trigger expression for every two week

I need to create cron trigger which runs on Friday in every tow week

0 30 17 ? * FRI#2 *

is what I managed to create, but the schedule it generates simply runs on every second Friday of month. Is it possible to achieve what I need using Cron Expression ?

I heard about new trigger type in quartz.net 2.x but could not get any good example our to configure it in XML config file. What I mean is CalendarIntervalTrigger Can anyone assist me on how to get desired result?

UPDATE

            var trigger = TriggerBuilder.Create()
            .WithIdentity("InvoiceGenerator", "InvoiceGenerator")
            .StartNow()
            .WithSchedule(CalendarIntervalScheduleBuilder.Create().WithIntervalInWeeks(2)).Build();

this is what I managed to create on c# side (no xml unfortunately) but when i call GetNextFireTimeUtc on trigger it always returns null.

UPDATE Thank your all for your answers what I managed to came up with is to either create normal cron trigger which runs every Friday and in execution check weather tow weeks passed science last run or not.

with c# it is the following code snippet.

        var friday = new DateTime(2015, 04, 10, 17, 30, 00);
        var trigger = TriggerBuilder.Create()
                .WithIdentity("InvoiceGenerator", "InvoiceGenerator")
                .WithCalendarIntervalSchedule(x => x.WithIntervalInWeeks(2))
                .WithDescription("trigger")
                .StartAt(friday)
                .Build();
        var nexRun = trigger.GetFireTimeAfter(friday);

nexRun is correct in this case. The only thing left now is to figure out who we can configure it using XML.

This is fairly simple to do programmatically with one trigger. I don't think it can be done via a single cron expression.

        var friday = new DateTime(2015, 04, 02, 17, 30, 00)
        var trigger = TriggerBuilder.Create()
                .WithIdentity("InvoiceGenerator", "InvoiceGenerator")
                .WithCalendarIntervalSchedule(x => x.WithIntervalInWeeks(2))
                .WithDescription("trigger")
                .StartAt(friday) 
                .Build();

It seems to me you are using RAMJobStore in which case your triggers are kept in the quartz_jobs.xml file. It is indeed possible to set calendar intervals in XML. For example, the above trigger would translate as follows:

<trigger>
  <calendar-interval>
    <name>calendarJobName</name>
    <group>calendarJobGroup</group>
    <description>CalendarTriggerDescription</description>
    <job-name>jobName1</job-name>
    <job-group>jobGroup1</job-group>
    <start-time>2015-04-02T17:30:00.0Z</start-time>
    <end-time>2099-04-02T17:30:00.0Z</end-time>
    <misfire-instruction>SmartPolicy</misfire-instruction>
    <repeat-interval>2</repeat-interval>
    <repeat-interval-unit>Week</repeat-interval-unit>
  </calendar-interval>
</trigger>

This can't be achieved with a cron expression. You can get close by adding three triggers to your Quartz job, although it doesn't meet your requirements for months with five Fridays.

The first trigger should fire every first Friday of the month:

0 30 17 ? * FRI#1 *

And the second trigger should fire every third Friday of the month

0 30 17 ? * FRI#3 *

And a third trigger is needed to handle months with a fifth Friday:

0 30 17 ? * FRI#5 *

This last trigger is not perfect as the next run will be in a week's time.

Alternatively you could create a cron expression that fires every week with the Quartz job checking the last fire time and do nothing if the last fire time is less than 14 days ago.

I solved this issue by creating multiple jobs for each Day of the Week, then using the CalendarIntervalScheduleBuilder to set the weekly interval. You will have to calculate your next startdate manually though.

Eg

For each DayOfWeek in MyDaysOfWeek
    StartDate = GetNextWeekDay(JobStartDate,DayOfWeek)
    Dim Sched = CalendarIntervalScheduleBuilder.Create().WithIntervalInWeeks(Interval).WithMisfireHandlingInstructionDoNothing()

    Dim JobTrigger = TriggerBuilder.Create().WithIdentity(JobTriggerName).
                            StartAt(StartDate).WithSchedule(Sched).EndAt(EndDate).Build()

     'Add to your scheduler here
next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM