Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fgqyxxlr
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
yaru
fgqyxxlr
Commits
f04c1fed
Commit
f04c1fed
authored
Dec 29, 2018
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
线程池代码优化
parent
383d0821
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
82 additions
and
22 deletions
+82
-22
README.md
+1
-3
ruoyi-common/src/main/java/com/ruoyi/common/utils/Threads.java
+62
-0
ruoyi-framework/src/main/java/com/ruoyi/framework/manager/AsyncManager.java
+6
-3
ruoyi-framework/src/main/java/com/ruoyi/framework/manager/ShutdownManager.java
+11
-7
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/session/SpringSessionValidationScheduler.java
+2
-9
No files found.
README.md
View file @
f04c1fed
...
...
@@ -8,9 +8,7 @@
> RuoYi从3.0开始,进行模块拆分,将原先的单应用转变为多模块,如需单应用,请移步 [RuoYi-fast](https://gitee.com/y_project/RuoYi-fast)
> 推荐使用阿里云部署,通用云产品代金券 :[点我领取](https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=brki8iof)
> 阿里云双12活动低至2折 :[点我购买](https://m.aliyun.com/act/team1212?params=N.DTB4ZxQhX0#/)
> 推荐使用阿里云部署,通用云产品代金券 :[点我领取](https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=brki8iof)
## 内置功能
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/Threads.java
0 → 100644
View file @
f04c1fed
package
com
.
ruoyi
.
common
.
utils
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.TimeUnit
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* 线程相关工具类.
*
* @author ruoyi
*/
public
class
Threads
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
"sys-user"
);
/**
* sleep等待,单位为毫秒
*/
public
static
void
sleep
(
long
milliseconds
)
{
try
{
Thread
.
sleep
(
milliseconds
);
}
catch
(
InterruptedException
e
)
{
return
;
}
}
/**
* 停止线程池
* 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务.
* 如果超时, 则调用shutdownNow, 取消在workQueue中Pending的任务,并中断所有阻塞函数.
* 如果仍人超時,則強制退出.
* 另对在shutdown时线程本身被调用中断做了处理.
*/
public
static
void
shutdownAndAwaitTermination
(
ExecutorService
pool
)
{
if
(
pool
!=
null
&&
!
pool
.
isShutdown
())
{
pool
.
shutdown
();
try
{
if
(!
pool
.
awaitTermination
(
120
,
TimeUnit
.
SECONDS
))
{
pool
.
shutdownNow
();
if
(!
pool
.
awaitTermination
(
120
,
TimeUnit
.
SECONDS
))
{
logger
.
info
(
"Pool did not terminate"
);
}
}
}
catch
(
InterruptedException
ie
)
{
pool
.
shutdownNow
();
Thread
.
currentThread
().
interrupt
();
}
}
}
}
ruoyi-framework/src/main/java/com/ruoyi/framework/manager/AsyncManager.java
View file @
f04c1fed
...
...
@@ -3,6 +3,7 @@ package com.ruoyi.framework.manager;
import
java.util.TimerTask
;
import
java.util.concurrent.ScheduledThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
com.ruoyi.common.utils.Threads
;
/**
* 异步任务管理器
...
...
@@ -41,9 +42,11 @@ public class AsyncManager
executor
.
schedule
(
task
,
OPERATE_DELAY_TIME
,
TimeUnit
.
MILLISECONDS
);
}
public
void
shutdown
(
long
timeout
,
TimeUnit
unit
)
throws
Exception
/**
* 停止任务线程池
*/
public
void
shutdown
()
{
executor
.
shutdown
();
executor
.
awaitTermination
(
timeout
,
unit
);
Threads
.
shutdownAndAwaitTermination
(
executor
);
}
}
ruoyi-framework/src/main/java/com/ruoyi/framework/manager/ShutdownManager.java
View file @
f04c1fed
...
...
@@ -6,13 +6,11 @@ import org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PreDestroy
;
import
java.util.concurrent.TimeUnit
;
/**
*
ShutdownManager 类
*
确保应用退出时能关闭后台线程
*
* @Auther: cj
* @Date: 2018/12/28
* @author cj
*/
@Component
public
class
ShutdownManager
...
...
@@ -29,13 +27,16 @@ public class ShutdownManager
shutdownAsyncManager
();
}
/**
* 停止Seesion会话检查
*/
private
void
shutdownSpringSessionValidationScheduler
()
{
if
(
springSessionValidationScheduler
!=
null
&&
springSessionValidationScheduler
.
isEnabled
())
{
try
{
logger
.
info
(
"
关闭会话验证任务
"
);
logger
.
info
(
"
====关闭会话验证任务====
"
);
springSessionValidationScheduler
.
disableSessionValidation
();
}
catch
(
Exception
e
)
...
...
@@ -45,12 +46,15 @@ public class ShutdownManager
}
}
/**
* 停止异步执行任务
*/
private
void
shutdownAsyncManager
()
{
try
{
logger
.
info
(
"
关闭后台任务线程池
"
);
AsyncManager
.
me
().
shutdown
(
10
,
TimeUnit
.
SECONDS
);
logger
.
info
(
"
====关闭后台任务任务线程池====
"
);
AsyncManager
.
me
().
shutdown
();
}
catch
(
Exception
e
)
{
...
...
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/session/SpringSessionValidationScheduler.java
View file @
f04c1fed
...
...
@@ -8,6 +8,7 @@ import org.apache.shiro.session.mgt.SessionValidationScheduler;
import
org.apache.shiro.session.mgt.ValidatingSessionManager
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.ruoyi.common.utils.Threads
;
/**
* 自定义任务调度器完成
...
...
@@ -136,15 +137,7 @@ public class SpringSessionValidationScheduler implements SessionValidationSchedu
if
(
this
.
enabled
)
{
executorService
.
shutdown
();
try
{
executorService
.
awaitTermination
(
10
,
TimeUnit
.
SECONDS
);
}
catch
(
InterruptedException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
Threads
.
shutdownAndAwaitTermination
(
executorService
);
}
this
.
enabled
=
false
;
}
...
...
Write
Preview
Markdown
is supported
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