定时任务优化,默认不打印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 @Override
public void run(ApplicationArguments applicationArguments) { public void run(ApplicationArguments applicationArguments) {
log.info("--------------------注入定时任务---------------------"); log.info("--------------------注入系统定时任务------------------");
List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse(); List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse();
quartzJobs.forEach(quartzManage::addJob); quartzJobs.forEach(quartzManage::addJob);
log.info("--------------------定时任务注入完成---------------------"); log.info("--------------------定时任务注入完成------------------");
} }
} }

View File

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

View File

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

View File

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