You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QuartzCore.cs 5.3 KiB

2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Quartz.Impl;
  2. using Quartz;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using static Org.BouncyCastle.Math.EC.ECCurve;
  10. namespace HKCardOUT.QuartzUtil
  11. {
  12. public class QuartzCore: IQuartzCore
  13. {
  14. private Task<IScheduler> instance;
  15. /// <summary>
  16. /// 初始化任务调度器
  17. /// </summary>
  18. public Task<IScheduler> Instance
  19. {
  20. get
  21. {
  22. if (instance != null)
  23. return instance;
  24. else
  25. {
  26. NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } };
  27. instance = new StdSchedulerFactory(props).GetScheduler();
  28. return instance;
  29. }
  30. }
  31. }
  32. /// <summary>
  33. /// 创建简单任务
  34. /// </summary>
  35. /// <param name="quartz"></param>
  36. /// <returns></returns>
  37. protected ITrigger CreateSimpleTrigger(QuartzMap quartz)
  38. {
  39. if (quartz.RunTimes > 0)
  40. {
  41. return TriggerBuilder.Create().WithIdentity(quartz.JobName, quartz.JobGroup)
  42. .StartAt(quartz.StartTime).EndAt(quartz.EndTime)
  43. .WithSimpleSchedule(t => t.WithIntervalInSeconds(quartz.IntervalSecond)
  44. .WithRepeatCount(quartz.RunTimes)).ForJob(quartz.JobName, quartz.JobGroup)
  45. .WithDescription(quartz.JobDetail).Build();
  46. }
  47. else
  48. {
  49. //无限循环执行
  50. return TriggerBuilder.Create().WithIdentity(quartz.JobName, quartz.JobGroup)
  51. .StartAt(quartz.StartTime).EndAt(quartz.EndTime)
  52. .WithSimpleSchedule(t => t.WithIntervalInSeconds(quartz.IntervalSecond)
  53. .RepeatForever()).ForJob(quartz.JobName, quartz.JobGroup)
  54. .WithDescription(quartz.JobDetail).Build();
  55. }
  56. }
  57. /// <summary>
  58. /// 创建表达式任务
  59. /// </summary>
  60. /// <param name="quartz"></param>
  61. /// <returns></returns>
  62. protected ITrigger CreateCronTrigger(QuartzMap quartz)
  63. {
  64. return TriggerBuilder.Create().WithIdentity(quartz.JobName, quartz.JobGroup)
  65. .StartAt(quartz.StartTime).EndAt(quartz.EndTime)
  66. .WithCronSchedule(quartz.Cron).ForJob(quartz.JobName, quartz.JobGroup)
  67. .WithDescription(quartz.JobDetail).Build();
  68. }
  69. /// <summary>
  70. /// 暂停指定任务
  71. /// </summary>
  72. /// <typeparam name="T"></typeparam>
  73. /// <param name="quartz"></param>
  74. /// <returns></returns>
  75. public async void StopResumeJob(QuartzMap quartz)
  76. {
  77. var key = new JobKey(quartz.JobName, quartz.JobGroup);
  78. if (await Instance.Result.CheckExists(key))
  79. await Instance.Result.PauseJob(key);
  80. }
  81. /// <summary>
  82. /// 恢复运行已经暂停的指定任务
  83. /// </summary>
  84. /// <param name="quartz"></param>
  85. public async void ResumeJob(QuartzMap quartz)
  86. {
  87. try
  88. {
  89. //检查任务是否存在
  90. var key = new JobKey(quartz.JobName, quartz.JobGroup);
  91. if (await Instance.Result.CheckExists(key))
  92. await Instance.Result.ResumeJob(key);
  93. }
  94. catch (Exception)
  95. {
  96. throw new Exception("恢复任务失败!");
  97. }
  98. }
  99. /// <summary>
  100. /// 停止任务
  101. /// </summary>
  102. public async void StopJob()
  103. {
  104. try
  105. {
  106. //判断调度是否已经关闭
  107. if (!Instance.Result.IsShutdown)
  108. //等待任务运行完成
  109. await Instance.Result.Shutdown();
  110. }
  111. catch (Exception)
  112. {
  113. throw new Exception("停止任务失败!");
  114. }
  115. }
  116. /// <summary>
  117. /// 添加任务指定实现IJob接口的类
  118. /// </summary>
  119. /// <typeparam name="T"></typeparam>
  120. /// <param name="quartz"></param>
  121. /// <returns></returns>
  122. public async Task<String> AddJob<T>(QuartzMap quartz) where T : IJob
  123. {
  124. try
  125. {
  126. await Instance.Result.Start();
  127. JobKey key = new JobKey(quartz.JobName, quartz.JobGroup);
  128. //任务存在则先删除
  129. if (await Instance.Result.CheckExists(key))
  130. await Instance.Result.DeleteJob(key);
  131. IJobDetail job = JobBuilder.CreateForAsync<T>().WithIdentity(quartz.JobName, quartz.JobGroup).Build();
  132. if (!string.IsNullOrEmpty(quartz.Cron) && CronExpression.IsValidExpression(quartz.Cron))
  133. await Instance.Result.ScheduleJob(job, CreateCronTrigger(quartz));
  134. else
  135. await Instance.Result.ScheduleJob(job, CreateSimpleTrigger(quartz));
  136. return "添加任务成功!";
  137. }
  138. catch (Exception)
  139. {
  140. throw new Exception("添加任务出错!");
  141. }
  142. }
  143. }
  144. }