QuartzFactory.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using OASystem.API.OAMethodLib.Quartz.Jobs;
  2. using Quartz;
  3. using Quartz.Spi;
  4. namespace QuzrtzJob.Factory
  5. {
  6. public class QuartzFactory
  7. {
  8. //1、声明一个调度工厂
  9. private ISchedulerFactory _schedulerFactory;
  10. private IScheduler _scheduler = default;
  11. private IJobFactory _IOCjobFactory;
  12. public QuartzFactory(ISchedulerFactory schedulerFactory, IJobFactory jobFactory)
  13. {
  14. _schedulerFactory = schedulerFactory;
  15. _IOCjobFactory = jobFactory;
  16. }
  17. public async Task<string> Start()
  18. {
  19. //2、通过调度工厂获得调度器
  20. _scheduler = await _schedulerFactory.GetScheduler();
  21. //这里是指定容器仓库
  22. _scheduler.JobFactory = _IOCjobFactory;
  23. //3、开启调度器
  24. await _scheduler.Start();
  25. //4、创建一个触发器
  26. var trigger = TriggerBuilder.Create()
  27. //.WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever())//每两秒执行一次
  28. .WithCronSchedule("0 15 9 * * ?")
  29. .Build();
  30. var taskTrigger = TriggerBuilder.Create()
  31. //.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())//每六十秒执行一次
  32. .WithCronSchedule("0 0 9,18 * * ?") //每天早上九点和下午六点触发
  33. .Build();
  34. var taskMsgTrigger = TriggerBuilder.Create()
  35. //.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())//每六十秒执行一次
  36. .WithCronSchedule("0 0 9,18 * * ?") //每天早上九点和下午六点触发
  37. .Build();
  38. var teamCurrencyTrigger = TriggerBuilder.Create()
  39. //.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())//每六十秒执行一次
  40. .WithCronSchedule("0 0 0 * * ? ") //每天零点零分点触发
  41. .Build();
  42. //5、创建任务
  43. var jobDetail = JobBuilder.Create<ALiYunPostMessageJob>().WithIdentity("job1", "group").Build();
  44. var taskJobDetail = JobBuilder.Create<TaskJob>().WithIdentity("job2", "group").Build();
  45. var taskMsgJobDetail = JobBuilder.Create<TaskNewsFeedJob>().WithIdentity("job3", "group").Build();
  46. var teamCurrencyJobDetail = JobBuilder.Create<GroupTeamCurrencyJob>().WithIdentity("job4", "group").Build();
  47. //6、将触发器和任务器绑定到调度器中
  48. await _scheduler.ScheduleJob(jobDetail, trigger);
  49. await _scheduler.ScheduleJob(taskJobDetail, taskTrigger);
  50. await _scheduler.ScheduleJob(taskMsgJobDetail, taskMsgTrigger);
  51. await _scheduler.ScheduleJob(teamCurrencyJobDetail, teamCurrencyTrigger);
  52. return await Task.FromResult("将触发器和任务器绑定到调度器中完成");
  53. }
  54. }
  55. }