TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
一、配置jdbc.properties添加:- #------------ Task ------------
) u; U( A% Q! r8 T0 v( d. Z+ A6 z - task.core_pool_size=5" o( M9 T/ m; m4 @$ f& E
- task.max_pool_size=505 ~/ z1 r. u' r' t
- task.queue_capacity=1000
; e% B+ H% i/ I R: e - task.keep_alive_seconds=60
复制代码 1 x0 L% B+ f. ^) B% \) a. N9 x( m
二、配置spring的applicationContext.xml添加:9 a5 n p0 |8 G% B& k+ c, b
- <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
# _- n& ` S. E( f' h3 q$ [% ]% m - <!-- 核心线程数 -->
6 M4 c3 z! f5 _8 j3 i) c - <property name="corePoolSize" value="${task.core_pool_size}" />
% |; n* o; G, ^" x - <!-- 最大线程数 -->
$ \3 {' X3 f7 j9 |% p2 b' i - <property name="maxPoolSize" value="${task.max_pool_size}" />
1 S2 o; G3 N) i# q6 s, A* H& k - <!-- 队列最大长度 -->
0 a0 b2 x; H, @1 q7 j+ v& v% X - <property name="queueCapacity" value="${task.queue_capacity}" />
* Z; i1 \5 l4 V/ v9 Q' G& ? - <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
5 ?" c$ T2 \7 I+ K+ ` - <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />6 h8 H9 ~% E4 c; K1 Q3 i! z5 o
- </bean>
/ E. O2 p" G( I8 f# x9 V - <!-- 注解式 -->
* y/ ^! t9 v9 L6 e! E% B3 O - <task:annotation-driven />
复制代码 三、在实现层Impl声明并注入:" B, \; d% y" E- Q/ v, |% d
- @Resource(name = "taskExecutor")4 G) j. y5 e0 l# r n% ~
- private TaskExecutor taskExecutor;
复制代码
( g2 e# h$ g% T- F/ e四、在方法里调用:7 p+ u% g+ W2 w0 W, g+ c- S$ b! P
- try {
3 D( l6 T# k: u9 x - taskExecutor.execute(new Runnable() {
) {1 `, E& G- ?5 A; y - public void run() {
" l4 h( q ~' E9 N - //这里编写处理业务代码
& S8 f( f% w& [, R3 C - 7 G% g5 C- H& W' q6 X# U# I4 I
- }" w5 G. {& @; R
- });
, Y; r. E6 @. J: H% T - } catch (Exception e) {$ j9 ?! L' y4 |7 A6 C9 E6 I9 Q
- e.printStackTrace();( R1 V. ], z! i2 Y
- }
复制代码 0 Y5 a3 s3 P9 i9 O7 d
/ u. C; |; W) P$ }( E |
|