Você não pode selecionar mais de 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.

QuartzCore.cs 5.2 KiB

2 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. .WithCronSchedule(quartz.Cron).ForJob(quartz.JobName, quartz.JobGroup)
  66. .WithDescription(quartz.JobDetail).Build();
  67. }
  68. /// <summary>
  69. /// 暂停指定任务
  70. /// </summary>
  71. /// <typeparam name="T"></typeparam>
  72. /// <param name="quartz"></param>
  73. /// <returns></returns>
  74. public async void StopResumeJob(QuartzMap quartz)
  75. {
  76. var key = new JobKey(quartz.JobName, quartz.JobGroup);
  77. if (await Instance.Result.CheckExists(key))
  78. await Instance.Result.PauseJob(key);
  79. }
  80. /// <summary>
  81. /// 恢复运行已经暂停的指定任务
  82. /// </summary>
  83. /// <param name="quartz"></param>
  84. public async void ResumeJob(QuartzMap quartz)
  85. {
  86. try
  87. {
  88. //检查任务是否存在
  89. var key = new JobKey(quartz.JobName, quartz.JobGroup);
  90. if (await Instance.Result.CheckExists(key))
  91. await Instance.Result.ResumeJob(key);
  92. }
  93. catch (Exception)
  94. {
  95. throw new Exception("恢复任务失败!");
  96. }
  97. }
  98. /// <summary>
  99. /// 停止任务
  100. /// </summary>
  101. public async void StopJob()
  102. {
  103. try
  104. {
  105. //判断调度是否已经关闭
  106. if (!Instance.Result.IsShutdown)
  107. //等待任务运行完成
  108. await Instance.Result.Shutdown();
  109. }
  110. catch (Exception)
  111. {
  112. throw new Exception("停止任务失败!");
  113. }
  114. }
  115. /// <summary>
  116. /// 添加任务指定实现IJob接口的类
  117. /// </summary>
  118. /// <typeparam name="T"></typeparam>
  119. /// <param name="quartz"></param>
  120. /// <returns></returns>
  121. public async Task<String> AddJob<T>(QuartzMap quartz) where T : IJob
  122. {
  123. try
  124. {
  125. await Instance.Result.Start();
  126. JobKey key = new JobKey(quartz.JobName, quartz.JobGroup);
  127. //任务存在则先删除
  128. if (await Instance.Result.CheckExists(key))
  129. await Instance.Result.DeleteJob(key);
  130. IJobDetail job = JobBuilder.CreateForAsync<T>().WithIdentity(quartz.JobName, quartz.JobGroup).Build();
  131. if (!string.IsNullOrEmpty(quartz.Cron) && CronExpression.IsValidExpression(quartz.Cron))
  132. await Instance.Result.ScheduleJob(job, CreateCronTrigger(quartz));
  133. else
  134. await Instance.Result.ScheduleJob(job, CreateSimpleTrigger(quartz));
  135. return "添加任务成功!";
  136. }
  137. catch (Exception)
  138. {
  139. throw new Exception("添加任务出错!");
  140. }
  141. }
  142. }
  143. }