定时任务优化,默认不打印SQL日志

pull/721/head
Zheng Jie 2021-12-24 21:01:49 +08:00
parent ed73ee6cc8
commit db1a20a83c
5 changed files with 19 additions and 18 deletions

View File

@ -45,9 +45,9 @@ public class JobRunner implements ApplicationRunner {
*/
@Override
public void run(ApplicationArguments applicationArguments) {
log.info("--------------------注入定时任务---------------------");
log.info("--------------------注入系统定时任务------------------");
List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse();
quartzJobs.forEach(quartzManage::addJob);
log.info("--------------------定时任务注入完成---------------------");
log.info("--------------------定时任务注入完成------------------");
}
}

View File

@ -16,6 +16,7 @@
package me.zhengjie.modules.quartz.task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
@ -24,6 +25,7 @@ import org.springframework.stereotype.Component;
* @date 2019-01-08
*/
@Slf4j
@Async
@Component
public class TestTask {

View File

@ -19,7 +19,6 @@ import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import me.zhengjie.config.thread.ThreadPoolExecutorUtil;
import me.zhengjie.domain.vo.EmailVo;
import me.zhengjie.modules.quartz.domain.QuartzJob;
import me.zhengjie.modules.quartz.domain.QuartzLog;
@ -31,6 +30,8 @@ import me.zhengjie.utils.SpringContextHolder;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.ThrowableUtil;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.*;
@ -44,17 +45,19 @@ import java.util.concurrent.*;
@Async
public class ExecutionJob extends QuartzJobBean {
/** 该处仅供参考 */
private final static ThreadPoolExecutor EXECUTOR = ThreadPoolExecutorUtil.getPoll();
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void executeInternal(JobExecutionContext context) {
// 创建单个线程
ExecutorService executor = Executors.newSingleThreadExecutor();
// 获取任务
QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_KEY);
// 获取spring bean
QuartzLogRepository quartzLogRepository = SpringContextHolder.getBean(QuartzLogRepository.class);
QuartzJobService quartzJobService = SpringContextHolder.getBean(QuartzJobService.class);
RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class);
String uuid = quartzJob.getUuid();
QuartzLog log = new QuartzLog();
@ -66,11 +69,8 @@ public class ExecutionJob extends QuartzJobBean {
log.setCronExpression(quartzJob.getCronExpression());
try {
// 执行任务
System.out.println("--------------------------------------------------------------");
System.out.println("任务开始执行,任务名称:" + quartzJob.getJobName());
QuartzRunnable task = new QuartzRunnable(quartzJob.getBeanName(), quartzJob.getMethodName(),
quartzJob.getParams());
Future<?> future = EXECUTOR.submit(task);
QuartzRunnable task = new QuartzRunnable(quartzJob.getBeanName(), quartzJob.getMethodName(), quartzJob.getParams());
Future<?> future = executor.submit(task);
future.get();
long times = System.currentTimeMillis() - startTime;
log.setTime(times);
@ -79,8 +79,7 @@ public class ExecutionJob extends QuartzJobBean {
}
// 任务状态
log.setIsSuccess(true);
System.out.println("任务执行完毕,任务名称:" + quartzJob.getJobName() + ", 执行时间:" + times + "毫秒");
System.out.println("--------------------------------------------------------------");
logger.info("任务执行成功,任务名称:" + quartzJob.getJobName() + ", 执行时间:" + times + "毫秒");
// 判断是否存在子任务
if(StringUtils.isNotBlank(quartzJob.getSubTask())){
String[] tasks = quartzJob.getSubTask().split("[,]");
@ -91,8 +90,7 @@ public class ExecutionJob extends QuartzJobBean {
if(StringUtils.isNotBlank(uuid)) {
redisUtils.set(uuid, false);
}
System.out.println("任务执行失败,任务名称:" + quartzJob.getJobName());
System.out.println("--------------------------------------------------------------");
logger.error("任务执行失败,任务名称:" + quartzJob.getJobName());
long times = System.currentTimeMillis() - startTime;
log.setTime(times);
// 任务状态 0成功 1失败
@ -114,6 +112,7 @@ public class ExecutionJob extends QuartzJobBean {
}
} finally {
quartzLogRepository.save(log);
executor.shutdown();
}
}

View File

@ -37,7 +37,6 @@ public class QuartzRunnable implements Callable<Object> {
throws NoSuchMethodException, SecurityException {
this.target = SpringContextHolder.getBean(beanName);
this.params = params;
if (StringUtils.isNotBlank(params)) {
this.method = target.getClass().getDeclaredMethod(methodName, String.class);
} else {
@ -46,6 +45,7 @@ public class QuartzRunnable implements Callable<Object> {
}
@Override
@SuppressWarnings("all")
public Object call() throws Exception {
ReflectionUtils.makeAccessible(method);
if (StringUtils.isNotBlank(params)) {

View File

@ -17,8 +17,8 @@
<appender-ref ref="console" />
</root>
<!--监控sql日志输出 -->
<logger name="jdbc.sqlonly" level="INFO" additivity="false">
<!--监控sql日志输出,如需监控 Sql 打印,请设置为 INFO -->
<logger name="jdbc.sqlonly" level="ERROR" additivity="false">
<appender-ref ref="console" />
</logger>