JobManage.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Specialized;
  6. using System.Reflection;
  7. using Quartz;
  8. using Quartz.Impl;
  9. using SCC.Common;
  10. using SCC.Models;
  11. namespace SCC.Crawler
  12. {
  13. /// <summary>
  14. /// 作业管理类
  15. /// </summary>
  16. public class JobManage
  17. {
  18. private IScheduler sched = null;
  19. /// <summary>
  20. /// 开始所有作业调度
  21. /// </summary>
  22. public void JobStart()
  23. {
  24. var configFile = AppDomain.CurrentDomain.BaseDirectory + "/SCCConfig.xml";
  25. var configs = CommonHelper.ConvertXMLToObject<SCCConfig>(configFile, "SCCSettings");
  26. var properties = new NameValueCollection
  27. {
  28. ["author"] = "大师兄"
  29. };
  30. ISchedulerFactory sf = new StdSchedulerFactory(properties);
  31. IScheduler sched = sf.GetScheduler();
  32. var allInheirtFromIJob = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob))).ToList();
  33. foreach (SCCConfig config in configs)
  34. {
  35. var jobType = allInheirtFromIJob.FirstOrDefault(s => s.Name == config.JobName);
  36. if (jobType == null) continue;
  37. IJobDetail job = JobBuilder.Create(jobType)
  38. .WithIdentity(config.JobIdentityName, config.JobGroup)
  39. .Build();
  40. ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
  41. .WithIdentity(config.TriggerIdentityName, config.JobGroup)
  42. .WithCronSchedule(config.CronExpression)
  43. .Build();
  44. foreach (PropertyInfo property in typeof(SCCConfig).GetProperties())
  45. {
  46. job.JobDataMap.Put(property.Name, property.GetValue(config, null));
  47. }
  48. var ft = sched.ScheduleJob(job, trigger);
  49. }
  50. sched.Start();
  51. }
  52. /// <summary>
  53. /// 停止所有作业调度
  54. /// </summary>
  55. public void JobStop()
  56. {
  57. if (sched != null && sched.IsStarted)
  58. sched.Shutdown(true);
  59. }
  60. }
  61. }