Skip to content

Commit e8c0ef8

Browse files
committed
ThreadPoolTaskScheduler allows for setPoolSize changes at runtime
Also exposing "getPoolSize()" and "getActiveCount()" from ThreadPoolTaskScheduler now, analogous to ThreadPoolTaskExecutor. Issue: SPR-10883
1 parent e523152 commit e8c0ef8

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -154,9 +154,7 @@ public int getKeepAliveSeconds() {
154154
* Specify whether to allow core threads to time out. This enables dynamic
155155
* growing and shrinking even in combination with a non-zero queue (since
156156
* the max pool size will only grow once the queue is full).
157-
* <p>Default is "false". Note that this feature is only available on Java 6
158-
* or above. On Java 5, consider switching to the backport-concurrent
159-
* version of ThreadPoolTaskExecutor which also supports this feature.
157+
* <p>Default is "false".
160158
* @see java.util.concurrent.ThreadPoolExecutor#allowCoreThreadTimeOut(boolean)
161159
*/
162160
public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
@@ -225,15 +223,23 @@ public ThreadPoolExecutor getThreadPoolExecutor() throws IllegalStateException {
225223
* @see java.util.concurrent.ThreadPoolExecutor#getPoolSize()
226224
*/
227225
public int getPoolSize() {
228-
return getThreadPoolExecutor().getPoolSize();
226+
if (this.threadPoolExecutor == null) {
227+
// Not initialized yet: assume core pool size.
228+
return this.corePoolSize;
229+
}
230+
return this.threadPoolExecutor.getPoolSize();
229231
}
230232

231233
/**
232234
* Return the number of currently active threads.
233235
* @see java.util.concurrent.ThreadPoolExecutor#getActiveCount()
234236
*/
235237
public int getActiveCount() {
236-
return getThreadPoolExecutor().getActiveCount();
238+
if (this.threadPoolExecutor == null) {
239+
// Not initialized yet: assume no active threads.
240+
return 0;
241+
}
242+
return this.threadPoolExecutor.getActiveCount();
237243
}
238244

239245

spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,14 +62,18 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
6262
/**
6363
* Set the ScheduledExecutorService's pool size.
6464
* Default is 1.
65+
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
6566
*/
6667
public void setPoolSize(int poolSize) {
6768
Assert.isTrue(poolSize > 0, "'poolSize' must be 1 or higher");
6869
this.poolSize = poolSize;
70+
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
71+
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setCorePoolSize(poolSize);
72+
}
6973
}
7074

7175
/**
72-
* Provide an {@link ErrorHandler} strategy.
76+
* Set a custom {@link ErrorHandler} strategy.
7377
*/
7478
public void setErrorHandler(ErrorHandler errorHandler) {
7579
Assert.notNull(errorHandler, "'errorHandler' must not be null");
@@ -111,6 +115,47 @@ public ScheduledExecutorService getScheduledExecutor() throws IllegalStateExcept
111115
return this.scheduledExecutor;
112116
}
113117

118+
/**
119+
* Return the underlying ScheduledThreadPoolExecutor, if available.
120+
* @return the underlying ScheduledExecutorService (never {@code null})
121+
* @throws IllegalStateException if the ThreadPoolTaskScheduler hasn't been initialized yet
122+
* or if the underlying ScheduledExecutorService isn't a ScheduledThreadPoolExecutor
123+
* @see #getScheduledExecutor()
124+
*/
125+
public ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() throws IllegalStateException {
126+
Assert.state(this.scheduledExecutor instanceof ScheduledThreadPoolExecutor,
127+
"No ScheduledThreadPoolExecutor available");
128+
return (ScheduledThreadPoolExecutor) this.scheduledExecutor;
129+
}
130+
131+
/**
132+
* Return the current pool size.
133+
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
134+
* @see #getScheduledThreadPoolExecutor()
135+
* @see java.util.concurrent.ScheduledThreadPoolExecutor#getPoolSize()
136+
*/
137+
public int getPoolSize() {
138+
if (this.scheduledExecutor == null) {
139+
// Not initialized yet: assume initial pool size.
140+
return this.poolSize;
141+
}
142+
return getScheduledThreadPoolExecutor().getPoolSize();
143+
}
144+
145+
/**
146+
* Return the number of currently active threads.
147+
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
148+
* @see #getScheduledThreadPoolExecutor()
149+
* @see java.util.concurrent.ScheduledThreadPoolExecutor#getActiveCount()
150+
*/
151+
public int getActiveCount() {
152+
if (this.scheduledExecutor == null) {
153+
// Not initialized yet: assume no active threads.
154+
return 0;
155+
}
156+
return getScheduledThreadPoolExecutor().getActiveCount();
157+
}
158+
114159

115160
// SchedulingTaskExecutor implementation
116161

0 commit comments

Comments
 (0)