|  | 
| 1 | 1 | /* | 
| 2 |  | - * Copyright 2002-2012 the original author or authors. | 
|  | 2 | + * Copyright 2002-2013 the original author or authors. | 
| 3 | 3 |  * | 
| 4 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
| 5 | 5 |  * you may not use this file except in compliance with the License. | 
| @@ -62,14 +62,18 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport | 
| 62 | 62 | 	/** | 
| 63 | 63 | 	 * Set the ScheduledExecutorService's pool size. | 
| 64 | 64 | 	 * Default is 1. | 
|  | 65 | +	 * <p><b>This setting can be modified at runtime, for example through JMX.</b> | 
| 65 | 66 | 	 */ | 
| 66 | 67 | 	public void setPoolSize(int poolSize) { | 
| 67 | 68 | 		Assert.isTrue(poolSize > 0, "'poolSize' must be 1 or higher"); | 
| 68 | 69 | 		this.poolSize = poolSize; | 
|  | 70 | +		if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { | 
|  | 71 | +			((ScheduledThreadPoolExecutor) this.scheduledExecutor).setCorePoolSize(poolSize); | 
|  | 72 | +		} | 
| 69 | 73 | 	} | 
| 70 | 74 | 
 | 
| 71 | 75 | 	/** | 
| 72 |  | -	 * Provide an {@link ErrorHandler} strategy. | 
|  | 76 | +	 * Set a custom {@link ErrorHandler} strategy. | 
| 73 | 77 | 	 */ | 
| 74 | 78 | 	public void setErrorHandler(ErrorHandler errorHandler) { | 
| 75 | 79 | 		Assert.notNull(errorHandler, "'errorHandler' must not be null"); | 
| @@ -111,6 +115,47 @@ public ScheduledExecutorService getScheduledExecutor() throws IllegalStateExcept | 
| 111 | 115 | 		return this.scheduledExecutor; | 
| 112 | 116 | 	} | 
| 113 | 117 | 
 | 
|  | 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 | + | 
| 114 | 159 | 
 | 
| 115 | 160 | 	// SchedulingTaskExecutor implementation | 
| 116 | 161 | 
 | 
|  | 
0 commit comments