| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /*
- * Copyright (c) 2018-2025, hnqz All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * Neither the name of the pig4cloud.com developer nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- * Author: hnqz
- */
- package net.yyc.quartz.util;
- import cn.hutool.core.util.StrUtil;
- import lombok.RequiredArgsConstructor;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import net.yyc.quartz.constants.QuartzEnum;
- import net.yyc.quartz.entity.SysJob;
- import net.yyc.quartz.entity.SysJobLog;
- import net.yyc.quartz.event.SysJobLogEvent;
- import net.yyc.quartz.service.SysJobService;
- import org.aspectj.lang.annotation.Aspect;
- import org.quartz.CronTrigger;
- import org.quartz.Trigger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationEventPublisher;
- import java.time.ZoneId;
- /**
- * 定时任务反射工具类
- *
- * @author 郑健楠
- */
- @Slf4j
- @Aspect
- @RequiredArgsConstructor
- public class TaskInvokeUtil {
- private final ApplicationEventPublisher publisher;
- @Autowired
- private SysJobService sysJobService;
- @SneakyThrows
- public void invokeMethod(SysJob sysJob, Trigger trigger, String executionId) {
- long startTime = System.currentTimeMillis();
- SysJob updateSysjob = new SysJob();
- updateSysjob.setJobId(sysJob.getJobId());
- SysJobLog sysJobLog = new SysJobLog();
- sysJobLog.setUuid(executionId);
- sysJobLog.setJobId(sysJob.getJobId());
- sysJobLog.setJobName(sysJob.getJobName());
- sysJobLog.setJobGroup(sysJob.getJobGroup());
- sysJobLog.setJobOrder(sysJob.getJobOrder());
- sysJobLog.setJobType(sysJob.getJobType());
- sysJobLog.setExecutePath(sysJob.getExecutePath());
- sysJobLog.setClassName(sysJob.getClassName());
- sysJobLog.setMethodName(sysJob.getMethodName());
- sysJobLog.setMethodParamsValue(sysJob.getMethodParamsValue());
- sysJobLog.setCronExpression(sysJob.getCronExpression());
- sysJobLog.setTenantId(sysJob.getTenantId());
- try {
- // 执行任务
- ITaskInvoke iTaskInvoke = TaskInvokeFactory.getInvoker(sysJob.getJobType());
- // 设置uuid
- sysJob.setUuid(executionId);
- // 直接执行任务
- iTaskInvoke.invokeMethod(sysJob);
- // 记录成功状态
- sysJobLog.setJobMessage(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getDescription());
- sysJobLog.setJobLogStatus(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
- // 任务表信息更新
- updateSysjob.setJobExecuteStatus(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());
- } catch (Throwable e) {
- log.error("[QUARTZ] [{}] 任务失败 | jobId={} jobName={} error={}",
- executionId, sysJob.getJobId(), sysJob.getJobName(), e.getMessage(), e);
- // 记录失败状态
- sysJobLog.setJobMessage(QuartzEnum.JOB_LOG_STATUS_FAIL.getDescription());
- sysJobLog.setJobLogStatus(QuartzEnum.JOB_LOG_STATUS_FAIL.getType());
- sysJobLog.setExceptionInfo(StrUtil.sub(e.getMessage(), 0, 2000));
- // 任务表信息更新
- updateSysjob.setJobExecuteStatus(QuartzEnum.JOB_LOG_STATUS_FAIL.getType());
- } finally {
- if (trigger instanceof CronTrigger) {
- updateSysjob.setStartTime(
- trigger.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
- updateSysjob.setPreviousTime(
- trigger.getPreviousFireTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
- updateSysjob.setNextTime(
- trigger.getNextFireTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
- }
- // 记录执行时长
- long endTime = System.currentTimeMillis();
- sysJobLog.setExecuteTime(String.valueOf(endTime - startTime));
- if (QuartzEnum.JOB_LOG_STATUS_SUCCESS.getType().equals(sysJobLog.getJobLogStatus())) {
- log.info("[QUARTZ] [{}] 任务完成 | jobId={} jobName={} 耗时={}ms",
- executionId, sysJob.getJobId(), sysJob.getJobName(), endTime - startTime);
- }
- publisher.publishEvent(new SysJobLogEvent(sysJobLog));
- sysJobService.updateById(updateSysjob);
- }
- }
- }
|