Quartz.net 一次性执行多个Job服务 电脑版发表于:2023/11/10 17:35 代码如下:6 ``` /// <summary> /// 定时服务入口 /// </summary> /// <param name="args"></param> static void Main(string[] args) { StdSchedulerFactory schedulerFactory = new StdSchedulerFactory(); Program program = new Program(); //program.DoInitUserVisitCountDay(schedulerFactory); //program.DoArticleTitleToCacheJob(schedulerFactory); program.DoJobs(schedulerFactory); Console.ReadLine(); } /// <summary> /// 一次性执行多个job /// 这个写起来还有点麻烦... /// </summary> /// <param name="schedulerFactory"></param> public void DoJobs(StdSchedulerFactory schedulerFactory) { Console.WriteLine("开始新的任务了,定义在每天0点2分执行................." + DateTime.Now); IScheduler _scheduler = schedulerFactory.GetScheduler().Result; var triggers = new ReadOnlyCollection<ITrigger>( new List<ITrigger>() { TriggerBuilder.Create() .WithCronSchedule("0/2 * * * * ?") .Build() }); var trigger2 = new ReadOnlyCollection<ITrigger>( new List<ITrigger>() { TriggerBuilder.Create() .WithCronSchedule("0/2 * * * * ?") .Build() }); // 创建作业实例 var initUserVisitCountJob = JobBuilder.Create<InitUserVisitCountDay>() .WithIdentity("InitUserVisitCountDay", "Group") .Build(); // 创建作业实例 var articleTitleToCacheJob = JobBuilder.Create<ArticleTitleToCacheJob>() .WithIdentity("ArticleTitleToCacheJob", "Group") .Build(); // 注意多个任务的triggers不能使用一个,不然经过测试触发不了 var jobAndTriggerMapping = new Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>>(); jobAndTriggerMapping.Add(initUserVisitCountJob, triggers); jobAndTriggerMapping.Add(articleTitleToCacheJob, trigger2); var readOnlyjobAndTriggerMapping = new ReadOnlyDictionary<IJobDetail, IReadOnlyCollection<ITrigger>>(jobAndTriggerMapping); _scheduler.ScheduleJobs(readOnlyjobAndTriggerMapping, true); //开启调度器 _scheduler.Start(); } ```