通过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);