3

I am quite new to quartz.net and cron expression and I have to create a quartz.net scheduler in the c# dotnet application which should execute on Monthly, Weekly and daily based on the values it is getting from the database.

Table which is having the scheduling details.

Id EffectiveDate  StartTime  Frequency  
 1  2012-04-22      20:55      Daily       
 2  2012-04-22      10:12      Weekly     
 3  2012-04-22      17:00      Daily   
 4  2012-04-23      02:15      Monthly   
 5  2012-04-26      18:30      Daily        
 6  2012-04-27      11:45      Weekly

Please help me to solve this problem.

user1301587
  • 455
  • 4
  • 11
  • 23

1 Answers1

3

@user1301587, hopefully you have found a way forward, but I noticed that your question still shows up quite high on Google so I will go ahead and add some psuedo code on how I would do this:

  1. Fetch the schedules from your datasource (looks like an RDBMS database table in your case)
  2. Iterate through the list of schedules and build an instance of CronScheduleBuilder with the schedule:

    string cronExpression = string.Format("{0} {1} {2} {3} {4} {5}", secondPart, minutePart, hourPart, dayOfMonthPart, monthPart, dayOfWeekPart );

    IScheduleBuilder scheduleBuilder = CronScheduleBuilder
                    .CronSchedule(cronExpression)
                    .InTimeZone(TimeZoneInfo.Utc);
    

E.g. the cron expression 0 26 6 * * ? runs your job at 6:26 AM every day of the week One gotcha I've found is that if you do not specify the time zone, Quartz will apply conversion to bring the specified time into UTC time based on your current timezone

Now you can build a trigger with the schedule built above:

ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                .WithIdentity("TestTrigger")
                .WithSchedule(scheduleBuilder)
                .Build();

Use THIS site to build a Quart.Net style expression

Sudhanshu Mishra
  • 6,523
  • 2
  • 59
  • 76