public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken)) { return(new TaskFactory().StartNew(() => { ConsoleExtend.WriteLineRed(@"监听器名称【{0}】作业【{1}=》{2}】作业取消 作业描述【{3}】 作业参数【{4}】 作业类型【{5}】 作业实体【{6}】 触发器【{7}=》{8}】 恢复触发器【{9}=》{10}】 触发器参数【{11}】 合并参数【{12}】 调度触发时间【{18}】 作业触发时间【{13}】 触发实体ID【{14}】 作业运行时间【{15}】 下次执行时间【{16}】 之前触发时间【{17}】" , Name , context.JobDetail.Key.Group , context.JobDetail.Key.Name , context.JobDetail.Description , Newtonsoft.Json.JsonConvert.SerializeObject(context.JobDetail.JobDataMap) , context.JobDetail.JobType.FullName , context.JobInstance.GetType().FullName , context.Trigger.Key.Group , context.Trigger.Key.Name , context.Recovering ? context.RecoveringTriggerKey.Group : String.Empty , context.Recovering ? context.RecoveringTriggerKey.Name : String.Empty , Newtonsoft.Json.JsonConvert.SerializeObject(context.Trigger.JobDataMap) , Newtonsoft.Json.JsonConvert.SerializeObject(context.MergedJobDataMap) , context.FireTimeUtc.LocalDateTime , context.FireInstanceId , /*new DateTime(context.JobRunTime.Ticks)*/ TimeSpan.FromTicks(context.JobRunTime.Ticks).Seconds + "s" , context.NextFireTimeUtc.HasValue?context.NextFireTimeUtc.Value.LocalDateTime.ToString():String.Empty , context.PreviousFireTimeUtc.HasValue? context.PreviousFireTimeUtc.Value.LocalDateTime.ToString():String.Empty , context.ScheduledFireTimeUtc.HasValue? context.ScheduledFireTimeUtc.Value.LocalDateTime.ToString():String.Empty); })); }
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 async Task RunAsync() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); IJobDetail job = JobBuilder.Create <SimpleJob1>() .WithIdentity("job1", "jobGroup1") .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "triggerGroup1") .StartNow() .WithSimpleSchedule(x => x.WithIntervalInSeconds(20).RepeatForever()) .Build(); scheduler.ListenerManager.AddJobListener(new JobListener(), KeyMatcher <JobKey> .KeyEquals(job.Key)); DateTimeOffset df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"作业【{job.Key}】 触发器【{trigger.Key}】 下次执行时间【{df.LocalDateTime}】 " + $"执行时间间隔【{trigger.RepeatInterval.Seconds}】s 重复次数【{trigger.RepeatCount}】"); //job = JobBuilder.Create<SimpleJob2>() // .WithIdentity("job2","jobGroup") // .Build(); //trigger = (ISimpleTrigger)TriggerBuilder.Create() // .WithIdentity("trigger2","triggerGroup1") // .StartNow() // .WithSimpleSchedule(x=>x.WithIntervalInSeconds(15).RepeatForever()) // .Build(); //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("等待1分钟。。。"); await Task.Delay(TimeSpan.FromMinutes(1)); await scheduler.Shutdown(); }
public async Task RunAsync() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); DateTimeOffset startTime = DateTimeOffset.Now.AddSeconds(10); #region 错误作业1 带参数 设置作业失败后马上重新开始 IJobDetail job = JobBuilder.Create <BadJob1>() .WithIdentity("job1", "jobGroup1") .UsingJobData(BadJob1.denominatorKey, 0) .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "trrigerGroup1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()) .Build(); DateTimeOffset df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"作业【{job.Key}】 触发器【{trigger.Key}】 执行时间【{df.LocalDateTime}】 " + $"执行时间间隔【{trigger.RepeatInterval.Seconds}】 重复执行次数【{trigger.RepeatCount}】"); #endregion #region 错误作业2 不带参数 设置作业失败后停止相关触发器执行 job = JobBuilder.Create <BadJob2>() .WithIdentity("job2", "jobGroup2") .Build(); trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "triggerGroup1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()) .Build(); df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"作业【{job.Key}】 触发器【{trigger.Key}】 执行时间【{df.LocalDateTime}】 " + $"执行时间间隔【{trigger.RepeatInterval.Seconds}】 重复执行次数【{trigger.RepeatCount}】"); #endregion #region 给错误作业2添加新的触发器 trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger3", "triggerGroup1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()) .ForJob("job2", "jobGroup2") .Build(); df = await scheduler.ScheduleJob(trigger); ConsoleExtend.WriteLineRed($"作业【{trigger.JobKey}】 触发器【{trigger.Key}】 执行时间【{df.LocalDateTime}】 " + $"执行时间间隔【{trigger.RepeatInterval.Seconds}】 重复执行次数【{trigger.RepeatCount}】"); #endregion await scheduler.Start(); await Task.Delay(TimeSpan.FromMinutes(1)); await scheduler.Shutdown(); }
public Task <bool> VetoJobExecution(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken)) { ConsoleExtend.WriteLineRed($"插件【{Name}】 触发器【{trigger.Key}】 取消作业【{context.JobDetail.Key}】执行:false 触发时间【{SystemTime.UtcNow()}】 下次触发时间【{trigger.GetNextFireTimeUtc()}】"); return(Task.FromResult(false)); }
public Task TriggerMisfired(ITrigger trigger, CancellationToken cancellationToken = default(CancellationToken)) { ConsoleExtend.WriteLineRed($"插件【{Name}】 触发器【{trigger.Key}】 触发失败 触发时间【{SystemTime.UtcNow()}】 下次触发时间【{trigger.GetNextFireTimeUtc()}】"); return(Task.FromResult(true)); }
public Task TriggerFired(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken)) { ConsoleExtend.WriteLineRed($"插件【{Name}】 触发器【{trigger.Key}】 触发作业【{context.JobDetail.Key}】 触发时间【{SystemTime.UtcNow()}】"); return(Task.FromResult(true)); }
public Task TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode, CancellationToken cancellationToken = default(CancellationToken)) { ConsoleExtend.WriteLineRed($"插件【{Name}】 触发器【{trigger.Key}】 触发作业【{context.JobDetail.Key}】完成 触发时间【{SystemTime.UtcNow()}】 下次触发时间【{trigger.GetNextFireTimeUtc()}】"); return(Task.FromResult(true)); }
public async Task RunAsync() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); DateTimeOffset startTime = DateTimeOffset.Now.AddSeconds(3); #region 作业1 每3秒执行一次 IJobDetail job = JobBuilder.Create <LogTimeJob>() .WithIdentity("job1", "jobGroup1") .SetJobData(new JobDataMap { { LogTimeJob.NumExecutions, 0 }, { LogTimeJob.ExecutionDelay, 10 } }) .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "triggerGroup1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(3)).RepeatForever()) .Build(); DateTimeOffset df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"触发器【{trigger.Key}】 作业【{job.Key}】 下次执行时间【{df.LocalDateTime.ToString("r")}】" + $" 执行时间间隔【{trigger.RepeatInterval.Seconds}】 重复次数【{trigger.RepeatCount}】"); #endregion #region 作业2 每3秒执行一次 job = JobBuilder.Create <LogTimeJob>() .WithIdentity("job2", "jobGroup1") .SetJobData(new JobDataMap { { LogTimeJob.NumExecutions, 0 }, { LogTimeJob.ExecutionDelay, 10 } }) .Build(); trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "triggerGroup1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(3)).RepeatForever().WithMisfireHandlingInstructionNowWithExistingCount()) .Build(); df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"触发器【{trigger.Key}】 作业【{job.Key}】 下次执行时间【{df.LocalDateTime.ToString("r")}】" + $" 执行时间间隔【{trigger.RepeatInterval.Seconds}】 重复次数【{trigger.RepeatCount}】"); #endregion #region 给作业1 添加一个新的触发器 trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger3", "triggerGroup1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(3)).RepeatForever().WithMisfireHandlingInstructionNowWithExistingCount()) .ForJob("job1", "jobGroup1") .Build(); df = await scheduler.ScheduleJob(trigger); ConsoleExtend.WriteLineRed($"触发器【{trigger.Key}】 作业【{trigger.JobKey}】 下次执行时间【{df.LocalDateTime.ToString("r")}】" + $" 执行时间间隔【{trigger.RepeatInterval.Seconds}】 重复次数【{trigger.RepeatCount}】"); #endregion await scheduler.Start(); await Task.Delay(TimeSpan.FromMinutes(2)); await scheduler.Shutdown(); }
public async Task RunAsync() { // 获取默认调度器 IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); // 获取一个15秒后执行的时间 DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(null, 15); #region 作业1 15秒后执行,只执行一次 // 创建带参数作业 IJobDetail job = JobBuilder.Create <ParameterJob>() .WithIdentity("job1", "jobGroup1") .WithDescription("带参数作业") .SetJobData(new JobDataMap() { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "json参数字符串" } }) .Build(); // 创建一个15秒后执行的触发器,只执行一次 ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "triggerGroup1") .WithDescription("15秒后执行,只执行一次") .StartAt(startTime) .UsingJobData("jsonParameter", "trigger json参数字符串") .Build(); // 调度作业执行 DateTimeOffset?ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 重复【{trigger.RepeatCount}】次,每【{trigger.RepeatInterval.TotalSeconds}】秒重复一次。"); #endregion #region 作业2 15秒后执行,然后每过10秒执行一次,共执行5次 // 创建带参数作业 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job2", "jobGroup1") .WithDescription("带参数作业") .SetJobData(new JobDataMap() { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "json参数字符串" } }) .Build(); // 创建一个15秒后执行的触发器,每过10秒触发一次,共触发5次 trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "triggerGroup1") .WithDescription("15秒后执行,然后每过10秒触发一次,共触发5次") .StartAt(startTime) .UsingJobData("jsonParameter", "trigger json参数字符串") .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(5)) .Build(); // 调度作业执行 ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineYellow($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 重复【{trigger.RepeatCount}】次,每【{trigger.RepeatInterval.TotalSeconds}】秒重复一次。"); //logProvider.GetLogger(typeof(SimpleTriggerTest).FullName).Invoke(LogLevel.Info // , () => $"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 重复【{trigger.RepeatCount}】次,每【{trigger.RepeatInterval.TotalSeconds}】秒重复一次。"); #endregion #region 相同的作业2可以被其他触发器执行,每5秒执行一次,共执行10次 trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger3_2", "triggerGroup1") .WithDescription("相同的作业2可以被其他触发器执行,每5秒执行一次,共执行10次") .ForJob("job2", "jobGroup1") // 根据job名称和组绑定 //.ForJob(new JobKey("job2", "jobGroup1")) // 根据jobKey绑定 //.ForJob(job) // 根据job绑定 .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithRepeatCount(10)) .Build(); ft = await scheduler.ScheduleJob(trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 重复【{trigger.RepeatCount}】次,每【{trigger.RepeatInterval.TotalSeconds}】秒重复一次。"); #endregion #region 作业3 将无限执行,每20秒执行一次 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job3", "jobGroup1") .WithDescription("带参数作业") .SetJobData(new JobDataMap() { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "json参数字符串" } }) .Build(); trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger4", "triggerGroup1") .WithDescription("将无限执行,每20秒执行一次") .WithSimpleSchedule(x => x.WithIntervalInSeconds(20).RepeatForever()) .Build(); ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineDarkMagenta($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 重复【{trigger.RepeatCount}】次,每【{trigger.RepeatInterval.TotalSeconds}】秒重复一次。"); #endregion await scheduler.Start(); #region 作业4 作业也可以在调度开始后添加进调度 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job4", "jobGroup1") .WithDescription("带参数作业") .SetJobData(new JobDataMap() { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "json参数字符串" } }) .Build(); trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger5", "triggerGroup1") .WithDescription("作业也可以在调度开始后添加进调度 将无限执行,每20秒执行一次") .WithSimpleSchedule(x => x.WithIntervalInSeconds(20).RepeatForever()) .Build(); ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineYellow($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 重复【{trigger.RepeatCount}】次,每【{trigger.RepeatInterval.TotalSeconds}】秒重复一次。"); #endregion #region 作业也可以直接出发(而不是通过触发器) job = JobBuilder.Create <ParameterJob>() .WithIdentity("job4", "jobGroup1") .WithDescription("带参数作业") .SetJobData(new JobDataMap() { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "json参数字符串" } }) .StoreDurably() //设置没有触发器纸箱改作业,调度不存储改作业 .Build(); await scheduler.AddJob(job, true); await scheduler.TriggerJob(new JobKey("job4", "jobGroup1") , new JobDataMap { { "jsonParameter", "手动出发作业传入 json参数字符串" } });// 手动出发作业 ConsoleExtend.WriteLineRed($"{job.Key} 手动触发作业。"); #endregion #region 手动触发之前作业 await scheduler.TriggerJob(new JobKey("job3", "jobGroup1") , new JobDataMap { { "jsonParameter", "手动出发作业传入 json参数字符串" } });// 手动出发作业 ConsoleExtend.WriteLineRed("手动出发之前作业job3"); #endregion #region 重新安排之前作业 trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger3_2", "triggerGroup1") .WithDescription("重新安排触发器3_2的作业,每20秒执行一次,共执行5次") .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(20).WithRepeatCount(5)) .Build(); ft = await scheduler.RescheduleJob(trigger.Key, trigger); ConsoleExtend.WriteLineRed($" 重新安排触发器3_2 job2 将在以下时间运行【{ft.Value.LocalDateTime}】 重复【{trigger.RepeatCount}】次,每【{trigger.RepeatInterval.TotalSeconds}】秒重复一次。"); #endregion Console.WriteLine("等待3分钟"); await Task.Delay(TimeSpan.FromMinutes(3)).ConfigureAwait(false); // 关闭调度 await scheduler.Shutdown(false); }
public async Task RunAsync() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); #region 作业1 每20秒执行一次 var job = JobBuilder.Create <ParameterJob>() .WithIdentity("job1", "jobGroup1") .WithDescription("每20秒执行一次") .SetJobData(new JobDataMap { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "Jons 参数" }, }) .Build(); ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "triggerGroup") .WithCronSchedule("0/20 * * * * ?") .Build(); // 调度作业执行 DateTimeOffset?ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 基于重复表达式【{trigger.CronExpressionString}】。"); #endregion #region 每2分钟的15秒执行一次 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job2", "jobGroup1") .WithDescription("每2分钟的15秒执行一次") .SetJobData(new JobDataMap { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "Jons 参数" }, }) .Build(); trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "triggerGroup") .WithCronSchedule("15 0/2 * * * ?") .Build(); // 调度作业执行 ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 基于重复表达式【{trigger.CronExpressionString}】。"); #endregion #region 在每天的 12点到18点 之间,每1分钟的20秒执行一次 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job3", "jobGroup1") .WithDescription("在每天的12点到18点之间,每1分钟的20秒执行一次") .SetJobData(new JobDataMap { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "Jons 参数" }, }) .Build(); trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger3", "triggerGroup") .WithCronSchedule("20 0/1 12-18 * * ?") .Build(); // 调度作业执行 ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 基于重复表达式【{trigger.CronExpressionString}】。"); #endregion #region 在每月的1号29号的下午4点执行 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job4", "jobGroup1") .WithDescription("在每月的29号1号的下午4点执行") .SetJobData(new JobDataMap { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "Jons 参数" }, }) .Build(); trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger4", "triggerGroup") .WithCronSchedule("0 0 16 1,29 * ?", x => x.InTimeZone(TimeZoneInfo.Local)) .Build(); // 调度作业执行 ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 基于重复表达式【{trigger.CronExpressionString}】。"); #endregion #region 在每个星期一到星期六,每30秒执行一次 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job5", "jobGroup1") .WithDescription("在每个星期一到星期六,每30秒执行一次") .SetJobData(new JobDataMap { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "Jons 参数" }, }) .Build(); trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger5", "triggerGroup") .WithCronSchedule("0/30 * * ? * MON,SAT", x => x.InTimeZone(TimeZoneInfo.Local)) .Build(); // 调度作业执行 ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 基于重复表达式【{trigger.CronExpressionString}】。"); #endregion #region 在每个星期天,每1小时执行一次 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job6", "jobGroup1") .WithDescription("在每个星期天,每1小时执行一次") .SetJobData(new JobDataMap { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "Jons 参数" }, }) .Build(); trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger6", "triggerGroup") .WithCronSchedule("0 0 1 ? * SUN") .Build(); // 调度作业执行 ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 基于重复表达式【{trigger.CronExpressionString}】。"); #endregion #region 在每天的10点42份执行 job = JobBuilder.Create <ParameterJob>() .WithIdentity("job7", "jobGroup1") .WithDescription("在每个星期天,每1小时执行一次") .SetJobData(new JobDataMap { { "dataBaseCon", "数据库配置字符串" }, { "jsonParameter", "Jons 参数" }, }) .Build(); trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger7", "triggerGroup") .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(10, 42).InTimeZone(TimeZoneInfo.Local)) //.WithCronSchedule("0 42 10 * * ?", x => x.InTimeZone(TimeZoneInfo.Local)) .Build(); // 调度作业执行 ft = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"{job.Key} 将在以下时间运行【{ft.Value.LocalDateTime}】 基于重复表达式【{trigger.CronExpressionString}】。"); #endregion await scheduler.Start(); Console.WriteLine("等待3分钟"); await Task.Delay(TimeSpan.FromMinutes(3)).ConfigureAwait(false); // 关闭调度 await scheduler.Shutdown(false); }
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken)) { ConsoleExtend.WriteLineDarkMagenta(@"监听器名称【{0}】作业【{1}=》{2}】作业执行完成 作业描述【{3}】 作业参数【{4}】 作业类型【{5}】 作业实体【{6}】 触发器【{7}=》{8}】 恢复触发器【{9}=》{10}】 触发器参数【{11}】 合并参数【{12}】 调度触发时间【{18}】 作业触发时间【{13}】 触发实体ID【{14}】 作业运行时间【{15}】 下次执行时间【{16}】 之前触发时间【{17}】" , Name , context.JobDetail.Key.Group , context.JobDetail.Key.Name , context.JobDetail.Description , Newtonsoft.Json.JsonConvert.SerializeObject(context.JobDetail.JobDataMap) , context.JobDetail.JobType.FullName , context.JobInstance.GetType().FullName , context.Trigger.Key.Group , context.Trigger.Key.Name , context.Recovering ? context.RecoveringTriggerKey.Group : String.Empty , context.Recovering ? context.RecoveringTriggerKey.Name : String.Empty , Newtonsoft.Json.JsonConvert.SerializeObject(context.Trigger.JobDataMap) , Newtonsoft.Json.JsonConvert.SerializeObject(context.MergedJobDataMap) , context.FireTimeUtc.LocalDateTime , context.FireInstanceId , /*new DateTime(context.JobRunTime.Ticks)*/ TimeSpan.FromTicks(context.JobRunTime.Ticks).Seconds + "s" , context.NextFireTimeUtc.HasValue ? context.NextFireTimeUtc.Value.LocalDateTime.ToString() : String.Empty , context.PreviousFireTimeUtc.HasValue ? context.PreviousFireTimeUtc.Value.LocalDateTime.ToString() : String.Empty , context.ScheduledFireTimeUtc.HasValue ? context.ScheduledFireTimeUtc.Value.LocalDateTime.ToString() : String.Empty); IScheduler scheduler = context.Scheduler; var jobKeys = await scheduler.GetJobKeys(GroupMatcher <JobKey> .GroupEquals("jobGroup2")); IJobDetail job = null; ISimpleTrigger trigger = null; if (jobKeys.Any(p => p.Group.Equals("jobGroup2") && p.Name.Equals("job2"))) { trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "triggerGroup2") .StartNow() .ForJob("job2", "jobGroup2") .Build(); DateTimeOffset df = await scheduler.ScheduleJob(trigger); ConsoleExtend.WriteLineDarkMagenta($"监听器【{Name}】调用作业【{job.Key}】"); ConsoleExtend.WriteLineRed($"作业【{trigger.JobKey}】 触发器【{trigger.Key}】 执行时间【{df.LocalDateTime}】"); //await scheduler.Shutdown(true); } else { job = JobBuilder.Create <SimpleJob2>() .WithIdentity("job2", "jobGroup2") .Build(); trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "triggerGroup2") .StartNow() .Build(); DateTimeOffset df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineDarkMagenta($"监听器【{Name}】调用作业【{job.Key}】"); ConsoleExtend.WriteLineRed($"作业【{job.Key}】 触发器【{trigger.Key}】 执行时间【{df.LocalDateTime}】"); //await scheduler.Shutdown(true); } }
public async Task RunAsync() { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); DateTimeOffset startTime = DateTimeOffset.Now.AddSeconds(10); #region 作业1 每10秒执行一次 IJobDetail job = JobBuilder.Create <ColorJob>() .WithIdentity("job1", "jobGroup1") .WithDescription("作业1 每10秒执行一次") .SetJobData(new JobDataMap { { ColorJob.ColorKey, "Red" }, { ColorJob.ColorCountKey, 0 } }) .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1", "triggerGroup1") .UsingJobData(ColorJob.ColorKey, "Blue") .UsingJobData(ColorJob.ColorCountKey, 1) .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()) .Build(); DateTimeOffset df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"作业【{job.Key}】 描述【{job.Description}】 执行时间【{df.LocalDateTime}】" + $" 执行时间间隔【{trigger.RepeatInterval.Seconds}】s 执行次数【{trigger.RepeatCount}】"); #endregion #region 作业2 每15秒执行一次 job = JobBuilder.Create <ColorJob>() .WithIdentity("job2", "jobGroup1") .WithDescription("作业2 每15秒执行一次") .SetJobData(new JobDataMap { { ColorJob.ColorKey, "Yellow" }, { ColorJob.ColorCountKey, 10 } }) .Build(); trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "triggerGroup1") .UsingJobData(ColorJob.ColorKey, "White") .UsingJobData(ColorJob.ColorCountKey, 11) .StartAt(startTime.AddSeconds(5)) .WithSimpleSchedule(x => x.WithIntervalInSeconds(15).RepeatForever()) .Build(); df = await scheduler.ScheduleJob(job, trigger); ConsoleExtend.WriteLineRed($"作业【{job.Key}】 描述【{job.Description}】 执行时间【{df.LocalDateTime}】" + $" 执行时间间隔【{trigger.RepeatInterval.Seconds}】s 执行次数【{trigger.RepeatCount}】"); #endregion #region 作业1 添加触发器,每25秒触发一次 trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger3", "triggerGroup1") .UsingJobData(ColorJob.ColorKey, "Green") .UsingJobData(ColorJob.ColorCountKey, 21) .StartAt(startTime.AddSeconds(15)) .ForJob("job1", "jobGroup1") .WithSimpleSchedule(x => x.WithIntervalInSeconds(25).RepeatForever()) .Build(); df = await scheduler.ScheduleJob(trigger); ConsoleExtend.WriteLineRed($"作业【{trigger.JobKey}】 执行时间【{df.LocalDateTime}】" + $" 执行时间间隔【{trigger.RepeatInterval.Seconds}】s 执行次数【{trigger.RepeatCount}】"); #endregion await scheduler.Start(); Console.WriteLine("等待3分钟..."); await Task.Delay(TimeSpan.FromMinutes(3)); await scheduler.Shutdown(); }