From ccf75464534965c47866449b2b4e457a6dadede9 Mon Sep 17 00:00:00 2001
From: liriming <1343021927@qq.com>
Date: 星期一, 14 十月 2024 14:13:00 +0800
Subject: [PATCH] 定时任务权限修改

---
 shasteel-biz/src/main/java/com/iailab/module/shasteel/job/utils/ScheduleJob.java |   80 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/job/utils/ScheduleJob.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/job/utils/ScheduleJob.java
new file mode 100644
index 0000000..2ced982
--- /dev/null
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/job/utils/ScheduleJob.java
@@ -0,0 +1,80 @@
+package com.iailab.module.shasteel.job.utils;
+
+import com.iailab.framework.common.constant.Constant;
+import com.iailab.framework.common.exception.ExceptionUtils;
+import com.iailab.framework.common.util.spring.SpringContextUtils;
+import com.iailab.framework.tenant.core.context.TenantContextHolder;
+import com.iailab.module.shasteel.job.entity.ScheduleJobEntity;
+import com.iailab.module.shasteel.job.entity.ScheduleJobLogEntity;
+import com.iailab.module.shasteel.job.service.ScheduleJobLogService;
+import org.quartz.DisallowConcurrentExecution;
+import org.quartz.JobExecutionContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.quartz.QuartzJobBean;
+
+import java.lang.reflect.Method;
+import java.util.Date;
+
+
+/**
+ * 定时任务
+ *
+ * @author Mark sunlightcs@gmail.com
+ */
+@DisallowConcurrentExecution
+public class ScheduleJob extends QuartzJobBean {
+	private Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Override
+    protected void executeInternal(JobExecutionContext context) {
+        ScheduleJobEntity scheduleJob = (ScheduleJobEntity) context.getMergedJobDataMap().
+				get(ScheduleUtils.JOB_PARAM_KEY);
+
+        //设置当前租户
+		TenantContextHolder.setTenantId(scheduleJob.getTenantId());
+		Long tenantId = TenantContextHolder.getRequiredTenantId();
+
+        //数据库保存执行记录
+        ScheduleJobLogEntity log = new ScheduleJobLogEntity();
+		log.setId(System.currentTimeMillis());
+        log.setJobId(scheduleJob.getId());
+        log.setBeanName(scheduleJob.getBeanName());
+        log.setParams(scheduleJob.getParams());
+		log.setCreateDate(new Date());
+		log.setTenantId(tenantId);
+
+        //任务开始时间
+        long startTime = System.currentTimeMillis();
+        
+        try {
+			//执行任务
+			logger.info("任务准备执行,任务ID:{}", scheduleJob.getId());
+			Object target = SpringContextUtils.getBean(scheduleJob.getBeanName());
+			Method method = target.getClass().getDeclaredMethod("run", String.class);
+			method.invoke(target, scheduleJob.getParams());
+
+			//任务执行总时长
+			long times = System.currentTimeMillis() - startTime;
+			log.setTimes((int)times);
+			//任务状态
+			log.setStatus(Constant.SUCCESS);
+			
+			logger.info("任务执行完毕,任务ID:{}  总共耗时:{} 毫秒", scheduleJob.getId(), times);
+		} catch (Exception e) {
+			logger.error("任务执行失败,任务ID:{}", scheduleJob.getId(), e);
+			
+			//任务执行总时长
+			long times = System.currentTimeMillis() - startTime;
+			log.setTimes((int)times);
+			
+			//任务状态
+			log.setStatus(Constant.FAIL);
+			log.setError(ExceptionUtils.getErrorStackTrace(e));
+		}finally {
+			//获取spring bean
+			ScheduleJobLogService scheduleJobLogService = SpringContextUtils.getBean(ScheduleJobLogService.class);
+			scheduleJobLogService.insert(log);
+		}
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3