Through the previous article, I believe that you have a basic understanding of Quartz. Net and can establish the simplest service. This article describes how to set Trigger conditions for a service.
According to the scenario, I usually encounter the following situations at work:
1. Execute when the service starts
This Trigger has been used in the previous example. If you do not need to talk about it, directly add the code.
ITrigger trigger = TriggerBuilder.Create().StartNow().Build()
2. Round-Robin execution within a specified interval
Using SimpleTriggerImpl in the framework, you can easily construct this type of trigger. The following lists several frequently-used constructor loads:
- SimpleTriggerImpl (string name, int repeatCount, TimeSpan repeatInterval)
- SimpleTriggerImpl (string name, DateTimeOffset startTimeUtc, DateTimeOffset? EndTimeUtc, int repeatCount, TimeSpan repeatInterval)
For example:
View Code
SimpleTriggerImpl(string name, int repeatCount, TimeSpan repeatInterval)
SimpleTriggerImpl(string name, DateTimeOffset startTimeUtc,
DateTimeOffset? endTimeUtc, int repeatCount, TimeSpan repeatInterval)
The trigger indicates that the start time is "Start now (UTCNOW required)" + "End Time" + "repeat count 1" (note that the repeat count JOB runs twice) + "Round Robin interval" is 10 seconds.
3. daily round robin
Using the self-contained DailyTimeIntervalTriggerImpl in the framework, you can easily construct this type of trigger. The following lists several frequently used constructor loads:
- DailyTimeIntervalTriggerImpl (String name, TimeOfDay startTimeOfDayUtc, TimeOfDay endTimeOfDayUtc, IntervalUnit intervalUnit, int repeatInterval)
- DailyTimeIntervalTriggerImpl (string name, DateTimeOffset startTimeUtc, DateTimeOffset? EndTimeUtc, TimeOfDay startTimeOfDayUtc, TimeOfDay endTimeOfDayUtc, IntervalUnit intervalUnit, int repeatInterval)
For example:
View Code
DailyTimeIntervalTriggerImpl trigger = new DailyTimeIntervalTriggerImpl("DailyTimeIntervalTrigger",DateTimeOffset.UtcNow,null, new TimeOfDay(1, 0, 0), new TimeOfDay(22, 01, 00), IntervalUnit.Minute, 1);
trigger.DaysOfWeek.Add(DayOfWeek.Monday);
The trigger indicates that the start time is immediate (UTCNOW is required) + the end time is none + each day, and the end time is each day + round robin per minute + repeated times 1 + only executed per Monday
4. complex time settings
If you want to build a time setting that is as flexible as SqlServerJob settings, you need to use the CronTriggerImpl class. The method is very simple. You can simply put the Cron expression in the structure.
ITrigger trigger = new CronTriggerImpl("CronTrigger", "TriggerGroup1", "0 0 12 * * ?");
This trigger means that it is executed at every day.
Note: This part of the younger brother did not have a deep understanding, and this part of the API has not been modified, so he will not be able to make a shift. Search for Cron expression/Cron expression by yourself