TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
一、配置jdbc.properties添加:- #------------ Task ------------
1 O8 R3 R$ C* |( r: v' z: H% h$ u - task.core_pool_size=5 S/ N& y) a$ R2 U2 e
- task.max_pool_size=50
! l9 y! F7 w5 r. L$ Y1 g& a3 A - task.queue_capacity=10005 n, _- s5 D( a
- task.keep_alive_seconds=60
复制代码
$ _0 C( X) Y" @6 \8 \$ e二、配置spring的applicationContext.xml添加:+ a/ W0 n+ U# I% A- O L4 m. ^8 |
- <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">' F' s7 z" N* M1 K
- <!-- 核心线程数 --> 5 X( [$ l+ x! X- f* o q, F
- <property name="corePoolSize" value="${task.core_pool_size}" />1 Z! d% j& D5 L. W5 r {
- <!-- 最大线程数 --> , ~: E7 V: S& K: h; ]& J4 o2 n, t
- <property name="maxPoolSize" value="${task.max_pool_size}" />* |. W# p7 X; a U9 V$ W0 a! ^3 m
- <!-- 队列最大长度 -->9 N. d( C0 G/ C& T2 z6 r( U, O
- <property name="queueCapacity" value="${task.queue_capacity}" />' t9 i7 ~4 ?* b" i
- <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
: t- o' F" H2 G4 q1 h' {1 ~' y0 T - <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />& V" j4 {& l* W" b5 D I& x" \' C; {
- </bean>* J, h2 ^- _: |) ]
- <!-- 注解式 -->
( M" Z' C1 c9 [6 W - <task:annotation-driven />
复制代码 三、在实现层Impl声明并注入:
# S6 l' z) m. @1 |- @Resource(name = "taskExecutor")
E2 j2 ^, I; G - private TaskExecutor taskExecutor;
复制代码
! v/ I4 e# q3 i' D/ D四、在方法里调用:2 R* T4 d% A2 l, _5 ^, e% x
- try {; W) X, Q1 k" {$ p+ [* e
- taskExecutor.execute(new Runnable() {
, T0 [, x' c5 R - public void run() {
. g4 O7 c! @7 o8 \& d1 d1 V - //这里编写处理业务代码
o% p5 T! V; J4 ]7 ?6 ^1 B0 R: X - 2 P5 L, c& m! E0 c' V" E
- }
/ [! T8 z/ ?9 M: i/ Y; ~ - });
" _1 m1 P# I+ P+ ~/ O, q! x8 U - } catch (Exception e) {' c; _( @$ n9 B
- e.printStackTrace();
) A! s" {+ \/ N - }
复制代码
+ q, r1 j9 k6 [ M4 n& a, G1 f* d6 j1 a1 U6 W" J0 k8 z; j
|
|