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
255a3254
Commit
255a3254
authored
Jul 04, 2022
by
Zheng Jie
Browse files
[代码完善](master): 代码优化
close
https://github.com/elunez/eladmin/issues/752
parent
64a3ff7b
Changes
5
Hide whitespace changes
Inline
Side-by-side
eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java
View file @
255a3254
/*
/*
* Copyright 2019-2020 Zheng Jie
* Copyright 2019-2020 Zheng Jie
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
...
...
eladmin-system/src/main/java/me/zhengjie/config/thread/AsyncTaskExecutePool.java
View file @
255a3254
...
@@ -43,8 +43,8 @@ public class AsyncTaskExecutePool implements AsyncConfigurer {
...
@@ -43,8 +43,8 @@ public class AsyncTaskExecutePool implements AsyncConfigurer {
executor
.
setQueueCapacity
(
AsyncTaskProperties
.
queueCapacity
);
executor
.
setQueueCapacity
(
AsyncTaskProperties
.
queueCapacity
);
//活跃时间
//活跃时间
executor
.
setKeepAliveSeconds
(
AsyncTaskProperties
.
keepAliveSeconds
);
executor
.
setKeepAliveSeconds
(
AsyncTaskProperties
.
keepAliveSeconds
);
//线程
名字前缀
//线程
工厂
executor
.
setThread
NamePrefix
(
"el-async
-
"
);
executor
.
setThread
Factory
(
new
TheadFactoryName
(
"el-async"
)
);
// setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
// setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
// CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
// CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
executor
.
setRejectedExecutionHandler
(
new
ThreadPoolExecutor
.
CallerRunsPolicy
());
executor
.
setRejectedExecutionHandler
(
new
ThreadPoolExecutor
.
CallerRunsPolicy
());
...
...
eladmin-system/src/main/java/me/zhengjie/config/thread/TheadFactoryName.java
View file @
255a3254
...
@@ -15,8 +15,8 @@
...
@@ -15,8 +15,8 @@
*/
*/
package
me.zhengjie.config.thread
;
package
me.zhengjie.config.thread
;
import
me.zhengjie.utils.StringUtils
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.atomic.AtomicInteger
;
...
@@ -33,24 +33,25 @@ public class TheadFactoryName implements ThreadFactory {
...
@@ -33,24 +33,25 @@ public class TheadFactoryName implements ThreadFactory {
private
final
AtomicInteger
threadNumber
=
new
AtomicInteger
(
1
);
private
final
AtomicInteger
threadNumber
=
new
AtomicInteger
(
1
);
private
final
String
namePrefix
;
private
final
String
namePrefix
;
private
final
static
String
DEF_NAME
=
"el-pool-"
;
public
TheadFactoryName
()
{
public
TheadFactoryName
()
{
this
(
"el-pool"
);
this
(
DEF_NAME
);
}
}
p
rivate
TheadFactoryName
(
String
name
){
p
ublic
TheadFactoryName
(
String
name
){
SecurityManager
s
=
System
.
getSecurityManager
();
SecurityManager
s
=
System
.
getSecurityManager
();
group
=
(
s
!=
null
)
?
s
.
getThreadGroup
()
:
group
=
(
s
!=
null
)
?
s
.
getThreadGroup
()
:
Thread
.
currentThread
().
getThreadGroup
();
Thread
.
currentThread
().
getThreadGroup
();
//此时namePrefix就是 name + 第几个用这个工厂创建线程池的
//此时namePrefix就是 name + 第几个用这个工厂创建线程池的
this
.
namePrefix
=
name
+
this
.
namePrefix
=
(
StringUtils
.
isNotBlank
(
name
)
?
name
:
DEF_NAME
)
+
"-"
+
POOL_NUMBER
.
getAndIncrement
();
POOL_NUMBER
.
getAndIncrement
();
}
}
@Override
@Override
public
Thread
newThread
(
Runnable
r
)
{
public
Thread
newThread
(
Runnable
r
)
{
//此时线程的名字 就是 namePrefix + -
thread
- + 这个线程池中第几个执行的线程
//此时线程的名字 就是 namePrefix + -
exec
- + 这个线程池中第几个执行的线程
Thread
t
=
new
Thread
(
group
,
r
,
Thread
t
=
new
Thread
(
group
,
r
,
namePrefix
+
"-
thread
-"
+
threadNumber
.
getAndIncrement
(),
namePrefix
+
"-
exec
-"
+
threadNumber
.
getAndIncrement
(),
0
);
0
);
if
(
t
.
isDaemon
())
{
if
(
t
.
isDaemon
())
{
t
.
setDaemon
(
false
);
t
.
setDaemon
(
false
);
...
...
eladmin-system/src/main/java/me/zhengjie/config/thread/ThreadPoolExecutorUtil.java
View file @
255a3254
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
me.zhengjie.config.thread
;
package
me.zhengjie.config.thread
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
...
@@ -26,14 +27,20 @@ import java.util.concurrent.TimeUnit;
...
@@ -26,14 +27,20 @@ import java.util.concurrent.TimeUnit;
*/
*/
public
class
ThreadPoolExecutorUtil
{
public
class
ThreadPoolExecutorUtil
{
public
static
ThreadPoolExecutor
getPoll
(){
public
static
ExecutorService
getPoll
(){
return
getPoll
(
null
);
}
public
static
ExecutorService
getPoll
(
String
threadName
){
return
new
ThreadPoolExecutor
(
return
new
ThreadPoolExecutor
(
AsyncTaskProperties
.
corePoolSize
,
AsyncTaskProperties
.
corePoolSize
,
AsyncTaskProperties
.
maxPoolSize
,
AsyncTaskProperties
.
maxPoolSize
,
AsyncTaskProperties
.
keepAliveSeconds
,
AsyncTaskProperties
.
keepAliveSeconds
,
TimeUnit
.
SECONDS
,
TimeUnit
.
SECONDS
,
new
ArrayBlockingQueue
<>(
AsyncTaskProperties
.
queueCapacity
),
new
ArrayBlockingQueue
<>(
AsyncTaskProperties
.
queueCapacity
),
new
TheadFactoryName
()
new
TheadFactoryName
(
threadName
),
// 队列与线程池中线程都满了时使用调用者所在的线程来执行
new
ThreadPoolExecutor
.
CallerRunsPolicy
()
);
);
}
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/ExecutionJob.java
View file @
255a3254
...
@@ -19,6 +19,7 @@ import cn.hutool.extra.template.Template;
...
@@ -19,6 +19,7 @@ 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
;
...
@@ -32,7 +33,6 @@ import me.zhengjie.utils.ThrowableUtil;
...
@@ -32,7 +33,6 @@ import me.zhengjie.utils.ThrowableUtil;
import
org.quartz.JobExecutionContext
;
import
org.quartz.JobExecutionContext
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.scheduling.quartz.QuartzJobBean
;
import
org.springframework.scheduling.quartz.QuartzJobBean
;
import
java.util.*
;
import
java.util.*
;
import
java.util.concurrent.*
;
import
java.util.concurrent.*
;
...
@@ -42,15 +42,15 @@ import java.util.concurrent.*;
...
@@ -42,15 +42,15 @@ import java.util.concurrent.*;
* @author /
* @author /
* @date 2019-01-07
* @date 2019-01-07
*/
*/
@Async
public
class
ExecutionJob
extends
QuartzJobBean
{
public
class
ExecutionJob
extends
QuartzJobBean
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
// 此处仅供参考,可根据任务执行情况自定义线程池参数
private
final
static
ExecutorService
executor
=
ThreadPoolExecutorUtil
.
getPoll
(
"el-quartz-job"
);
@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
...
@@ -112,7 +112,6 @@ public class ExecutionJob extends QuartzJobBean {
...
@@ -112,7 +112,6 @@ public class ExecutionJob extends QuartzJobBean {
}
}
}
finally
{
}
finally
{
quartzLogRepository
.
save
(
log
);
quartzLogRepository
.
save
(
log
);
executor
.
shutdown
();
}
}
}
}
...
...
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