TaskInvokeUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2018-2025, hnqz All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the pig4cloud.com developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: hnqz
  16. */
  17. package net.yyc.quartz.util;
  18. import cn.hutool.core.util.StrUtil;
  19. import lombok.RequiredArgsConstructor;
  20. import lombok.SneakyThrows;
  21. import lombok.extern.slf4j.Slf4j;
  22. import net.yyc.quartz.constants.QuartzEnum;
  23. import net.yyc.quartz.entity.SysJob;
  24. import net.yyc.quartz.entity.SysJobLog;
  25. import net.yyc.quartz.event.SysJobLogEvent;
  26. import net.yyc.quartz.service.SysJobService;
  27. import org.aspectj.lang.annotation.Aspect;
  28. import org.quartz.CronTrigger;
  29. import org.quartz.Trigger;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.context.ApplicationEventPublisher;
  32. import java.time.ZoneId;
  33. /**
  34. * 定时任务反射工具类
  35. *
  36. * @author 郑健楠
  37. */
  38. @Slf4j
  39. @Aspect
  40. @RequiredArgsConstructor
  41. public class TaskInvokeUtil {
  42. private final ApplicationEventPublisher publisher;
  43. @Autowired
  44. private SysJobService sysJobService;
  45. @SneakyThrows
  46. public void invokeMethod(SysJob sysJob, Trigger trigger, String executionId) {
  47. long startTime = System.currentTimeMillis();
  48. SysJob updateSysjob = new SysJob();
  49. updateSysjob.setJobId(sysJob.getJobId());
  50. SysJobLog sysJobLog = new SysJobLog();
  51. sysJobLog.setUuid(executionId);
  52. sysJobLog.setJobId(sysJob.getJobId());
  53. sysJobLog.setJobName(sysJob.getJobName());
  54. sysJobLog.setJobGroup(sysJob.getJobGroup());
  55. sysJobLog.setJobOrder(sysJob.getJobOrder());
  56. sysJobLog.setJobType(sysJob.getJobType());
  57. sysJobLog.setExecutePath(sysJob.getExecutePath());
  58. sysJobLog.setClassName(sysJob.getClassName());
  59. sysJobLog.setMethodName(sysJob.getMethodName());
  60. sysJobLog.setMethodParamsValue(sysJob.getMethodParamsValue());
  61. sysJobLog.setCronExpression(sysJob.getCronExpression());
  62. sysJobLog.setTenantId(sysJob.getTenantId());
  63. try {
  64. // 执行任务
  65. ITaskInvoke iTaskInvoke = TaskInvokeFactory.getInvoker(sysJob.getJobType());
  66. // 设置uuid
  67. sysJob.setUuid(executionId);
  68. // 直接执行任务
  69. iTaskInvoke.invokeMethod(sysJob);
  70. // 记录成功状态
  71. sysJobLog.setJobMessage(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getDescription());
  72. sysJobLog.setJobLogStatus(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
  73. // 任务表信息更新
  74. updateSysjob.setJobExecuteStatus(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
  75. } catch (Throwable e) {
  76. log.error("[QUARTZ] [{}] 任务失败 | jobId={} jobName={} error={}",
  77. executionId, sysJob.getJobId(), sysJob.getJobName(), e.getMessage(), e);
  78. // 记录失败状态
  79. sysJobLog.setJobMessage(QuartzEnum.JOB_LOG_STATUS_FAIL.getDescription());
  80. sysJobLog.setJobLogStatus(QuartzEnum.JOB_LOG_STATUS_FAIL.getType());
  81. sysJobLog.setExceptionInfo(StrUtil.sub(e.getMessage(), 0, 2000));
  82. // 任务表信息更新
  83. updateSysjob.setJobExecuteStatus(QuartzEnum.JOB_LOG_STATUS_FAIL.getType());
  84. } finally {
  85. if (trigger instanceof CronTrigger) {
  86. updateSysjob.setStartTime(
  87. trigger.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
  88. updateSysjob.setPreviousTime(
  89. trigger.getPreviousFireTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
  90. updateSysjob.setNextTime(
  91. trigger.getNextFireTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
  92. }
  93. // 记录执行时长
  94. long endTime = System.currentTimeMillis();
  95. sysJobLog.setExecuteTime(String.valueOf(endTime - startTime));
  96. if (QuartzEnum.JOB_LOG_STATUS_SUCCESS.getType().equals(sysJobLog.getJobLogStatus())) {
  97. log.info("[QUARTZ] [{}] 任务完成 | jobId={} jobName={} 耗时={}ms",
  98. executionId, sysJob.getJobId(), sysJob.getJobName(), endTime - startTime);
  99. }
  100. publisher.publishEvent(new SysJobLogEvent(sysJobLog));
  101. sysJobService.updateById(updateSysjob);
  102. }
  103. }
  104. }