Chr☆s Kwok 的技术笔记

.NET, C#, WPF, WCF, WF, .NetCore & LINQ ... I know how it works because I know why it works ...

博客园 首页 新随笔 订阅 管理

通过quartz配置一个任务,比如我设定每小时(整点)执行一次,比如15:28启动,预想结果应该是,在16:00第一次执行,然后类推。结果经常是,15:28就进行了第一次执行,也就是说quartz在启动时不按照预定排程执行了一次。这在实际操作中会带来不小的麻烦。

解决办法:((CronTriggerImpl)trigger).MisfireInstruction = MisfireInstruction.CronTrigger.DoNothing;

_scheduler = await GetSchedulerAsync();
Type type = Type.GetType(scheduleInfo.JobName, true, true);
IJobDetail job = JobBuilder.Create(type)
                            .WithIdentity(scheduleInfo.JobName, scheduleInfo.JobGroup)
                            .Build();
ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                            .StartAt(startRunTime)
                            .EndAt(endRunTime)
                            .WithIdentity(scheduleInfo.JobName, scheduleInfo.JobGroup)
                            .WithCronSchedule(scheduleInfo.CronExpress)
                            .Build();
((CronTriggerImpl)trigger).MisfireInstruction = MisfireInstruction.CronTrigger.DoNothing;
IList<ICronTrigger> triggers = new List<ICronTrigger> { trigger };
await _scheduler.ScheduleJob(job, new ReadOnlyCollection<ICronTrigger>(triggers), true);
if (!_scheduler.IsStarted) await _scheduler.Start();

另外:下面一句如果存在相同名字的Job或Trigger参数replace设置为true可以更新作业的调度计划(如Cron重设)而不报错.
await _scheduler.ScheduleJob(job, new ReadOnlyCollection<ICronTrigger>(triggers), true);

await _scheduler.ScheduleJob(job, new ReadOnlyCollection<ICronTrigger>(triggers), true);
posted on 2020-05-17 14:53  Chr☆s  阅读(10727)  评论(0编辑  收藏  举报