StartJob.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Quartz;
  2. using Quartz.Impl;
  3. public class StartJob
  4. {
  5. public readonly static StartJob Instance = new StartJob();
  6. private StartJob()
  7. { }
  8. public async void Start()
  9. {
  10. // 创建调度器
  11. IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
  12. await scheduler.Start();
  13. string[] list = { "my", "your"};
  14. foreach(string sub in list)
  15. {
  16. // 定义作业
  17. IJobDetail job = JobBuilder.Create<MyJob>()
  18. .WithIdentity(sub + "Job")
  19. .SetJobData(new JobDataMap(Newtonsoft.Json.JsonConvert.DeserializeObject<IDictionary<string, object>>("{\"" + sub + "test\":123,\"" + sub + "test2\":456}")))
  20. .Build();
  21. // 定义触发器
  22. ITrigger trigger = TriggerBuilder.Create()
  23. .WithIdentity(sub + "Trigger")
  24. .StartNow()
  25. .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
  26. .Build();
  27. // 调度作业
  28. await scheduler.ScheduleJob(job, trigger);
  29. }
  30. }
  31. }