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