public async Task Execute(IJobExecutionContext context) { JobDataMap map = context.JobDetail.JobDataMap; int executeCount = 0; if (map.ContainsKey(NumExecutions)) { executeCount = map.GetInt(NumExecutions); } executeCount++; map.Put(NumExecutions, executeCount); int delay = 5; if (map.ContainsKey(ExecutionDelay)) { delay = map.GetInt(ExecutionDelay); } ConsoleExtend.WriteLineDebug1($"【{DateTime.Now}】 开始执行 触发器【{context.Trigger.Key}】 作业【{context.JobDetail.Key}】 执行次数【{executeCount}】"); await Task.Delay(TimeSpan.FromSeconds(delay)); ConsoleExtend.WriteLineDebug2($"【{DateTime.Now}】 执行完成 触发器【{context.Trigger.Key}】 作业【{context.JobDetail.Key}】 执行次数【{executeCount}】"); }
public async Task Execute(IJobExecutionContext context) { var executeTime = DateTime.Now; ConsoleExtend.WriteLineDebug($"【{executeTime}】 开始执行作业 作业【{context.JobDetail.Key}】 触发器【{context.Trigger.Key}】"); try { for (int i = 0; i < 5; i++) { LockAction(() => ExecutedCount++); await Task.Delay(TimeSpan.FromSeconds(10)); if (context.CancellationToken.IsCancellationRequested) { ConsoleExtend.WriteLineDebug1($"【{DateTime.Now}】 作业被打断 作业【{context.JobDetail.Key}】 触发器【{context.Trigger.Key}】 触发时间【{executeTime}】"); // 也可以抛出JobExecutionException异常 LockAction(() => InterruptlCount++); } else { LockAction(() => SuccessfulCount++); } } } finally { ConsoleExtend.WriteLineDebug2($"【{DateTime.Now}】 作业执行完成 作业【{context.JobDetail.Key}】 触发器【{context.Trigger.Key}】 触发时间【{executeTime}】"); } }
public async Task RunAsync() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); #region 添加日历 AnnualCalendar holidays = new AnnualCalendar(); // 创建一个年度假日日历 // 元旦 1月1日 DateTime NewYearDay = new DateTime(DateTime.UtcNow.Year, 1, 1); holidays.SetDayExcluded(NewYearDay, true); // true 排除元旦执行 false 在元旦执行 // 劳动节 5月1日 DateTime LaborDay = new DateTime(DateTime.UtcNow.Year, 5, 1); holidays.SetDayExcluded(LaborDay, true); // true 排除元旦执行 false 在元旦执行 // 国庆节 10月1日 DateTime NationalDay = new DateTime(DateTime.UtcNow.Year, 10, 1); holidays.SetDayExcluded(NationalDay, true); // true 排除元旦执行 false 在元旦执行 await scheduler.AddCalendar("holidays", holidays, true, true); #endregion DateTimeOffset runDate = DateBuilder.DateOf(10, 0, 0, 1, 10); IJobDetail job = JobBuilder.Create <HelloJob2>() .WithIdentity("job1", "jobGroup1") .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "triggerGroup1") .StartAt(runDate) .WithSimpleSchedule(x => x.WithIntervalInHours(2).RepeatForever()) .ModifiedByCalendar("holidays") .Build(); DateTimeOffset df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineDebug1($"作业【{job.Key}】 触发器【{trigger.Key}】 将在【{df.LocalDateTime}】执行" + $",重复执行时间间隔【{trigger.RepeatInterval.Hours}】小时 重复【{trigger.RepeatCount}】次"); await scheduler.Start(); Console.WriteLine("等待30秒。。。"); await Task.Delay(TimeSpan.FromSeconds(30)); await scheduler.Shutdown(true); ConsoleExtend.WriteLineDebug2($"共执行了{(await scheduler.GetMetaData()).NumberOfJobsExecuted}个作业"); }
public async Task RunAsync() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(null, 10); IJobDetail job = JobBuilder.Create <InterruptJob>() .WithIdentity("job1", "jobGroup1") .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "triggerGroup1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(3).RepeatForever()) .Build(); DateTimeOffset df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"作业【{job.Key}】 触发器【{trigger.Key}】 执行时间【{df.LocalDateTime}】 " + $"执行时间间隔【{trigger.RepeatInterval.Seconds}】s 重复执行次数【{trigger.RepeatCount}】"); await scheduler.Start(); Console.WriteLine("每7秒中断一次调度"); for (int i = 0; i < 50; i++) { await Task.Delay(TimeSpan.FromSeconds(7)); try { await scheduler.Interrupt(job.Key); } catch (Exception ex) { ConsoleExtend.WriteLineError(ex.Message); } } await scheduler.Shutdown(true); SchedulerMetaData metaData = await scheduler.GetMetaData(); ConsoleExtend.WriteLineDebug2($"执行了 {metaData.NumberOfJobsExecuted} 次作业."); ConsoleExtend.WriteLineDebug2($"作业【{job.Key}】 中循环共执行了【{InterruptJob.ExecutedCount}】次" + $",其中成功【{InterruptJob.SuccessfulCount}】次,中断【{InterruptJob.InterruptlCount}】次"); }
public Task Execute(IJobExecutionContext context) { return(new TaskFactory().StartNew(() => { var jobContext = context as IJobExecutionContext; var jobKey = jobContext.JobDetail.Key; //JobDataMap dataMap = jobContext.JobDetail.JobDataMap; //使用作业中的参数 JobDataMap dataMap = jobContext.MergedJobDataMap; // 触发器中的惨覆盖作业中的参数 string dataBaseCon = dataMap.GetString("dataBaseCon"); string JsonParameter = dataMap.GetString("jsonParameter"); ConsoleExtend.WriteLineDebug2($"【{DateTime.Now.ToLongTimeString()}】 作业【{jobKey}】 " + $"描述【{jobContext.JobDetail.Description}】 数据库配置【{dataBaseCon}】 " + $"json参数【{JsonParameter}】"); })); }
public Task Execute(IJobExecutionContext context) { try { int denominator = context.JobDetail.JobDataMap.GetIntValue(denominatorKey); ConsoleExtend.WriteLineDebug1($"【{DateTime.Now}】 开始执行作业 作业【{context.JobDetail.Key}】 触发器【{context.Trigger.Key}】 参数【分母:{denominator}】"); var i = 10 / denominator; } catch (Exception e) { ConsoleExtend.WriteLineError($"{GetType().FullName}作业执行异常:{e.Message}"); context.JobDetail.JobDataMap.Put(denominatorKey, 1); JobExecutionException e2 = new JobExecutionException(); e2.RefireImmediately = true; // 设置作业马上重新开始 throw e2; } ConsoleExtend.WriteLineDebug2($"【{DateTime.Now}】 作业执行完成 作业【{context.JobDetail.Key}】 触发器【{context.Trigger.Key}】"); return(Task.FromResult(true)); }
public Task TriggerMisfired(ITrigger trigger, CancellationToken cancellationToken = default(CancellationToken)) { return(new TaskFactory().StartNew(() => { ConsoleExtend.WriteLineDebug2(@"监听器名称【{0}】 触发器【{1}=>{2}】触发丢失 触发器描述【{3}】 作业【{4}=》{5}】 触发器参数【{6}】 触发器日历名称【{7}】 最后执行时间【{8}】 下次执行时间【{9}】" , Name , trigger.Key.Group , trigger.Key.Name , trigger.Description , trigger.JobKey.Group , trigger.JobKey.Name , Newtonsoft.Json.JsonConvert.SerializeObject(trigger.JobDataMap) , trigger.CalendarName , trigger.FinalFireTimeUtc.HasValue ? trigger.FinalFireTimeUtc.Value.LocalDateTime.ToString() : String.Empty //最终执行时间 , trigger.GetFireTimeAfter(DateTimeOffset.Now).HasValue ? trigger.GetFireTimeAfter(DateTimeOffset.Now).Value.LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss") : String.Empty //下次执行时间 ); })); }
public Task Execute(IJobExecutionContext context) { ConsoleExtend.WriteLineDebug1($"【{DateTime.Now}】 开始执行作业 作业【{context.JobDetail.Key}】 触发器【{context.Trigger.Key}】"); int denominator = 0; try { var i = 10 / denominator; } catch (Exception e) { ConsoleExtend.WriteLineError($"{GetType().FullName}作业执行异常:{e.Message}"); denominator = 1; JobExecutionException e2 = new JobExecutionException(); e2.UnscheduleAllTriggers = true; // Quartz 会自动取消与此工作相关的所有触发器,这样它就不会再次运行了。 throw e2; } ConsoleExtend.WriteLineDebug2($"【{DateTime.Now}】 作业执行完成 作业【{context.JobDetail.Key}】 触发器【{context.Trigger.Key}】"); return(Task.FromResult(true)); }