ScheduleUtils.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.ruoyi.quartz.util;
  2. import org.quartz.CronScheduleBuilder;
  3. import org.quartz.CronTrigger;
  4. import org.quartz.Job;
  5. import org.quartz.JobBuilder;
  6. import org.quartz.JobDetail;
  7. import org.quartz.JobKey;
  8. import org.quartz.Scheduler;
  9. import org.quartz.SchedulerException;
  10. import org.quartz.TriggerBuilder;
  11. import org.quartz.TriggerKey;
  12. import com.ruoyi.common.constant.Constants;
  13. import com.ruoyi.common.constant.ScheduleConstants;
  14. import com.ruoyi.common.exception.job.TaskException;
  15. import com.ruoyi.common.exception.job.TaskException.Code;
  16. import com.ruoyi.common.utils.StringUtils;
  17. import com.ruoyi.common.utils.spring.SpringUtils;
  18. import com.ruoyi.quartz.domain.SysJob;
  19. /**
  20. * 定时任务工具类
  21. *
  22. * @author ruoyi
  23. *
  24. */
  25. public class ScheduleUtils
  26. {
  27. /**
  28. * 得到quartz任务类
  29. *
  30. * @param sysJob 执行计划
  31. * @return 具体执行任务类
  32. */
  33. private static Class<? extends Job> getQuartzJobClass(SysJob sysJob)
  34. {
  35. boolean isConcurrent = "0".equals(sysJob.getConcurrent());
  36. return isConcurrent ? QuartzJobExecution.class : QuartzDisallowConcurrentExecution.class;
  37. }
  38. /**
  39. * 构建任务触发对象
  40. */
  41. public static TriggerKey getTriggerKey(Long jobId, String jobGroup)
  42. {
  43. return TriggerKey.triggerKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup);
  44. }
  45. /**
  46. * 构建任务键对象
  47. */
  48. public static JobKey getJobKey(Long jobId, String jobGroup)
  49. {
  50. return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup);
  51. }
  52. /**
  53. * 创建定时任务
  54. */
  55. public static void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException
  56. {
  57. Class<? extends Job> jobClass = getQuartzJobClass(job);
  58. // 构建job信息
  59. Long jobId = job.getJobId();
  60. String jobGroup = job.getJobGroup();
  61. JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(getJobKey(jobId, jobGroup)).build();
  62. // 表达式调度构建器
  63. CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
  64. cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);
  65. // 按新的cronExpression表达式构建一个新的trigger
  66. CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobId, jobGroup))
  67. .withSchedule(cronScheduleBuilder).build();
  68. // 放入参数,运行时的方法可以获取
  69. jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);
  70. // 判断是否存在
  71. if (scheduler.checkExists(getJobKey(jobId, jobGroup)))
  72. {
  73. // 防止创建时存在数据问题 先移除,然后在执行创建操作
  74. scheduler.deleteJob(getJobKey(jobId, jobGroup));
  75. }
  76. // 判断任务是否过期
  77. if (StringUtils.isNotNull(CronUtils.getNextExecution(job.getCronExpression())))
  78. {
  79. // 执行调度任务
  80. scheduler.scheduleJob(jobDetail, trigger);
  81. }
  82. // 暂停任务
  83. if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue()))
  84. {
  85. scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
  86. }
  87. }
  88. /**
  89. * 设置定时任务策略
  90. */
  91. public static CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob job, CronScheduleBuilder cb)
  92. throws TaskException
  93. {
  94. switch (job.getMisfirePolicy())
  95. {
  96. case ScheduleConstants.MISFIRE_DEFAULT:
  97. return cb;
  98. case ScheduleConstants.MISFIRE_IGNORE_MISFIRES:
  99. return cb.withMisfireHandlingInstructionIgnoreMisfires();
  100. case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED:
  101. return cb.withMisfireHandlingInstructionFireAndProceed();
  102. case ScheduleConstants.MISFIRE_DO_NOTHING:
  103. return cb.withMisfireHandlingInstructionDoNothing();
  104. default:
  105. throw new TaskException("The task misfire policy '" + job.getMisfirePolicy()
  106. + "' cannot be used in cron schedule tasks", Code.CONFIG_ERROR);
  107. }
  108. }
  109. /**
  110. * 检查包名是否为白名单配置
  111. *
  112. * @param invokeTarget 目标字符串
  113. * @return 结果
  114. */
  115. public static boolean whiteList(String invokeTarget)
  116. {
  117. String packageName = StringUtils.substringBefore(invokeTarget, "(");
  118. int count = StringUtils.countMatches(packageName, ".");
  119. if (count > 1)
  120. {
  121. return StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR);
  122. }
  123. Object obj = SpringUtils.getBean(StringUtils.split(invokeTarget, ".")[0]);
  124. return StringUtils.containsAnyIgnoreCase(obj.getClass().getPackage().getName(), Constants.JOB_WHITELIST_STR);
  125. }
  126. }