Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

146 linhas
5.3 KiB

  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. }