於忠林 发表于 2023-6-19 16:36:42

Quartz.net的最佳实践

\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"hdHb-1687161964776","name":"code-line","data":{},"nodes":[{"type":"text","id":"tKsr-1687161964775","leaves":[{"text":"                {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"FcL4-1687161964778","name":"code-line","data":{},"nodes":[{"type":"text","id":"Jrlf-1687161964777","leaves":[{"text":"                    Console.WriteLine(\"--------1\");","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"gxrb-1687162997648","name":"code-line","data":{},"nodes":[{"type":"text","id":"3QAv-1687162997647","leaves":[{"text":"                    ","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"kGwv-1687163014786","name":"code-line","data":{},"nodes":[{"type":"text","id":"lqQD-1687163014785","leaves":[{"text":"                    //调度作业的唯一id的唯一标识,用于集群搭建cluster","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ofgF-1687162999468","name":"code-line","data":{},"nodes":[{"type":"text","id":"w4gN-1687162999467","leaves":[{"text":"                    q.SchedulerId = \"SchedulerId_01\";","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"SynN-1687162484183","name":"code-line","data":{},"nodes":[{"type":"text","id":"cBbl-1687162484181","leaves":[{"text":"                    //配置Quartz服务\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"XKgk-1687161964780","name":"code-line","data":{},"nodes":[{"type":"text","id":"A3HP-1687161964779","leaves":[{"text":"                    services.AddQuartz(q =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"9ejq-1687161964782","name":"code-line","data":{},"nodes":[{"type":"text","id":"9JE6-1687161964781","leaves":[{"text":"                    {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"dFR8-1687161964784","name":"code-line","data":{},"nodes":[{"type":"text","id":"fxny-1687161964783","leaves":[{"text":"                        Console.WriteLine(\"--------2\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"AIb6-1687161964786","name":"code-line","data":{},"nodes":[{"type":"text","id":"B9tu-1687161964785","leaves":[{"text":"                        //依赖注入,ISchedulerFactory,Ijob等等\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"C0sE-1687161964788","name":"code-line","data":{},"nodes":[{"type":"text","id":"RECk-1687161964787","leaves":[{"text":"                        q.UseMicrosoftDependencyInjectionJobFactory();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"6UYy-1687161964790","name":"code-line","data":{},"nodes":[{"type":"text","id":"MYxn-1687161964789","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"96QN-1687161964792","name":"code-line","data":{},"nodes":[{"type":"text","id":"8Ypx-1687161964791","leaves":[{"text":"                        //方法一和方法二使用不同方法的写法,本质基本是一样的\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"WOcS-1687161964794","name":"code-line","data":{},"nodes":[{"type":"text","id":"swLO-1687161964793","leaves":[{"text":"                        //方法一\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"5PDa-1687161964796","name":"code-line","data":{},"nodes":[{"type":"text","id":"5lSM-1687161964795","leaves":[{"text":"                        q.ScheduleJob(\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"oZtu-1687161964798","name":"code-line","data":{},"nodes":[{"type":"text","id":"P973-1687161964797","leaves":[{"text":"                            trigger =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"tZRE-1687161964800","name":"code-line","data":{},"nodes":[{"type":"text","id":"MXnI-1687161964799","leaves":[{"text":"                            {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"C4Vn-1687161964802","name":"code-line","data":{},"nodes":[{"type":"text","id":"cYFh-1687161964801","leaves":[{"text":"                                Console.WriteLine(\"--------33\");","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"wv1i-1687162568244","name":"code-line","data":{},"nodes":[{"type":"text","id":"NDAs-1687162568242","leaves":[{"text":"                                //WithIdentity 绑定触发器或者job的唯一属性和组","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"syeT-1687162501619","name":"code-line","data":{},"nodes":[{"type":"text","id":"2hfl-1687162501617","leaves":[{"text":"                                //TriggerKey,JobKey 都是代表唯一个属性和组\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"IEw5-1687161964804","name":"code-line","data":{},"nodes":[{"type":"text","id":"sGcJ-1687161964803","leaves":[{"text":"                                trigger.WithIdentity(new TriggerKey(\"trigger1\", \"triggergroup1\"))\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"pBEI-1687161964806","name":"code-line","data":{},"nodes":[{"type":"text","id":"nZr0-1687161964805","leaves":[{"text":"                                       .WithSimpleSchedule(x => x.WithIntervalInSeconds(5))\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ftXf-1687161964808","name":"code-line","data":{},"nodes":[{"type":"text","id":"y0YV-1687161964807","leaves":[{"text":"                                       // .StartAt(DateBuilder.EvenSecondDate(DateTimeOffset.UtcNow.AddSeconds(5)))\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"bLPo-1687161964810","name":"code-line","data":{},"nodes":[{"type":"text","id":"Aruk-1687161964809","leaves":[{"text":"                                       //  .WithDailyTimeIntervalSchedule(x => x.WithInterval(10, IntervalUnit.Second))\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Neb7-1687161964812","name":"code-line","data":{},"nodes":[{"type":"text","id":"XntO-1687161964811","leaves":[{"text":"                                       .WithDescription(\"init 描述\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ojB3-1687161964814","name":"code-line","data":{},"nodes":[{"type":"text","id":"T85Q-1687161964813","leaves":[{"text":"                            },\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"UCPT-1687161964816","name":"code-line","data":{},"nodes":[{"type":"text","id":"FqbD-1687161964815","leaves":[{"text":"                            jobConfigure =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"nk0d-1687161964818","name":"code-line","data":{},"nodes":[{"type":"text","id":"7dFc-1687161964817","leaves":[{"text":"                            {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"uZaQ-1687161964820","name":"code-line","data":{},"nodes":[{"type":"text","id":"LGaI-1687161964819","leaves":[{"text":"                                Console.WriteLine(\"--------44\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"AmzA-1687161964822","name":"code-line","data":{},"nodes":[{"type":"text","id":"vBbz-1687161964821","leaves":[{"text":"                                jobConfigure.WithIdentity(new JobKey(\"Init1\", \"jobgroup1\"));\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"FSdq-1687161964824","name":"code-line","data":{},"nodes":[{"type":"text","id":"GGYM-1687161964823","leaves":[{"text":"                            }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"3RmJ-1687161964826","name":"code-line","data":{},"nodes":[{"type":"text","id":"uZ5O-1687161964825","leaves":[{"text":"                        );\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"I3H9-1687161964828","name":"code-line","data":{},"nodes":[{"type":"text","id":"ZcKV-1687161964827","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"aT3T-1687161964830","name":"code-line","data":{},"nodes":[{"type":"text","id":"aCr9-1687161964829","leaves":[{"text":"                        //方法二\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"eDqL-1687161964832","name":"code-line","data":{},"nodes":[{"type":"text","id":"4HHN-1687161964831","leaves":[{"text":"                        //q.AddJob(opts =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"m3DR-1687161964834","name":"code-line","data":{},"nodes":[{"type":"text","id":"Eijx-1687161964833","leaves":[{"text":"                        //{\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"SjJq-1687161964836","name":"code-line","data":{},"nodes":[{"type":"text","id":"890D-1687161964835","leaves":[{"text":"                        //    Console.WriteLine(\"--------3\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"5wo7-1687161964838","name":"code-line","data":{},"nodes":[{"type":"text","id":"6LjA-1687161964837","leaves":[{"text":"                        //    opts.WithIdentity(new JobKey(\"Init1\", \"jobgroup1\"));\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"BRoB-1687161964840","name":"code-line","data":{},"nodes":[{"type":"text","id":"8tYb-1687161964839","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"0kWX-1687161964842","name":"code-line","data":{},"nodes":[{"type":"text","id":"DMIt-1687161964841","leaves":[{"text":"                        //});\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"jmqT-1687161964844","name":"code-line","data":{},"nodes":[{"type":"text","id":"PuiS-1687161964843","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"3HFe-1687161964846","name":"code-line","data":{},"nodes":[{"type":"text","id":"Wsyf-1687161964845","leaves":[{"text":"                        //q.AddTrigger(opts =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"M0OP-1687161964848","name":"code-line","data":{},"nodes":[{"type":"text","id":"dekC-1687161964847","leaves":[{"text":"                        //{\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"X0Re-1687161964850","name":"code-line","data":{},"nodes":[{"type":"text","id":"p6NF-1687161964849","leaves":[{"text":"                        //    Console.WriteLine(\"--------4\");","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"6NXV-1687162597842","name":"code-line","data":{},"nodes":[{"type":"text","id":"RZrj-1687162597841","leaves":[{"text":"                        //    //将job添加至触发器中\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ly6H-1687161964852","name":"code-line","data":{},"nodes":[{"type":"text","id":"92D6-1687161964851","leaves":[{"text":"                        //    opts.ForJob(new JobKey(\"Init1\", \"jobgroup1\"))\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"amt4-1687161964854","name":"code-line","data":{},"nodes":[{"type":"text","id":"YXvc-1687161964853","leaves":[{"text":"                        //    .WithIdentity(\"trigger1\", \"triggergroup1\")\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"YNWw-1687161964856","name":"code-line","data":{},"nodes":[{"type":"text","id":"LYLw-1687161964855","leaves":[{"text":"                        //    .WithSimpleSchedule(x =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"5aXw-1687161964858","name":"code-line","data":{},"nodes":[{"type":"text","id":"QxWf-1687161964857","leaves":[{"text":"                        //    {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"iL4m-1687161964860","name":"code-line","data":{},"nodes":[{"type":"text","id":"8uq1-1687161964859","leaves":[{"text":"                        //        Console.WriteLine(\"--------6\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"lWeG-1687161964862","name":"code-line","data":{},"nodes":[{"type":"text","id":"rl4u-1687161964861","leaves":[{"text":"                        //        x.WithIntervalInSeconds(5);\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"QiH0-1687161964864","name":"code-line","data":{},"nodes":[{"type":"text","id":"gbTR-1687161964863","leaves":[{"text":"                        //        //.RepeatForever();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"imez-1687161964866","name":"code-line","data":{},"nodes":[{"type":"text","id":"2t3L-1687161964865","leaves":[{"text":"                        //        //.WithRepeatCount(5);\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"DtjY-1687161964868","name":"code-line","data":{},"nodes":[{"type":"text","id":"0j3n-1687161964867","leaves":[{"text":"                        //    });\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"buSP-1687161964870","name":"code-line","data":{},"nodes":[{"type":"text","id":"U5RY-1687161964869","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"0RGU-1687161964872","name":"code-line","data":{},"nodes":[{"type":"text","id":"tAZ1-1687161964871","leaves":[{"text":"                        //});\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"gH6b-1687161964874","name":"code-line","data":{},"nodes":[{"type":"text","id":"7j14-1687161964873","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Lfn1-1687161964876","name":"code-line","data":{},"nodes":[{"type":"text","id":"iX8j-1687161964875","leaves":[{"text":"                    });\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"29WM-1687161964878","name":"code-line","data":{},"nodes":[{"type":"text","id":"VkEv-1687161964877","leaves":[{"text":"                    services.AddQuartzHostedService(options =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"zAWJ-1687161964880","name":"code-line","data":{},"nodes":[{"type":"text","id":"N8pK-1687161964879","leaves":[{"text":"                    {    \r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Uy6v-1687161964882","name":"code-line","data":{},"nodes":[{"type":"text","id":"T3nS-1687161964881","leaves":[{"text":"                        options.WaitForJobsToComplete = true;\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"5gyg-1687161964884","name":"code-line","data":{},"nodes":[{"type":"text","id":"wj2M-1687161964883","leaves":[{"text":"                    });\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"gC4R-1687161964886","name":"code-line","data":{},"nodes":[{"type":"text","id":"DF7f-1687161964885","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"KIUk-1687161964888","name":"code-line","data":{},"nodes":[{"type":"text","id":"zetC-1687161964887","leaves":[{"text":"                }).Build();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"U99X-1687161964890","name":"code-line","data":{},"nodes":[{"type":"text","id":"BY3E-1687161964889","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"yui9-1687161964892","name":"code-line","data":{},"nodes":[{"type":"text","id":"Vg7u-1687161964891","leaves":[{"text":"            //var schedulerFactory =  build.Services.GetService();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"7r7s-1687161964894","name":"code-line","data":{},"nodes":[{"type":"text","id":"Cjzb-1687161964893","leaves":[{"text":"            //var scheduler = schedulerFactory.GetScheduler();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"5VaG-1687161964896","name":"code-line","data":{},"nodes":[{"type":"text","id":"AE3W-1687161964895","leaves":[{"text":"            build.Run();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"z3Yd-1687161964898","name":"code-line","data":{},"nodes":[{"type":"text","id":"SvIF-1687161964897","leaves":[{"text":"            Console.WriteLine(\"--------7\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"cxaK-1687161964900","name":"code-line","data":{},"nodes":[{"type":"text","id":"fEHd-1687161964899","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Wh0I-1687161964902","name":"code-line","data":{},"nodes":[{"type":"text","id":"tkiU-1687161964901","leaves":[{"text":"    }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"i9Rc-1687161964904","name":"code-line","data":{},"nodes":[{"type":"text","id":"jQFd-1687161964903","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"kyPU-1687161964906","name":"code-line","data":{},"nodes":[{"type":"text","id":"vKgA-1687161964905","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"LcZy-1687161964908","name":"code-line","data":{},"nodes":[{"type":"text","id":"v2NE-1687161964907","leaves":[{"text":"    public class SampleJob : IJob\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"BDWD-1687161964911","name":"code-line","data":{},"nodes":[{"type":"text","id":"4yNa-1687161964910","leaves":[{"text":"    {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"6oXm-1687161964913","name":"code-line","data":{},"nodes":[{"type":"text","id":"lBB0-1687161964912","leaves":[{"text":"        public SampleJob(ISchedulerFactory schedulerFactory, IJobFactory jobFactory)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"NLya-1687161964915","name":"code-line","data":{},"nodes":[{"type":"text","id":"xrdW-1687161964914","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"x44A-1687161964917","name":"code-line","data":{},"nodes":[{"type":"text","id":"SMhy-1687161964916","leaves":[{"text":"            Console.WriteLine(\"--------8\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"CoCC-1687161964919","name":"code-line","data":{},"nodes":[{"type":"text","id":"ELs7-1687161964918","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Qk7w-1687161964921","name":"code-line","data":{},"nodes":[{"type":"text","id":"bBHK-1687161964920","leaves":[{"text":"        public async Task Execute(IJobExecutionContext context)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"MW95-1687161964923","name":"code-line","data":{},"nodes":[{"type":"text","id":"60bx-1687161964922","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"IECC-1687161964925","name":"code-line","data":{},"nodes":[{"type":"text","id":"1hSh-1687161964924","leaves":[{"text":"            Console.WriteLine(\"--------9\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Icdk-1687161964927","name":"code-line","data":{},"nodes":[{"type":"text","id":"h3eO-1687161964926","leaves":[{"text":"            context.JobDetail.JobDataMap.GetString(\"我是sample的job数据key\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"6w14-1687161964929","name":"code-line","data":{},"nodes":[{"type":"text","id":"Qq38-1687161964928","leaves":[{"text":"            Console.WriteLine($\"我是sample的job数据key: {context.JobDetail.JobDataMap.GetString(\"我是sample的job数据key\")}\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"GQUQ-1687161964931","name":"code-line","data":{},"nodes":[{"type":"text","id":"vowm-1687161964930","leaves":[{"text":"            Console.WriteLine($\"我是sample的Trigger数据key: {context.MergedJobDataMap.GetString(\"我是sample的Trigger数据key\")}\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"YMns-1687161964933","name":"code-line","data":{},"nodes":[{"type":"text","id":"6LhP-1687161964932","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"upiq-1687161964935","name":"code-line","data":{},"nodes":[{"type":"text","id":"YJRw-1687161964934","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"1yXP-1687161964937","name":"code-line","data":{},"nodes":[{"type":"text","id":"Knvp-1687161964936","leaves":[{"text":"    }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"sX2y-1687161964939","name":"code-line","data":{},"nodes":[{"type":"text","id":"U6Tz-1687161964938","leaves":[{"text":"    public class InitJob : IJob\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"70TV-1687161964941","name":"code-line","data":{},"nodes":[{"type":"text","id":"hKwJ-1687161964940","leaves":[{"text":"    {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"t6qr-1687161964943","name":"code-line","data":{},"nodes":[{"type":"text","id":"cKh3-1687161964942","leaves":[{"text":"        public ISchedulerFactory _schedulerFactory;\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"L4QA-1687161964945","name":"code-line","data":{},"nodes":[{"type":"text","id":"22pp-1687161964944","leaves":[{"text":"        public IJobFactory _jobFactory;\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"sgCv-1687161964947","name":"code-line","data":{},"nodes":[{"type":"text","id":"8EUu-1687161964946","leaves":[{"text":"        public InitJob(ISchedulerFactory schedulerFactory, IJobFactory jobFactory)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Uezr-1687161964949","name":"code-line","data":{},"nodes":[{"type":"text","id":"eHKq-1687161964948","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Npvn-1687161964951","name":"code-line","data":{},"nodes":[{"type":"text","id":"Db3E-1687161964950","leaves":[{"text":"            Console.WriteLine(\"--------12\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ei04-1687161964953","name":"code-line","data":{},"nodes":[{"type":"text","id":"74xU-1687161964952","leaves":[{"text":"            _schedulerFactory = schedulerFactory;\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"6ymY-1687161964955","name":"code-line","data":{},"nodes":[{"type":"text","id":"Xp1q-1687161964954","leaves":[{"text":"            _jobFactory = jobFactory;\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ILOX-1687161964957","name":"code-line","data":{},"nodes":[{"type":"text","id":"Ul0x-1687161964956","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"0wYK-1687161964959","name":"code-line","data":{},"nodes":[{"type":"text","id":"jKRl-1687161964958","leaves":[{"text":"        public async Task Execute(IJobExecutionContext context)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"SUYj-1687161964961","name":"code-line","data":{},"nodes":[{"type":"text","id":"k7rB-1687161964960","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Yo4d-1687161964963","name":"code-line","data":{},"nodes":[{"type":"text","id":"bxII-1687161964962","leaves":[{"text":"            Console.WriteLine(\"--------13\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"LGxD-1687161964965","name":"code-line","data":{},"nodes":[{"type":"text","id":"tc5O-1687161964964","leaves":[{"text":"            Console.WriteLine(\"InitJob Execute \" + Random.Shared.Next(0, 100));\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"i8fR-1687161964967","name":"code-line","data":{},"nodes":[{"type":"text","id":"o27Z-1687161964966","leaves":[{"text":"            ","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Dk4o-1687162287366","name":"code-line","data":{},"nodes":[{"type":"text","id":"i6jr-1687162287364","leaves":[{"text":"            //创建job\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"UIgv-1687161964969","name":"code-line","data":{},"nodes":[{"type":"text","id":"ip0i-1687161964968","leaves":[{"text":"            IJobDetail job = JobBuilder.Create()","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"vNZg-1687162730860","name":"code-line","data":{},"nodes":[{"type":"text","id":"omeV-1687162730858","leaves":[{"text":"                                //写入参数\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"keUA-1687161964971","name":"code-line","data":{},"nodes":[{"type":"text","id":"cmsu-1687161964970","leaves":[{"text":"                                .UsingJobData(\"我是sample的job数据key\", \"我是sample的job数据value\")\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ga4I-1687161964973","name":"code-line","data":{},"nodes":[{"type":"text","id":"5PtW-1687161964972","leaves":[{"text":"                                .WithIdentity(\"sample1\", \"jobgroup1\").Build();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"uz29-1687161964975","name":"code-line","data":{},"nodes":[{"type":"text","id":"uTdn-1687161964974","leaves":[{"text":"            //创建触发器\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Q5xK-1687161964977","name":"code-line","data":{},"nodes":[{"type":"text","id":"x2Aa-1687161964976","leaves":[{"text":"            ITrigger trigger = TriggerBuilder.Create()\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"oP85-1687161964979","name":"code-line","data":{},"nodes":[{"type":"text","id":"0lVb-1687161964978","leaves":[{"text":"                                   .UsingJobData(\"我是sample的Trigger数据key\", \"我是sample的Trigger数据value\")\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"p7Vg-1687161964981","name":"code-line","data":{},"nodes":[{"type":"text","id":"CUP8-1687161964980","leaves":[{"text":"                                   .WithIdentity(\"trigger_sample1\", \"triggergroup1\")\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"BpCR-1687161964983","name":"code-line","data":{},"nodes":[{"type":"text","id":"BsYd-1687161964982","leaves":[{"text":"                                   .WithDescription(\"我是描述\")","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"8zLn-1687162742867","name":"code-line","data":{},"nodes":[{"type":"text","id":"3M15-1687162742866","leaves":[{"text":"                                   //通过corn符号来创建触发器\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"I849-1687161964985","name":"code-line","data":{},"nodes":[{"type":"text","id":"kN0A-1687161964984","leaves":[{"text":"                                   //.WithCronSchedule(taskOptions.CronExpression)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"tZ5n-1687161964987","name":"code-line","data":{},"nodes":[{"type":"text","id":"fHSV-1687161964986","leaves":[{"text":"                                   .WithSimpleSchedule(x =>","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"fz62-1687162767231","name":"code-line","data":{},"nodes":[{"type":"text","id":"ftre-1687162767230","leaves":[{"text":"                                       x.WithIntervalInSeconds(5) //5秒后执行","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"WeVG-1687162770868","name":"code-line","data":{},"nodes":[{"type":"text","id":"888a-1687162770867","leaves":[{"text":"                                       .RepeatForever()  //重复","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"McC5-1687162772989","name":"code-line","data":{},"nodes":[{"type":"text","id":"jH15-1687162772988","leaves":[{"text":"                                    )\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Mgwr-1687161964989","name":"code-line","data":{},"nodes":[{"type":"text","id":"Loea-1687161964988","leaves":[{"text":"                                   .Build();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"0zDj-1687161964991","name":"code-line","data":{},"nodes":[{"type":"text","id":"LAjG-1687161964990","leaves":[{"text":"            //通过工厂获取一个作业调度\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"8amS-1687161964993","name":"code-line","data":{},"nodes":[{"type":"text","id":"IxAJ-1687161964992","leaves":[{"text":"            var scheduler = await _schedulerFactory.GetScheduler();","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"SGgm-1687162338312","name":"code-line","data":{},"nodes":[{"type":"text","id":"A0Ka-1687162338310","leaves":[{"text":"            //绑定一个job的事件侦听器,从执行顺序上看 new JobListen是一个单例类\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"kBA9-1687161964995","name":"code-line","data":{},"nodes":[{"type":"text","id":"kPPe-1687161964994","leaves":[{"text":"            scheduler.ListenerManager.AddJobListener(new JobListen(), KeyMatcher.KeyEquals(new JobKey(\"sample1\", \"jobgroup1\")));","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"fVO4-1687162354638","name":"code-line","data":{},"nodes":[{"type":"text","id":"HKrS-1687162354636","leaves":[{"text":"            //将作业和从触发器绑定至作业调度上\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ROHL-1687161964997","name":"code-line","data":{},"nodes":[{"type":"text","id":"V9rL-1687161964996","leaves":[{"text":"            await scheduler.ScheduleJob(job, trigger);\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"SlgU-1687162375425","name":"code-line","data":{},"nodes":[{"type":"text","id":"hUcU-1687162375423","leaves":[{"text":"            //启动作业调度","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"xmGN-1687161964999","name":"code-line","data":{},"nodes":[{"type":"text","id":"RONa-1687161964998","leaves":[{"text":"            await scheduler.Start();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"l6Cs-1687161965001","name":"code-line","data":{},"nodes":[{"type":"text","id":"Kmjm-1687161965000","leaves":[{"text":"\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"86Cu-1687161965003","name":"code-line","data":{},"nodes":[{"type":"text","id":"qCcC-1687161965002","leaves":[{"text":"            Console.WriteLine(\"--------14\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"c6Tl-1687161965005","name":"code-line","data":{},"nodes":[{"type":"text","id":"PCLz-1687161965004","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"RS0S-1687161965007","name":"code-line","data":{},"nodes":[{"type":"text","id":"vsxl-1687161965006","leaves":[{"text":"    }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Vl4I-1687161965009","name":"code-line","data":{},"nodes":[{"type":"text","id":"c0RA-1687161965008","leaves":[{"text":"    //作业侦听器\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"VF9t-1687161965011","name":"code-line","data":{},"nodes":[{"type":"text","id":"e3hb-1687161965010","leaves":[{"text":"    public class JobListen : JobListenerSupport\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"6pM7-1687161965013","name":"code-line","data":{},"nodes":[{"type":"text","id":"x1YH-1687161965012","leaves":[{"text":"    {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"aEpf-1687161965015","name":"code-line","data":{},"nodes":[{"type":"text","id":"aOG0-1687161965014","leaves":[{"text":"        public JobListen()\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"3cDS-1687161965017","name":"code-line","data":{},"nodes":[{"type":"text","id":"WMmB-1687161965016","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"uiyX-1687161965019","name":"code-line","data":{},"nodes":[{"type":"text","id":"xMEE-1687161965018","leaves":[{"text":"            Console.WriteLine(\"--------20\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"cxcB-1687161965021","name":"code-line","data":{},"nodes":[{"type":"text","id":"Mnr9-1687161965020","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"WKCS-1687161965023","name":"code-line","data":{},"nodes":[{"type":"text","id":"nuIo-1687161965022","leaves":[{"text":"        public override string Name { get { return \"JobListen20\"; } }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"SJlh-1687161965025","name":"code-line","data":{},"nodes":[{"type":"text","id":"5LGA-1687161965024","leaves":[{"text":"        //调用job之前执行\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"e0sq-1687161965027","name":"code-line","data":{},"nodes":[{"type":"text","id":"azuB-1687161965026","leaves":[{"text":"        public override Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Nd8X-1687161965029","name":"code-line","data":{},"nodes":[{"type":"text","id":"uADz-1687161965028","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"7qUh-1687161965031","name":"code-line","data":{},"nodes":[{"type":"text","id":"u286-1687161965030","leaves":[{"text":"            Console.WriteLine(\"--------21\");\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"MkIC-1687161965033","name":"code-line","data":{},"nodes":[{"type":"text","id":"Wx6M-1687161965032","leaves":[{"text":"            return base.JobToBeExecuted(context, cancellationToken);\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"0Z1U-1687161965035","name":"code-line","data":{},"nodes":[{"type":"text","id":"EvyH-1687161965034","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"HXi2-1687161965037","name":"code-line","data":{},"nodes":[{"type":"text","id":"1KkY-1687161965036","leaves":[{"text":"    }","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"jqj6-1687162448650","name":"code-line","data":{},"nodes":[{"type":"text","id":"Y6jI-1687162448649","leaves":[{"text":"    //日志组件\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Wi6O-1687161965039","name":"code-line","data":{},"nodes":[{"type":"text","id":"ow12-1687161965038","leaves":[{"text":"    public class ConsoleLogProvider : ILogProvider\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"u67z-1687161965041","name":"code-line","data":{},"nodes":[{"type":"text","id":"yVNX-1687161965040","leaves":[{"text":"    {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"rE7p-1687161965043","name":"code-line","data":{},"nodes":[{"type":"text","id":"4qy9-1687161965042","leaves":[{"text":"        public Logger GetLogger(string name)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"ukxy-1687161965045","name":"code-line","data":{},"nodes":[{"type":"text","id":"8xDY-1687161965044","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"5jNp-1687161965047","name":"code-line","data":{},"nodes":[{"type":"text","id":"seNE-1687161965046","leaves":[{"text":"            return (level, func, exception, parameters) =>\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"9WQ2-1687161965049","name":"code-line","data":{},"nodes":[{"type":"text","id":"BflN-1687161965048","leaves":[{"text":"            {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"DKrq-1687161965051","name":"code-line","data":{},"nodes":[{"type":"text","id":"DBZG-1687161965050","leaves":[{"text":"                if (level >= LogLevel.Info && func != null)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Tdmv-1687161965053","name":"code-line","data":{},"nodes":[{"type":"text","id":"x8w1-1687161965052","leaves":[{"text":"                {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"kHG6-1687161965055","name":"code-line","data":{},"nodes":[{"type":"text","id":"CJJO-1687161965054","leaves":[{"text":"                    Console.WriteLine(\"[\" + DateTime.Now.ToLongTimeString() + \"] [\" + level + \"] \" + func(), parameters);\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"uDz8-1687161965057","name":"code-line","data":{},"nodes":[{"type":"text","id":"JbUz-1687161965056","leaves":[{"text":"                }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Gbau-1687161965059","name":"code-line","data":{},"nodes":[{"type":"text","id":"1Jol-1687161965058","leaves":[{"text":"                return true;\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"b1sP-1687161965061","name":"code-line","data":{},"nodes":[{"type":"text","id":"xjFv-1687161965060","leaves":[{"text":"            };\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"MNJf-1687161965063","name":"code-line","data":{},"nodes":[{"type":"text","id":"bHOS-1687161965062","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"gIO2-1687161965065","name":"code-line","data":{},"nodes":[{"type":"text","id":"xvRo-1687161965064","leaves":[{"text":"        public IDisposable OpenNestedContext(string message)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"A42h-1687161965067","name":"code-line","data":{},"nodes":[{"type":"text","id":"fFF7-1687161965066","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"j6q1-1687161965069","name":"code-line","data":{},"nodes":[{"type":"text","id":"Zwbr-1687161965068","leaves":[{"text":"            throw new NotImplementedException();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"v57d-1687161965071","name":"code-line","data":{},"nodes":[{"type":"text","id":"a1HK-1687161965070","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"verW-1687161965073","name":"code-line","data":{},"nodes":[{"type":"text","id":"bv48-1687161965072","leaves":[{"text":"        public IDisposable OpenMappedContext(string key, object value, bool destructure = false)\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"d4GC-1687161965075","name":"code-line","data":{},"nodes":[{"type":"text","id":"UMta-1687161965074","leaves":[{"text":"        {\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"sVnQ-1687161965077","name":"code-line","data":{},"nodes":[{"type":"text","id":"hTY0-1687161965076","leaves":[{"text":"            throw new NotImplementedException();\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"xpkq-1687161965079","name":"code-line","data":{},"nodes":[{"type":"text","id":"PIjd-1687161965078","leaves":[{"text":"        }\r","marks":[{"type":"fontSize","value":14}]}]}]},{"type":"block","id":"Bry6-1687161965081","name":"code-line","data":{},"nodes":[{"type":"text","id":"kHU2-1687161965080","leaves":[{"text":"    }","marks":[{"type":"fontSize","value":14}]}]}]}],"state":{"language":"typescript"}},{"type":"block","id":"kMKw-1687161963230","name":"heading","data":{"level":"h5","style":{}},"nodes":[{"type":"text","id":"h4Or-1687161963231","leaves":[{"text":"执行结果","marks":[]}]}],"state":{}},{"type":"block","id":"rjvE-1687162003033","name":"paragraph","data":{},"nodes":[{"type":"text","id":"c2KH-1687162003031","leaves":[{"text":"","marks":[{"type":"fontSize","value":14}]}]}],"state":{}},{"type":"block","id":"4k6t-1687162236435","name":"image","data":{"version":1,"url":"https://note.youdao.com/yws/res/7/WEBRESOURCEce1e0087065686034f05d5e3d91acbf7","width":1121,"height":989},"nodes":[],"state":{"loading":false,"renderSource":"https://note.youdao.com/yws/res/7/WEBRESOURCEce1e0087065686034f05d5e3d91acbf7","initialSize":{"width":1121,"height":989}}},{"type":"block","id":"kR6k-1687162236439","name":"paragraph","data":{},"nodes":[{"type":"text","id":"KwHi-1687162236438","leaves":[{"text":"","marks":[{"type":"fontSize","value":14}]}]}],"state":{}}]">Quartz.NET 官网Quartz.net是什么

Quartz.NET 是一个功能齐全的开源作业调度系统,他的前身来源于java的Quartz.Quartz.net安装和使用

基于visual studio引用安装,其他IDE类似,或者下载DLL手动引用也是可以的;运行环境基于.net core,源.net程序类似 Quartz.net的架构和关键属性以及方法

三个主要的概念


[*]scheduler 作业调度,作业计划在给定触发器发生时运行,实际就是领导
[*]job 作业,实现简单 IJob 接口的任何 .NET 类,实际就是干活的员工
[*]trigger 侦听器,负责捕获调度事件以监视或控制作业,实际就是监工
监工发现员工偷懒了,报告给领导,领导知道后,给员工派了很多活,导致了员工天天996. 大概是这么个关系 Quartz.net的一些关键属性

类型 ISchedulerFactorySchedulerBuilder的工厂类IScheduler用于与调度程序交互的主要 APISchedulerBuilder用于定义/构建调度程序实例,需要 Quartz 3.1 或更高版本IJobFactoryJobBuilder的工厂类IJob由您希望由调度程序执行的组件实现的接口IJobDetail用于定义作业的实例JobBuilder用于定义/构建 JobDetail 实例,这些实例定义作业的实例TriggerBuilder用于定义/构建触发器实例ITrigger定义执行给定作业的计划的一个组件,作业可以有多个关联的触发器ListenerManager侦听器事件,例如:执行job工作之前,之后触发等等,同时也可用于触发器侦听 IServiceCollectionQuartzConfigurator 参数

Scheduler Name调度作业的名称Scheduler IdSchedulerIdMax Batch Size同时执行job的最大数量InterruptJobsOnShutdown InterruptJobsOnShutdownWithWait BatchTriggerAcquisitionFireAheadTimeWindow  在通用host或者webhost中的最佳实践

通用host或者webhost代码是一样的执行流程


[*]在通用主机服务中注入服务AddQuartz,AddQuartzHostedService
[*]在AddQuartz中配置调度作业的基本属性(SchedulerId等等)和调度器以及作业(ScheduleJob,AddJob,AddTrigger);可以在这个地方写入所有的调度作业,也可以写入一个initjob作业,在主机完全启动5秒后执行相应的业务(可规避掉某些依赖服务未启动的问题)
[*]在initjob中,初始化其他定时任务。官网介绍job只能有一个无参的构造函数,但我亲测可以注入(笑脸)
[*]关于job和reigger的具体参数,可查看官网
如下

以下代码和执行结果,其中执行顺序一目了然代码

   static void Main(string[] args)
      {
            Console.WriteLine("Hello, World!");

            LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());
            //通用主机配置
            var build = Host.CreateDefaultBuilder(args)
                .ConfigureServices((host, services) =>
                {
                  Console.WriteLine("--------1");
                  
                  //调度作业的唯一id的唯一标识,用于集群搭建cluster
                  q.SchedulerId = "SchedulerId_01";
                  //配置Quartz服务
                  services.AddQuartz(q =>
                  {
                        Console.WriteLine("--------2");
                        //依赖注入,ISchedulerFactory,Ijob等等
                        q.UseMicrosoftDependencyInjectionJobFactory();

                        //方法一和方法二使用不同方法的写法,本质基本是一样的
                        //方法一
                        q.ScheduleJob<InitJob>(
                            trigger =>
                            {
                              Console.WriteLine("--------33");
                              //WithIdentity 绑定触发器或者job的唯一属性和组
                              //TriggerKey,JobKey 都是代表唯一个属性和组
                              trigger.WithIdentity(new TriggerKey("trigger1", "triggergroup1"))
                                       .WithSimpleSchedule(x => x.WithIntervalInSeconds(5))
                                       // .StartAt(DateBuilder.EvenSecondDate(DateTimeOffset.UtcNow.AddSeconds(5)))
                                       //.WithDailyTimeIntervalSchedule(x => x.WithInterval(10, IntervalUnit.Second))
                                       .WithDescription("init 描述");
                            },
                            jobConfigure =>
                            {
                              Console.WriteLine("--------44");
                              jobConfigure.WithIdentity(new JobKey("Init1", "jobgroup1"));
                            }
                        );

                        //方法二
                        //q.AddJob<InitJob>(opts =>
                        //{
                        //    Console.WriteLine("--------3");
                        //    opts.WithIdentity(new JobKey("Init1", "jobgroup1"));

                        //});

                        //q.AddTrigger(opts =>
                        //{
                        //    Console.WriteLine("--------4");
                        //    //将job添加至触发器中
                        //    opts.ForJob(new JobKey("Init1", "jobgroup1"))
                        //    .WithIdentity("trigger1", "triggergroup1")
                        //    .WithSimpleSchedule(x =>
                        //    {
                        //      Console.WriteLine("--------6");
                        //      x.WithIntervalInSeconds(5);
                        //      //.RepeatForever();
                        //      //.WithRepeatCount(5);
                        //    });

                        //});

                  });
                  services.AddQuartzHostedService(options =>
                  {   
                        options.WaitForJobsToComplete = true;
                  });

                }).Build();

            //var schedulerFactory =build.Services.GetService<ISchedulerFactory>();
            //var scheduler = schedulerFactory.GetScheduler();
            build.Run();
            Console.WriteLine("--------7");
      }
    }


    public class SampleJob : IJob
    {
      public SampleJob(ISchedulerFactory schedulerFactory, IJobFactory jobFactory)
      {
            Console.WriteLine("--------8");
      }
      public async Task Execute(IJobExecutionContext context)
      {
            Console.WriteLine("--------9");
            context.JobDetail.JobDataMap.GetString("我是sample的job数据key");
            Console.WriteLine($"我是sample的job数据key: {context.JobDetail.JobDataMap.GetString("我是sample的job数据key")}");
            Console.WriteLine($"我是sample的Trigger数据key: {context.MergedJobDataMap.GetString("我是sample的Trigger数据key")}");

      }
    }
    public class InitJob : IJob
    {
      public ISchedulerFactory _schedulerFactory;
      public IJobFactory _jobFactory;
      public InitJob(ISchedulerFactory schedulerFactory, IJobFactory jobFactory)
      {
            Console.WriteLine("--------12");
            _schedulerFactory = schedulerFactory;
            _jobFactory = jobFactory;
      }
      public async Task Execute(IJobExecutionContext context)
      {
            Console.WriteLine("--------13");
            Console.WriteLine("InitJob Execute " + Random.Shared.Next(0, 100));
            
            //创建job
            IJobDetail job = JobBuilder.Create<SampleJob>()
                              //写入参数
                              .UsingJobData("我是sample的job数据key", "我是sample的job数据value")
                              .WithIdentity("sample1", "jobgroup1").Build();
            //创建触发器
            ITrigger trigger = TriggerBuilder.Create()
                                 .UsingJobData("我是sample的Trigger数据key", "我是sample的Trigger数据value")
                                 .WithIdentity("trigger_sample1", "triggergroup1")
                                 .WithDescription("我是描述")
                                 //通过corn符号来创建触发器
                                 //.WithCronSchedule(taskOptions.CronExpression)
                                 .WithSimpleSchedule(x =>
                                       x.WithIntervalInSeconds(5) //5秒后执行
                                       .RepeatForever()//重复
                                    )
                                 .Build();
            //通过工厂获取一个作业调度
            var scheduler = await _schedulerFactory.GetScheduler();
            //绑定一个job的事件侦听器,从执行顺序上看 new JobListen是一个单例类
            scheduler.ListenerManager.AddJobListener(new JobListen(), KeyMatcher<JobKey>.KeyEquals(new JobKey("sample1", "jobgroup1")));
            //将作业和从触发器绑定至作业调度上
            await scheduler.ScheduleJob(job, trigger);
            //启动作业调度
            await scheduler.Start();

            Console.WriteLine("--------14");
      }
    }
    //作业侦听器
    public class JobListen : JobListenerSupport
    {
      public JobListen()
      {
            Console.WriteLine("--------20");
      }
      public override string Name { get { return "JobListen20"; } }
      //调用job之前执行
      public override Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken)
      {
            Console.WriteLine("--------21");
            return base.JobToBeExecuted(context, cancellationToken);
      }
    }
    //日志组件
    public class ConsoleLogProvider : ILogProvider
    {
      public Logger GetLogger(string name)
      {
            return (level, func, exception, parameters) =>
            {
                if (level >= LogLevel.Info && func != null)
                {
                  Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters);
                }
                return true;
            };
      }
      public IDisposable OpenNestedContext(string message)
      {
            throw new NotImplementedException();
      }
      public IDisposable OpenMappedContext(string key, object value, bool destructure = false)
      {
            throw new NotImplementedException();
      }
    }执行结果

  
来源:https://www.cnblogs.com/workcn/archive/2023/06/19/17491550.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Quartz.net的最佳实践