| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.rice.ksb.messaging.threadpool; | 
  | 17 |  |   | 
  | 18 |  |  import org.apache.log4j.Logger; | 
  | 19 |  |  import org.kuali.rice.core.api.config.property.ConfigContext; | 
  | 20 |  |  import org.kuali.rice.ksb.util.KSBConstants; | 
  | 21 |  |   | 
  | 22 |  |  import java.util.concurrent.Executors; | 
  | 23 |  |  import java.util.concurrent.ScheduledThreadPoolExecutor; | 
  | 24 |  |  import java.util.concurrent.ThreadFactory; | 
  | 25 |  |  import java.util.concurrent.TimeUnit; | 
  | 26 |  |   | 
  | 27 |  |  public class KSBScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor implements KSBScheduledPool { | 
  | 28 |  |   | 
  | 29 | 0 |          private static final Logger LOG = Logger.getLogger(KSBScheduledThreadPoolExecutor.class); | 
  | 30 |  |   | 
  | 31 |  |          private boolean started; | 
  | 32 |  |          private static final int DEFAULT_SIZE = 2; | 
  | 33 |  |   | 
  | 34 |  |          public KSBScheduledThreadPoolExecutor() { | 
  | 35 | 0 |                  super(DEFAULT_SIZE, new KSBThreadFactory()); | 
  | 36 | 0 |          } | 
  | 37 |  |   | 
  | 38 |  |          public boolean isStarted() { | 
  | 39 | 0 |                  return started; | 
  | 40 |  |          } | 
  | 41 |  |   | 
  | 42 |  |          public void start() throws Exception { | 
  | 43 | 0 |                  LOG.info("Starting the KSB scheduled thread pool..."); | 
  | 44 |  |                  try { | 
  | 45 | 0 |                          Integer size = new Integer(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.FIXED_POOL_SIZE)); | 
  | 46 | 0 |                          this.setCorePoolSize(size); | 
  | 47 | 0 |                  } catch (NumberFormatException nfe) { | 
  | 48 |  |                           | 
  | 49 | 0 |                  } | 
  | 50 | 0 |                  LOG.info("...KSB scheduled thread pool successfully started."); | 
  | 51 | 0 |          } | 
  | 52 |  |   | 
  | 53 |  |          public void stop() throws Exception { | 
  | 54 | 0 |                  LOG.info("Stopping the KSB scheduled thread pool..."); | 
  | 55 |  |                  try { | 
  | 56 | 0 |                          this.shutdownNow(); | 
  | 57 | 0 |                          LOG.info("awaiting termination: " + this.awaitTermination(20, TimeUnit.SECONDS)); | 
  | 58 | 0 |                          LOG.info("...KSB scheduled thread pool successfully stopped, isShutdown=" + this.isShutdown()); | 
  | 59 | 0 |                  } catch (Exception e) { | 
  | 60 | 0 |                          LOG.warn("Exception thrown shutting down " + KSBScheduledThreadPoolExecutor.class.getSimpleName(), e); | 
  | 61 | 0 |                  } | 
  | 62 |  |   | 
  | 63 | 0 |          } | 
  | 64 |  |           | 
  | 65 | 0 |          private static class KSBThreadFactory implements ThreadFactory { | 
  | 66 |  |                   | 
  | 67 | 0 |                  private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); | 
  | 68 |  |                   | 
  | 69 |  |                  public Thread newThread(Runnable runnable) { | 
  | 70 | 0 |                          Thread thread = defaultThreadFactory.newThread(runnable); | 
  | 71 | 0 |                          thread.setName("KSB-Scheduled-" + thread.getName()); | 
  | 72 | 0 |                          return thread; | 
  | 73 |  |              } | 
  | 74 |  |          } | 
  | 75 |  |   | 
  | 76 |  |  } |