1 16 17 package org.springframework.scheduling.concurrent; 18 19 import java.util.concurrent.Executors ; 20 import java.util.concurrent.RejectedExecutionHandler ; 21 import java.util.concurrent.ScheduledExecutorService ; 22 import java.util.concurrent.ScheduledThreadPoolExecutor ; 23 import java.util.concurrent.ThreadFactory ; 24 import java.util.concurrent.ThreadPoolExecutor ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.beans.factory.DisposableBean; 30 import org.springframework.beans.factory.FactoryBean; 31 import org.springframework.beans.factory.InitializingBean; 32 import org.springframework.util.Assert; 33 import org.springframework.util.ObjectUtils; 34 35 62 public class ScheduledExecutorFactoryBean implements FactoryBean, InitializingBean, DisposableBean { 63 64 protected final Log logger = LogFactory.getLog(getClass()); 65 66 private ScheduledExecutorTask[] scheduledExecutorTasks; 67 68 private int poolSize = 1; 69 70 private ThreadFactory threadFactory = Executors.defaultThreadFactory(); 71 72 private RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy (); 73 74 private boolean exposeUnconfigurableExecutor = false; 75 76 private ScheduledExecutorService executor; 77 78 79 87 public void setScheduledExecutorTasks(ScheduledExecutorTask[] scheduledExecutorTasks) { 88 this.scheduledExecutorTasks = scheduledExecutorTasks; 89 } 90 91 95 public void setPoolSize(int poolSize) { 96 Assert.isTrue(poolSize > 0, "'poolSize' must be 1 or higher"); 97 this.poolSize = poolSize; 98 } 99 100 105 public void setThreadFactory(ThreadFactory threadFactory) { 106 this.threadFactory = (threadFactory != null ? threadFactory : Executors.defaultThreadFactory()); 107 } 108 109 114 public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) { 115 this.rejectedExecutionHandler = 116 (rejectedExecutionHandler != null ? rejectedExecutionHandler : new ThreadPoolExecutor.AbortPolicy ()); 117 } 118 119 127 public void setExposeUnconfigurableExecutor(boolean exposeUnconfigurableExecutor) { 128 this.exposeUnconfigurableExecutor = exposeUnconfigurableExecutor; 129 } 130 131 132 public void afterPropertiesSet() { 133 logger.info("Initializing SchedulerExecutorService"); 134 ScheduledExecutorService executor = 135 createExecutor(this.poolSize, this.threadFactory, this.rejectedExecutionHandler); 136 137 if (!ObjectUtils.isEmpty(this.scheduledExecutorTasks)) { 139 registerTasks(this.scheduledExecutorTasks, executor); 140 } 141 142 this.executor = (this.exposeUnconfigurableExecutor ? 144 Executors.unconfigurableScheduledExecutorService(executor) : executor); 145 } 146 147 160 protected ScheduledExecutorService createExecutor( 161 int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { 162 163 return new ScheduledThreadPoolExecutor (poolSize, threadFactory, rejectedExecutionHandler); 164 } 165 166 172 protected void registerTasks(ScheduledExecutorTask[] tasks, ScheduledExecutorService executor) { 173 for (int i = 0; i < tasks.length; i++) { 174 ScheduledExecutorTask task = tasks[i]; 175 if (task.isOneTimeTask()) { 176 executor.schedule(task.getRunnable(), task.getDelay(), task.getTimeUnit()); 177 } 178 else { 179 if (task.isFixedRate()) { 180 executor.scheduleAtFixedRate( 181 task.getRunnable(), task.getDelay(), task.getPeriod(), task.getTimeUnit()); 182 } 183 else { 184 executor.scheduleWithFixedDelay( 185 task.getRunnable(), task.getDelay(), task.getPeriod(), task.getTimeUnit()); 186 } 187 } 188 } 189 } 190 191 192 public Object getObject() { 193 return this.executor; 194 } 195 196 public Class getObjectType() { 197 return (this.executor != null ? this.executor.getClass() : ScheduledExecutorService .class); 198 } 199 200 public boolean isSingleton() { 201 return true; 202 } 203 204 205 210 public void destroy() { 211 logger.info("Shutting down ScheduledExecutorService"); 212 this.executor.shutdown(); 213 } 214 215 } 216 | Popular Tags |