quartz简单使用
using Quartz; using Quartz.Impl; using System; using System.Threading.Tasks; namespace quartzTest { class Program { static void Main(string[] args) { ///一般定时任务 Run(); //传参方式循环 Run3(); Console.ReadKey(); } public async static Task Run3() { // 1.创建scheduler的引用 ISchedulerFactory schedFact = new StdSchedulerFactory(); IScheduler sched = await schedFact.GetScheduler(); //2.启动 scheduler await sched.Start(); ///传参数方式 IJobDetail job = JobBuilder.Create<DumbJob>() .WithIdentity("myJob", "group1") // name "myJob", group "group1" .UsingJobData("jobSays", "Hello World!") .UsingJobData("myFloatValue", 3.141f) .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("myTrigger", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(3) .RepeatForever()) .Build(); await sched.ScheduleJob(job, trigger); } /// <summary> /// 任务调度的使用过程 /// </summary> /// <returns></returns> public async static Task Run() { // 1.创建scheduler的引用 ISchedulerFactory schedFact = new StdSchedulerFactory(); IScheduler sched = await schedFact.GetScheduler(); //2.启动 scheduler await sched.Start(); // 3.创建 job IJobDetail job = JobBuilder.Create<SimpleJob>() .WithIdentity("job1", "group1") .Build(); // 4.创建 trigger ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever()) .Build(); // 5.使用trigger规划执行任务job await sched.ScheduleJob(job, trigger); } } /// <summary> /// 任务 /// </summary> public class SimpleJob : IJob { public virtual Task Execute(IJobExecutionContext context) { return Console.Out.WriteLineAsync($"job工作了 在{DateTime.Now}"); } } public class DumbJob : IJob { public async Task Execute(IJobExecutionContext context) { JobKey key = context.JobDetail.Key; JobDataMap dataMap = context.JobDetail.JobDataMap; string jobSays = dataMap.GetString("jobSays"); float myFloatValue = dataMap.GetFloat("myFloatValue"); await Console.Error.WriteLineAsync("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue); } } }
至于定时,周期的方式都很简单,百度下都有了
posted on 2021-04-13 10:55 HOT SUMMER 阅读(158) 评论(0) 编辑 收藏 举报