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
21b30b1b
Commit
21b30b1b
authored
Jun 08, 2023
by
Zheng Jie
Browse files
代码优化,自定义线程池优化
parent
64e608b8
Changes
4
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/config/thread/
AsyncTaskExecutePool
.java
→
eladmin-system/src/main/java/me/zhengjie/config/thread/
CustomExecutorConfig
.java
View file @
21b30b1b
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
me.zhengjie.config.thread
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.
scheduling
.annotation.
AsyncConfigurer
;
import
org.springframework.
context
.annotation.
Primary
;
import
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
;
import
java.util.concurrent.Executor
;
import
java.util.concurrent.ThreadPoolExecutor
;
/**
*
异步任务线程池装配类
* @author
https://juejin.im/entry/5abb8f6951882555677e9da2
* @d
ate 2019年10月31日15:06:18
*
/
@Slf4j
*
创建自定义的线程池
* @author
Zheng Jie
* @d
escription
*
@date 2023-06-08
**/
@Configuration
public
class
AsyncTaskExecutePool
implements
Async
Config
urer
{
public
class
CustomExecutor
Config
{
@Override
public
Executor
getAsyncExecutor
()
{
/**
* 自定义线程池,用法 @Async
* @return Executor
*/
@Bean
@Primary
public
Executor
elAsync
()
{
ThreadPoolTaskExecutor
executor
=
new
ThreadPoolTaskExecutor
();
//核心线程池大小
executor
.
setCorePoolSize
(
AsyncTaskProperties
.
corePoolSize
);
//最大线程数
executor
.
setMaxPoolSize
(
AsyncTaskProperties
.
maxPoolSize
);
//队列容量
executor
.
setQueueCapacity
(
AsyncTaskProperties
.
queueCapacity
);
//活跃时间
executor
.
setThreadNamePrefix
(
"el-async-"
);
executor
.
setKeepAliveSeconds
(
AsyncTaskProperties
.
keepAliveSeconds
);
//线程工厂
executor
.
setThreadFactory
(
new
TheadFactoryName
(
"el-async"
));
// setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
// CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
executor
.
setRejectedExecutionHandler
(
new
ThreadPoolExecutor
.
CallerRunsPolicy
());
executor
.
initialize
();
return
executor
;
}
@Override
public
AsyncUncaughtExceptionHandler
getAsyncUncaughtExceptionHandler
()
{
return
(
throwable
,
method
,
objects
)
->
{
log
.
error
(
"===="
+
throwable
.
getMessage
()+
"===="
,
throwable
);
log
.
error
(
"exception method:"
+
method
.
getName
());
};
/**
* 自定义线程池,用法 @Async("otherAsync")
* @return Executor
*/
@Bean
public
Executor
otherAsync
()
{
ThreadPoolTaskExecutor
executor
=
new
ThreadPoolTaskExecutor
();
executor
.
setCorePoolSize
(
15
);
executor
.
setQueueCapacity
(
50
);
executor
.
setKeepAliveSeconds
(
AsyncTaskProperties
.
keepAliveSeconds
);
executor
.
setThreadNamePrefix
(
"el-task-"
);
executor
.
setRejectedExecutionHandler
(
new
ThreadPoolExecutor
.
CallerRunsPolicy
());
executor
.
initialize
();
return
executor
;
}
}
eladmin-system/src/main/java/me/zhengjie/config/thread/TheadFactoryName.java
deleted
100644 → 0
View file @
64e608b8
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
me.zhengjie.config.thread
;
import
me.zhengjie.utils.StringUtils
;
import
org.springframework.stereotype.Component
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
* 自定义线程名称
* @author Zheng Jie
* @date 2019年10月31日17:49:55
*/
@Component
public
class
TheadFactoryName
implements
ThreadFactory
{
private
static
final
AtomicInteger
POOL_NUMBER
=
new
AtomicInteger
(
1
);
private
final
ThreadGroup
group
;
private
final
AtomicInteger
threadNumber
=
new
AtomicInteger
(
1
);
private
final
String
namePrefix
;
private
final
static
String
DEF_NAME
=
"el-pool-"
;
public
TheadFactoryName
()
{
this
(
DEF_NAME
);
}
public
TheadFactoryName
(
String
name
){
SecurityManager
s
=
System
.
getSecurityManager
();
group
=
(
s
!=
null
)
?
s
.
getThreadGroup
()
:
Thread
.
currentThread
().
getThreadGroup
();
//此时namePrefix就是 name + 第几个用这个工厂创建线程池的
this
.
namePrefix
=
(
StringUtils
.
isNotBlank
(
name
)
?
name
:
DEF_NAME
)
+
"-"
+
POOL_NUMBER
.
getAndIncrement
();
}
@Override
public
Thread
newThread
(
Runnable
r
)
{
//此时线程的名字 就是 namePrefix + -exec- + 这个线程池中第几个执行的线程
Thread
t
=
new
Thread
(
group
,
r
,
namePrefix
+
"-exec-"
+
threadNumber
.
getAndIncrement
(),
0
);
if
(
t
.
isDaemon
())
{
t
.
setDaemon
(
false
);
}
if
(
t
.
getPriority
()
!=
Thread
.
NORM_PRIORITY
)
{
t
.
setPriority
(
Thread
.
NORM_PRIORITY
);
}
return
t
;
}
}
eladmin-system/src/main/java/me/zhengjie/config/thread/ThreadPoolExecutorUtil.java
deleted
100644 → 0
View file @
64e608b8
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
me.zhengjie.config.thread
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
/**
* 用于获取自定义线程池
* @author Zheng Jie
* @date 2019年10月31日18:16:47
*/
public
class
ThreadPoolExecutorUtil
{
public
static
ExecutorService
getPoll
(){
return
getPoll
(
null
);
}
public
static
ExecutorService
getPoll
(
String
threadName
){
return
new
ThreadPoolExecutor
(
AsyncTaskProperties
.
corePoolSize
,
AsyncTaskProperties
.
maxPoolSize
,
AsyncTaskProperties
.
keepAliveSeconds
,
TimeUnit
.
SECONDS
,
new
ArrayBlockingQueue
<>(
AsyncTaskProperties
.
queueCapacity
),
new
TheadFactoryName
(
threadName
),
// 队列与线程池中线程都满了时使用调用者所在的线程来执行
new
ThreadPoolExecutor
.
CallerRunsPolicy
()
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/ExecutionJob.java
View file @
21b30b1b
...
...
@@ -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
;
...
...
@@ -33,12 +32,13 @@ import me.zhengjie.utils.ThrowableUtil;
import
org.quartz.JobExecutionContext
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
;
import
org.springframework.scheduling.quartz.QuartzJobBean
;
import
java.util.*
;
import
java.util.concurrent.*
;
/**
* 参考人人开源,https://gitee.com/renrenio/renren-security
* 参考人人开源,
<a href="
https://gitee.com/renrenio/renren-security
">...</a>
* @author /
* @date 2019-01-07
*/
...
...
@@ -47,7 +47,7 @@ public class ExecutionJob extends QuartzJobBean {
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
// 此处仅供参考,可根据任务执行情况自定义线程池参数
private
final
static
Executor
Service
executor
=
ThreadPoolExecutorUtil
.
getPoll
(
"el-quartz-job
"
);
private
final
ThreadPoolTask
Executor
executor
=
SpringContextHolder
.
getBean
(
"elAsync
"
);
@Override
public
void
executeInternal
(
JobExecutionContext
context
)
{
...
...
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