
com.taotao.boot.job.quartz.execution.AbstractQuartzExecutionJob Maven / Gradle / Ivy
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.boot.job.quartz.execution;
import com.google.common.base.Stopwatch;
import com.taotao.boot.common.utils.context.ContextUtils;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.job.quartz.entity.QuartzTask;
import com.taotao.boot.job.quartz.entity.QuartzTaskLog;
import com.taotao.boot.job.quartz.utils.QuartzLogRecord;
import org.apache.commons.lang3.StringUtils;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* 文摘石英执行工作
*
* @author shuigedeng
* @version 2022.09
* @since 2022-09-06 09:03:37
*/
public abstract class AbstractQuartzExecutionJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) {
QuartzTask quartzTask = (QuartzTask) context.getMergedJobDataMap().get(QuartzTask.JOB_KEY);
QuartzTaskLog quartzTaskLog = new QuartzTaskLog();
quartzTaskLog.setJobName(quartzTask.getJobName());
quartzTaskLog.setBeanName(quartzTask.getBeanName());
quartzTaskLog.setMethodName(quartzTask.getMethodName());
quartzTaskLog.setParams(quartzTask.getParams());
quartzTaskLog.setCronExpression(quartzTask.getCronExpression());
quartzTaskLog.setExecutionThread(Thread.currentThread().getName());
quartzTaskLog.setStartTime(LocalDateTime.now());
Stopwatch sw = Stopwatch.createStarted();
try {
LogUtils.info("准备执行任务,任务ID:{}", quartzTask.getId());
// 执行任务
doExecute(quartzTask);
long seconds = sw.stop().elapsed(TimeUnit.SECONDS);
quartzTaskLog.setTime(seconds);
quartzTaskLog.setSuccess(true);
quartzTaskLog.setEndTime(LocalDateTime.now());
LogUtils.info("任务执行完毕,任务名称:{} 总共耗时:{} 秒", quartzTask.getJobName(), seconds);
} catch (Exception e) {
LogUtils.error("任务执行失败,任务名称:{}" + quartzTask.getJobName(), e);
long seconds = sw.stop().elapsed(TimeUnit.SECONDS);
quartzTaskLog.setTime(seconds);
quartzTaskLog.setSuccess(false);
quartzTaskLog.setExceptionDetail(LogUtils.getStackTrace(e));
quartzTaskLog.setEndTime(LocalDateTime.now());
// todo 更新数据状态
// quartzJob.setPause(false);
// quartzJobService.updateIsPause(quartzJobModel);
} finally {
// todo 添加日志
// quartzLogService.save(log);
}
// 获取spring bean
QuartzLogRecord quartzLogService = ContextUtils.getBean(QuartzLogRecord.class, false);
if (quartzLogService != null) {
quartzLogService.addLog(quartzTaskLog);
}
}
/** 执行spring bean方法 */
protected void doExecute(QuartzTask scheduleJob) throws Exception {
String methodName = scheduleJob.getMethodName();
String params = scheduleJob.getParams();
Object target = ContextUtils.getBean(scheduleJob.getBeanName(), true);
if (Objects.isNull(target)) {
throw new RuntimeException("未找到bean");
}
Method method;
if (StringUtils.isNotBlank(params)) {
method = target.getClass().getDeclaredMethod(methodName, String.class);
} else {
method = target.getClass().getDeclaredMethod(methodName);
}
ReflectionUtils.makeAccessible(method);
if (StringUtils.isNotBlank(params)) {
method.invoke(target, params);
} else {
method.invoke(target);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy