Skip to content

Commit

Permalink
定时任务优化,默认不打印SQL日志
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Dec 24, 2021
1 parent ed73ee6 commit db1a20a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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("--------------------定时任务注入完成------------------");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -24,6 +25,7 @@
* @date 2019-01-08
*/
@Slf4j
@Async
@Component
public class TestTask {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand All @@ -31,6 +30,8 @@
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.*;
Expand All @@ -44,17 +45,19 @@
@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();
Expand All @@ -66,11 +69,8 @@ public void executeInternal(JobExecutionContext context) {
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);
Expand All @@ -79,8 +79,7 @@ public void executeInternal(JobExecutionContext context) {
}
// 任务状态
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("[,,]");
Expand All @@ -91,8 +90,7 @@ public void executeInternal(JobExecutionContext context) {
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:失败
Expand All @@ -114,6 +112,7 @@ public void executeInternal(JobExecutionContext context) {
}
} finally {
quartzLogRepository.save(log);
executor.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions eladmin-system/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down

0 comments on commit db1a20a

Please sign in to comment.