JobManage.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Main.Base;
  7. using Models;
  8. using Quartz;
  9. using Quartz.Impl;
  10. namespace Main
  11. {
  12. /// <summary>
  13. /// 作业管理类
  14. /// </summary>
  15. public class JobManage : BaseJobManager
  16. {
  17. private IScheduler _sched = null;
  18. /// <summary>
  19. /// 开始所有作业调度
  20. /// </summary>
  21. public void JobStart()
  22. {
  23. //获取配置文件
  24. List<JobConfigEntity> configs = InitJobConfig();
  25. NameValueCollection properties = new NameValueCollection
  26. {
  27. ["author"] = "大师兄",
  28. ["version"] = "V1.0.0",
  29. ["createtime"] = "2018年1月25日"
  30. };
  31. ISchedulerFactory sf = new StdSchedulerFactory(properties);
  32. _sched = sf.GetScheduler();
  33. List<Type> allInheirtFromIJob = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob))).ToList();
  34. foreach (JobConfigEntity config in configs)
  35. {
  36. Type jobType = allInheirtFromIJob.FirstOrDefault(s => s.Name == config.JobName);
  37. if (jobType == null) continue;
  38. IJobDetail job = JobBuilder.Create(jobType)
  39. .WithIdentity(config.JobIdentityName, config.JobGroup)
  40. .Build();
  41. ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
  42. .WithIdentity(config.TriggerIdentityName, config.JobGroup)
  43. .WithCronSchedule(config.CronExpression)
  44. .Build();
  45. foreach (PropertyInfo property in typeof(JobConfigEntity).GetProperties())
  46. {
  47. job.JobDataMap.Put(property.Name, property.GetValue(config, null));
  48. }
  49. DateTimeOffset ft = _sched.ScheduleJob(job, trigger);
  50. }
  51. _sched.Start();
  52. }
  53. /// <summary>
  54. /// 停止所有作业调度
  55. /// </summary>
  56. public void JobStop()
  57. {
  58. if (_sched != null && _sched.IsStarted)
  59. _sched.Shutdown(true);
  60. }
  61. }
  62. }