Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
db1a20a8
Commit
db1a20a8
authored
Dec 24, 2021
by
Zheng Jie
Browse files
定时任务优化,默认不打印SQL日志
parent
ed73ee6c
Changes
5
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/quartz/config/JobRunner.java
View file @
db1a20a8
...
...
@@ -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
(
"--------------------定时任务注入完成------------------"
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/TestTask.java
View file @
db1a20a8
...
...
@@ -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
{
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/ExecutionJob.java
View file @
db1a20a8
...
...
@@ -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
();
}
}
...
...
eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/QuartzRunnable.java
View file @
db1a20a8
...
...
@@ -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
))
{
...
...
eladmin-system/src/main/resources/logback.xml
View file @
db1a20a8
...
...
@@ -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>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment